1 |
|
2 | (function() {
|
3 | var fs, parentDirSync, path;
|
4 |
|
5 | path = require('path');
|
6 |
|
7 | fs = require('fs');
|
8 |
|
9 | parentDirSync = function(dir, fileToFind) {
|
10 | var answer, dirToCheck, existsSync, oldDirToCheck, _ref;
|
11 | existsSync = (_ref = fs.existsSync) != null ? _ref : path.existsSync;
|
12 | dirToCheck = path.resolve(dir);
|
13 | answer = null;
|
14 | while (true) {
|
15 | if (existsSync(path.join(dirToCheck, fileToFind))) {
|
16 | answer = dirToCheck;
|
17 | break;
|
18 | }
|
19 | oldDirToCheck = dirToCheck;
|
20 | dirToCheck = path.resolve(dirToCheck, "..");
|
21 | if (oldDirToCheck === dirToCheck) {
|
22 | break;
|
23 | }
|
24 | }
|
25 | return answer;
|
26 | };
|
27 |
|
28 | exports.findScript = function(scriptName, options) {
|
29 | var answer, scriptFullPath, scriptsDir;
|
30 | if (options == null) {
|
31 | options = {};
|
32 | }
|
33 | scriptsDir = path.resolve(__dirname, path.join("..", "lib/scripts"));
|
34 | scriptFullPath = path.resolve(scriptsDir, scriptName);
|
35 | if (!fs.existsSync(scriptFullPath)) {
|
36 | throw new Error("Could not find script " + scriptName);
|
37 | }
|
38 | if (options.ninjaFilePath != null) {
|
39 | answer = path.relative(options.ninjaFilePath, scriptFullPath);
|
40 | } else {
|
41 | answer = scriptFullPath;
|
42 | }
|
43 | return answer;
|
44 | };
|
45 |
|
46 | exports.findCommand = function(commandName, options) {
|
47 | var answer;
|
48 | if (options == null) {
|
49 | options = {};
|
50 | }
|
51 | answer = exports.findCommandIfExists(commandName, options);
|
52 | if (!answer) {
|
53 | throw new Error("Could not find command " + commandName);
|
54 | }
|
55 | return answer;
|
56 | };
|
57 |
|
58 | exports.findCommandIfExists = function(commandName, options) {
|
59 | var answer, commandFullPath, currentDir, done, nextDir;
|
60 | if (options == null) {
|
61 | options = {};
|
62 | }
|
63 | if (commandName === "loop-configure-ninja" && (options.configureNinjaScript != null)) {
|
64 | answer = options.configureNinjaScript;
|
65 | } else {
|
66 | answer = null;
|
67 | done = false;
|
68 | currentDir = options.fromDir || options.ninjaFilePath;
|
69 | if (currentDir == null) {
|
70 | throw new Error("Need option ninjaFilePath.");
|
71 | }
|
72 | while (!answer && !done) {
|
73 | currentDir = parentDirSync(currentDir, "node_modules");
|
74 | if (currentDir === null) {
|
75 | done = true;
|
76 | } else {
|
77 | commandFullPath = path.resolve(currentDir, path.join("node_modules", ".bin", commandName));
|
78 | if (fs.existsSync(commandFullPath)) {
|
79 | answer = commandFullPath;
|
80 | } else {
|
81 | nextDir = path.resolve(currentDir, '..');
|
82 | if (nextDir === currentDir) {
|
83 | done = true;
|
84 | }
|
85 | currentDir = nextDir;
|
86 | }
|
87 | }
|
88 | }
|
89 | }
|
90 | if (answer && (options.ninjaFilePath != null)) {
|
91 | answer = path.relative(options.ninjaFilePath, answer);
|
92 | }
|
93 | return answer;
|
94 | };
|
95 |
|
96 | exports.findLocalCommand = function(commandName, options) {
|
97 | var answer;
|
98 | if (options == null) {
|
99 | options = {};
|
100 | }
|
101 | answer = path.resolve(__dirname, path.join("..", "node_modules", ".bin", commandName));
|
102 | if (fs.existsSync(answer)) {
|
103 | if (options.ninjaFilePath != null) {
|
104 | answer = path.relative(options.ninjaFilePath, answer);
|
105 | }
|
106 | } else {
|
107 | answer = exports.findCommand(commandName, {
|
108 | ninjaFilePath: options.ninjaFilePath,
|
109 | configureNinjaScript: options.configureNinjaScript,
|
110 | fromDir: __dirname
|
111 | });
|
112 | }
|
113 | return answer;
|
114 | };
|
115 |
|
116 | }).call(this);
|
117 |
|
118 |
|