1 | #!/usr/bin/env node
|
2 |
|
3 | var util = require('util');
|
4 | var path = require('path');
|
5 | var assemble = require('..');
|
6 | var commands = require('../lib/commands');
|
7 | var plugins = require('../lib/plugins');
|
8 | var utils = require('../lib/utils');
|
9 | var errors = require('./errors');
|
10 | var argv = utils.parseArgs(process.argv.slice(2));
|
11 |
|
12 | function run(cb) {
|
13 | var cwd = process.cwd();
|
14 | var app;
|
15 |
|
16 | |
17 |
|
18 |
|
19 |
|
20 | if (argv.cwd && cwd !== path.resolve(argv.cwd)) {
|
21 | process.ORIG_CWD = process.cwd();
|
22 | process.chdir(argv.cwd);
|
23 | cwd = process.cwd();
|
24 | }
|
25 |
|
26 | |
27 |
|
28 |
|
29 |
|
30 | console.log(utils.log.timestamp, 'using cwd ' + utils.formatDir(cwd));
|
31 |
|
32 | |
33 |
|
34 |
|
35 |
|
36 | var assemblefile = path.resolve(cwd, argv.file || 'assemblefile.js');
|
37 |
|
38 | |
39 |
|
40 |
|
41 |
|
42 | var defaults = require('../lib/generator');
|
43 | var configfile = utils.exists(assemblefile);
|
44 | if (configfile) {
|
45 | app = require(assemblefile);
|
46 | } else {
|
47 | app = defaults;
|
48 | }
|
49 |
|
50 | |
51 |
|
52 |
|
53 |
|
54 |
|
55 | if (typeof app === 'function') {
|
56 | var fn = app;
|
57 | app = assemble(argv);
|
58 | app.option(argv);
|
59 | app.use(fn);
|
60 | } else if (Object.keys(app).length === 0) {
|
61 | var msg = util.format(errors['instance'], utils.homeRelative(assemblefile));
|
62 | cb(new Error(msg));
|
63 | return;
|
64 | }
|
65 |
|
66 | assemble.initPlugins(app);
|
67 |
|
68 | |
69 |
|
70 |
|
71 |
|
72 | app.on('error', handleError);
|
73 | app.on('build', function(event, build) {
|
74 | if (typeof event === 'string' && !build.isSilent) {
|
75 | app.log.time(event, build.key, app.log.magenta(build.time));
|
76 | }
|
77 | });
|
78 | app.on('task', function(event, task) {
|
79 | if (typeof event === 'string' && !task.isSilent) {
|
80 | app.log.time(event, task.key);
|
81 | }
|
82 | });
|
83 |
|
84 | |
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 | if (argv.emit && typeof argv.emit === 'string') {
|
92 | app.on(argv.emit, app.log.bind(console));
|
93 | }
|
94 |
|
95 | |
96 |
|
97 |
|
98 |
|
99 | var tasks = argv._.length ? argv._ : ['default'];
|
100 | var args = app.argv(argv);
|
101 | args.tasks = tasks;
|
102 |
|
103 | app.set('cache.argv', args);
|
104 | app.option(args);
|
105 |
|
106 | |
107 |
|
108 |
|
109 |
|
110 | if (configfile) {
|
111 | var fp = utils.homeRelative(assemblefile);
|
112 | app.log.path('using assemblefile ' + utils.colors.green('~/' + fp));
|
113 | }
|
114 |
|
115 | |
116 |
|
117 |
|
118 |
|
119 | app.use(plugins.runtimes());
|
120 |
|
121 | |
122 |
|
123 |
|
124 |
|
125 | cb(null, app);
|
126 | }
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 | run(function(err, app) {
|
133 | if (err) handleError(err);
|
134 | commands(app);
|
135 |
|
136 | |
137 |
|
138 |
|
139 |
|
140 | app.cli.process(app.get('cache.argv'), function(err) {
|
141 | if (err) handleError(err);
|
142 |
|
143 | |
144 |
|
145 |
|
146 |
|
147 | var tasks = app.get('cache.argv.tasks');
|
148 | app.build(tasks, function(err) {
|
149 | if (err) handleError(err);
|
150 | app.log.success('finished');
|
151 | });
|
152 | });
|
153 | });
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 | function handleError(err) {
|
160 | if (typeof err === 'string' && errors[err]) {
|
161 | console.error(errors[err]);
|
162 | } else {
|
163 | if (argv.verbose) {
|
164 | console.error(err.stack);
|
165 | } else {
|
166 | console.error(err.message);
|
167 | }
|
168 | }
|
169 | process.exit(1);
|
170 | }
|