-
Notifications
You must be signed in to change notification settings - Fork 5
/
cli.js
executable file
·106 lines (91 loc) · 3 KB
/
cli.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
#!/usr/bin/env node
'use strict';
require('colorful').colorful();
require('gnode');
require('babel-core/register')({
ignore: '',
only: /scripts-hook\/_index/
});
var program = require('commander');
var log = require('spm-log');
var join = require('path').join;
var exists = require('fs').existsSync;
var readFile = require('fs').readFileSync;
var Build = require('./lib').Build;
var hook = require('scripts-hook');
program
.option('-I, --input-directory <dir>', 'input directory, default: current working directory')
.option('-O, --output-directory <dir>', 'output directory, default: dist')
.option('-o, --output-file <file>', 'output file')
.option('-f, --force', 'force to clean dest directory first')
.option('-r, --registry <url>', 'registry url of yuan server')
.option('--include <include>', 'file include mode with seajs, optional: self, relative, all')
.option('--global <global>', 'replace package name to global variable, format jquery:$,underscore:_')
.option('--ignore <ignore>', 'determine which id will not be transported')
.option('--skip <skip>', 'determine which id will not be parsed when analyse')
.option('--idleading [idleading]', 'prefix of module name, default: {{name}}/{{version}}')
.option('--with-deps', 'build package in dependencies')
.option('--verbose', 'show more logging')
.option('--no-color', 'disable colorful print')
.option('--no-install', 'disable install')
.parse(process.argv);
log.config(program);
var cwd = join(process.cwd(), program.inputDirectory || '');
var file = join(cwd, 'package.json');
var pkg = exists(file) && JSON.parse(readFile(file, 'utf-8')) || {};
var entry = program.args;
var info = '';
if (entry.length) {
info = 'build ' + entry.join(',');
} else if (!pkg || !pkg.spm) {
console.log();
log.error('miss', 'package.json or "spm" key');
console.log();
process.exit(1);
} else {
var pkgId = pkg.name && pkg.version && (pkg.name + '@' + pkg.version) || '';
info = ('build ' + pkgId).to.magenta.color;
}
var begin = Date.now();
console.log();
log.info('start', info);
var args = {
dest: program.outputDirectory,
cwd: cwd,
outputFile: program.outputFile,
include: program.include,
ignore: program.ignore,
global: program.global,
skip: program.skip,
idleading: program.idleading,
registry: program.registry || (pkg.spm && pkg.spm.registry),
withDeps: program.withDeps,
zip: program.zip,
force: program.force,
install: program.install,
originPkg: pkg,
entry: entry
};
var scripts = pkg && pkg.spm && pkg.spm.scripts || {};
hook(scripts, 'build', function(done) {
new Build(args)
.getArgs()
.installDeps()
.parsePkg()
.addCleanTask()
.run(function(err) {
if (err) {
log.error(err.message);
log.error(err.stack);
process.exit(1);
}
done();
});
}).then(function() {
log.info('finish', info + showDiff(begin));
console.log();
}, function() {});
function showDiff(time) {
var diff = Date.now() - time;
return (' (' + diff + 'ms)').to.gray.color;
}