UNPKG

4.41 kBtext/coffeescriptView Raw
1path = require 'path'
2fs = require 'fs'
3
4# Find the first parent directory of `dir` which contains a file named `fileToFind`.
5parentDirSync = (dir, fileToFind) ->
6 existsSync = fs.existsSync ? path.existsSync
7
8 dirToCheck = path.resolve dir
9
10 answer = null
11 while true
12 if existsSync path.join(dirToCheck, fileToFind)
13 answer = dirToCheck
14 break
15
16 oldDirToCheck = dirToCheck
17 dirToCheck = path.resolve dirToCheck, ".."
18 if oldDirToCheck == dirToCheck
19 # We've hit '/'. We're done
20 break
21
22 return answer
23
24# Returns the path to a script, relative to the directory we're writing the build.ninja file into.
25#
26# `options.ninjaFilePath` is the path to the build.ninja file being generated.
27#
28exports.findScript = (scriptName, options={}) ->
29 # Look for this script in the scripts dir
30 scriptsDir = path.resolve __dirname, path.join("..", "lib/scripts")
31 scriptFullPath = path.resolve scriptsDir, scriptName
32 if !fs.existsSync scriptFullPath
33 throw new Error("Could not find script #{scriptName}")
34
35 if options.ninjaFilePath?
36 answer = path.relative options.ninjaFilePath, scriptFullPath
37 else
38 answer = scriptFullPath
39
40 return answer
41
42# Finds a command.
43#
44# This will find a node_modules directory in the current directory or an ancestor of the current
45# directory that contains ".bin/#{command}" and return the relative path to the command.
46#
47# Throws an exception if the command cannot be found.
48#
49# * `options.ninjaFilePath` is the path to the build.ninja file being generated.
50# * `options.configureNinjaScript` is the full path to the loop-configure-ninja executable.
51# * `options.fromDir` is the directory to start searching for the command in. If not specified,
52# the search will start at `options.ninjaFilePath`.
53#
54exports.findCommand = (commandName, options={}) ->
55 answer = exports.findCommandIfExists commandName, options
56 if !answer then throw new Error("Could not find command #{commandName}")
57 return answer
58
59# Finds a command.
60#
61# This will find a node_modules directory in the current directory or an ancestor of the current
62# directory that contains ".bin/#{command}" and return the relative path to the command.
63#
64# * `options.ninjaFilePath` is the path to the build.ninja file being generated.
65# * `options.configureNinjaScript` is the full path to the loop-configure-ninja executable.
66# * `options.fromDir` is the directory to start searching for the command in. If not specified,
67# the search will start at `options.ninjaFilePath`.
68#
69exports.findCommandIfExists = (commandName, options={}) ->
70 if commandName is "loop-configure-ninja" and options.configureNinjaScript?
71 answer = options.configureNinjaScript
72 else
73 answer = null
74 done = false
75 currentDir = options.fromDir or options.ninjaFilePath
76 if !currentDir? then throw new Error "Need option ninjaFilePath."
77
78 while !answer and !done
79 currentDir = parentDirSync currentDir, "node_modules"
80 if currentDir == null
81 done = true
82 else
83 commandFullPath = path.resolve currentDir, path.join("node_modules", ".bin", commandName)
84 if fs.existsSync commandFullPath
85 answer = commandFullPath
86 else
87 nextDir = path.resolve currentDir, '..'
88 if nextDir == currentDir then done = true
89 currentDir = nextDir
90
91 if answer && options.ninjaFilePath?
92 answer = path.relative options.ninjaFilePath, answer
93
94 return answer
95
96
97# Finds a command from this project's node_modules.
98exports.findLocalCommand = (commandName, options={}) ->
99 answer = path.resolve __dirname, path.join("..", "node_modules", ".bin", commandName)
100
101 if fs.existsSync answer
102 if options.ninjaFilePath?
103 answer = path.relative options.ninjaFilePath, answer
104
105 else
106 # This can happen if the project which is requiring benbria-build already requires one
107 # of our dependencies - the command will be in the parent project.
108 answer = exports.findCommand commandName, {
109 ninjaFilePath: options.ninjaFilePath,
110 configureNinjaScript: options.configureNinjaScript,
111 fromDir: __dirname
112 }
113
114 return answer
115