-
Notifications
You must be signed in to change notification settings - Fork 1
/
aardvark.js
191 lines (154 loc) · 3.9 KB
/
aardvark.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
var cp = require('child_process'),
fs = require('fs'),
path = require('path'),
TS_TIME_TO_EXEC = 'Time to exec',
ADVK_CONFIG_FILE = '.advk.json',
cwd = process.cwd();
console.time(TS_TIME_TO_EXEC);
function NO_OP() {}
function isExistingDirectory(dir, callback) {
fs.exists(dir, function(dirExists) {
if (!dirExists) {
callback(null, false);
return;
}
fs.stat(dir, function(err, stats) {
if (err) {
callback(err);
} else {
callback(null, stats.isDirectory());
}
});
});
}
function findProperties(dir, callback) {
var propsFile = path.join(dir, ADVK_CONFIG_FILE);
function next() {
var parentDir = path.join(dir, '../');
if (parentDir === dir) {
callback('No .advk.json file found in the folder hierarchy');
} else {
findProperties(parentDir, callback);
}
}
fs.exists(propsFile, function(exists) {
if (!exists) {
return next();
}
fs.readFile(propsFile, { encoding: 'utf8' }, function(err, contents) {
var properties;
if (err) {
return callback(err);
}
try {
properties = JSON.parse(contents);
properties.taskRunnerDir = path.resolve(dir, properties.taskRunnerDir);
callback(null, properties);
} catch(e) {
callback(e);
}
});
});
}
// Inspired by https://github.com/caolan/async
function all(fns, callback) {
var completed = 0,
total = fns.length,
results = [];
function done(index, err, result) {
if (err) {
callback(err);
callback = NO_OP;
} else {
completed += 1;
results[index] = result;
if (completed >= total) {
callback(null, results);
callback = NO_OP;
}
}
}
fns.forEach(function(fn, index) {
fn(done.bind(null, index));
});
}
function statFile(file) {
return function(callback) {
fs.stat(file, callback);
};
}
function ensureDependencies(taskRunnerDir, callback) {
var nodeModulesDir = path.join(taskRunnerDir, 'node_modules');
fs.exists(nodeModulesDir, function(exists) {
if (!exists) {
return npmInstall(taskRunnerDir, callback);
}
all([
statFile(nodeModulesDir),
statFile(path.join(taskRunnerDir, 'package.json'))
], function(err, stats) {
if (err) {
return callback(err);
}
var nodeModulesStats = stats[0],
packageJsonStats = stats[1];
if (!nodeModulesStats.isDirectory()) {
return callback(nodeModulesDir + ' is not a directory!');
}
if (packageJsonStats.mtime.getTime() > nodeModulesStats.mtime.getTime()) {
npmInstall(taskRunnerDir, callback);
} else {
callback();
}
});
});
}
function exec(cmd, args, cwd, callback) {
var execCmd = cmd,
opts = {
cwd: cwd,
stdio: 'inherit'
};
if (process.platform === 'win32') {
execCmd = 'cmd';
args = ['/c', '"' + cmd + '"'].concat(args);
opts.windowsVerbatimArguments = true;
}
cp.spawn(execCmd, args, opts)
.on('exit', callback);
}
function npmInstall(taskRunnerDir, callback) {
exec('npm', ['install'/*, '--production'*/], taskRunnerDir, function(code) {
callback(code ? ('npm install exited with code ' + code) : null);
});
}
function logAndExit(err) {
console.error(err);
process.exit(1);
}
function execTaskRunner(properties) {
var args = [properties.workingDirArg, cwd].concat(process.argv.slice(2)),
taskRunnerDir = properties.taskRunnerDir;
console.timeEnd(TS_TIME_TO_EXEC);
exec(path.join(taskRunnerDir, 'node_modules', '.bin', properties.taskRunnerBin), args, taskRunnerDir, function(code) {
if (code) {
logAndExit(properties.taskRunnerBin + ' process exited with code ' + code);
}
});
}
findProperties(cwd, function(err, properties) {
if (err) {
return logAndExit(err);
}
isExistingDirectory(properties.taskRunnerDir, function(err, exists) {
if (err || !exists) {
return logAndExit(err || 'Specified taskRunnerDir "' + properties.taskRunnerDir + '" not found.');
}
ensureDependencies(properties.taskRunnerDir, function(err) {
if (err) {
return logAndExit(err);
}
execTaskRunner(properties);
});
});
});