/
index.js
315 lines (276 loc) · 7.99 KB
/
index.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
var Parser = require('posix-getopt').BasicParser;
var assert = require('assert');
var debug = require('debug')('strong-build');
var fmt = require('util').format;
var fs = require('fs');
var git = require('./lib/git');
var json = require('json-file-plus');
var lodash = require('lodash');
var path = require('path');
var shell = require('shelljs');
var vasync = require('vasync');
function printHelp($0, prn) {
var USAGE = fs.readFileSync(require.resolve('./sl-build.txt'), 'utf-8')
.replace(/%MAIN%/g, $0)
.trim();
prn(USAGE);
}
function runCommand(cmd, callback) {
debug('run command: %s', cmd);
shell.exec(cmd, {silent: true}, function(code, output) {
debug('code %d: <<<\n%s>>>', code, output);
if (code !== 0) {
var er = Error(cmd);
}
return callback(er, output, code);
});
}
function runWait(cmd, callback) {
console.log('Running `%s`', cmd);
runCommand(cmd, function(er, output) {
if (er) {
console.error('Error on `%s`:', cmd);
reportRunError(er, output);
return callback(er);
}
return callback(null, output);
});
}
function runStep(cmd, callback) {
return function(_, callback) {
runWait(cmd, function(er) {
return callback(er); // do not return output of runWait()
});
};
}
function reportRunError(er, output) {
if (!er) return;
console.error("Failed to run `%s`:", er.message);
if (output && output !== '') {
process.stderr.write(output);
}
}
exports.build = function build(argv, callback) {
var $0 = process.env.SLC_COMMAND ?
'slc ' + process.env.SLC_COMMAND :
path.basename(argv[1]);
var parser = new Parser([
':v(version)',
'h(help)',
'n(npm)',
'g(git)',
's(scripts)',
'i(install)',
'b(bundle)',
'p(pack)',
'O:(onto)',
'c(commit)',
'N(no-commit)',
].join(''),
argv);
var option;
var onto = 'deploy';
var install;
var scripts;
var bundle;
var pack;
var commit;
while ((option = parser.getopt()) !== undefined) {
switch (option.option) {
case 'v':
console.log(require('./package.json').version);
return callback();
case 'h':
printHelp($0, console.log);
return callback();
case 'n':
install = true;
commit = false;
bundle = pack = true;
break;
case 'g':
install = true;
commit = true;
bundle = pack = false;
break;
case 's':
scripts = true;
break;
case 'i':
install = true;
break;
case 'b':
bundle = true;
break;
case 'p':
pack = true;
break;
case 'O':
onto = option.optarg;
break;
case 'c':
commit = true;
break;
case 'N':
commit = false;
break;
default:
console.error('Invalid usage (near option \'%s\'), try `%s --help`.',
option.optopt, $0);
return callback(Error('usage'));
}
}
if (parser.optind() !== argv.length) {
console.error('Invalid usage (extra arguments), try `%s --help`.', $0);
return callback(Error('usage'));
}
// With no actions selected, do everything we can (onto requires an argument,
// so we can't do it automatically).
if (!(install || pack || commit || bundle)) {
install = true;
if (git.isGit()) {
commit = true;
bundle = pack = false;
} else {
commit = false;
bundle = pack = true;
}
}
if (commit && !git.isGit()) {
console.error('Cannot perform commit on non-git working directory');
return callback(Error('usage'));
}
var steps = [];
if (commit) {
steps.push(doEnsureGitBranch);
steps.push(doGitSyncBranch);
}
if (install) {
steps.push(doNpmInstall);
}
if (bundle) {
steps.push(doBundle);
}
if (pack) {
steps.push(doNpmPack);
}
if (commit) {
steps.push(doGitCommit);
}
vasync.pipeline({funcs: steps}, callback);
function doEnsureGitBranch(_, callback) {
try {
git.ensureBranch(onto);
} catch(er) {
console.error('%s', er.message);
return callback(er);
}
return callback();
}
function doGitSyncBranch(_, callback) {
try {
var info = git.syncBranch(onto);
if (info.srcBranch && info.dstBranch)
console.log('Merged source tree of `%s` onto `%s`',
info.srcBranch, info.dstBranch);
else
console.log('Not merging HEAD into `%s`, already up to date.', onto);
} catch(er) {
console.error('%s', er.message);
return callback(er);
}
return callback();
}
function doNpmInstall(_, callback) {
var pkg = require(path.resolve('package.json'));
var install = 'npm install';
if (!scripts) {
install += ' --ignore-scripts';
}
var steps = [ runStep(install) ];
if (pkg.scripts && pkg.scripts.build) {
steps.push(runStep('npm run build'));
}
steps.push(runStep('npm prune --production'));
vasync.pipeline({ funcs: steps }, function(er) {
return callback(er);
});
}
function doBundle(_, callback) {
// Build output won't get packed if it is .npmignored (a configuration
// error, don't .npmignore your build output) or if there is no .npmignore,
// if it is .gitignored (as they should be). So, create an empty .npmignore
// if there is a .gitignore but not a .npmignore so build products are
// packed.
if (fs.existsSync('.gitignore') && !fs.existsSync('.npmignore')) {
console.log('Running `touch .npmignore`');
console.warn('Check the auto-generated .npmignore is correct!');
fs.close(fs.openSync('.npmignore', 'a'));
}
// node_modules is unconditionally ignored by npm pack, the only way to get
// the dependencies packed is to name them in the package.json's
// bundledDepenencies.
var info = require(path.resolve('package.json'));
if (info.bundleDependencies || info.bundledDependencies) {
// Use package specified dependency bundling
return callback();
}
// Bundle non-dev dependencies. Optional deps may fail to build at deploy
// time, that's OK, but must be present during packing. If the user has
// more specific desires, they can configure the dependencies themselves, or
// just not run the --bundle action.
var bundled = lodash.union(
Object.keys(info.dependencies || {}),
Object.keys(info.optionalDependencies || {})
).sort();
debug('saving bundled: %j', bundled);
if (bundled.length < 1) {
return callback();
}
console.log('Setting package.json "bundleDependencies" to: [\n %s\n]',
bundled.join(',\n '));
// Re-write package.json, preserving its format if possible.
json('package.json', function(er, p) {
if (er) {
console.error('Error reading package.json: %s', er.message);
return callback(er);
}
p.data.bundleDependencies = bundled;
p.save(function(er) {
if (er) {
console.error('Error writing package.json: %s', er.message);
return callback(er);
}
return callback();
});
});
}
function doNpmPack(_, callback) {
var ignoreFiles = shell.find('node_modules').filter(function(file) {
return file.match(/\.(git|npm)ignore$/);
});
shell.rm('-f', ignoreFiles);
runWait('npm --quiet pack', function(er, output) {
if (er) return callback(er);
var pkg = JSON.parse(fs.readFileSync('package.json'));
var src = fmt('%s-%s.tgz', pkg.name, pkg.version);
var dst = path.join('..', src);
console.log('Running `mv -f %s %s`', src, dst);
shell.mv('-f', src, dst);
return callback();
});
}
function doGitCommit(_, callback) {
try {
var info = git.commitAll(onto);
if (info.branch)
console.log('Committed build products onto `%s`', info.branch);
else
console.log('Build products already up to date on `%s`', onto);
} catch(er) {
console.error('%s', er.message);
return callback(er);
}
return callback();
}
};