1 | path = require 'path'
|
2 | fs = require 'fs'
|
3 |
|
4 |
|
5 | parentDirSync = (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 |
|
20 | break
|
21 |
|
22 | return answer
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 | exports.findScript = (scriptName, options={}) ->
|
29 |
|
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 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 | exports.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 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 | exports.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 |
|
98 | exports.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 |
|
107 |
|
108 | answer = exports.findCommand commandName, {
|
109 | ninjaFilePath: options.ninjaFilePath,
|
110 | configureNinjaScript: options.configureNinjaScript,
|
111 | fromDir: __dirname
|
112 | }
|
113 |
|
114 | return answer
|
115 |
|