UNPKG

3.32 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3var util = require('util');
4var path = require('path');
5var assemble = require('..');
6var commands = require('../lib/commands');
7var plugins = require('../lib/plugins');
8var utils = require('../lib/utils');
9var errors = require('./errors');
10var argv = utils.parseArgs(process.argv.slice(2));
11
12function run(cb) {
13 var cwd = process.cwd();
14 var app;
15
16 /**
17 * Set the working directory
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 * Log the working directory
28 */
29
30 console.log(utils.log.timestamp, 'using cwd ' + utils.formatDir(cwd));
31
32 /**
33 * Get the assemblefile.js to use
34 */
35
36 var assemblefile = path.resolve(cwd, argv.file || 'assemblefile.js');
37
38 /**
39 * Get the `assemble` instance to use
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 * If `app` is a function, it's an assemble "generator",
52 * so we need to invoke it with an instance of assemble
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 * Listen for errors
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 * Support `--emit` for debugging
86 *
87 * Example:
88 * $ --emit data
89 */
90
91 if (argv.emit && typeof argv.emit === 'string') {
92 app.on(argv.emit, app.log.bind(console));
93 }
94
95 /**
96 * Process command line arguments
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 * Show path to assemblefile
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 * Registert `runtimes` plugin
117 */
118
119 app.use(plugins.runtimes());
120
121 /**
122 * Process command line arguments
123 */
124
125 cb(null, app);
126}
127
128/**
129 * Run
130 */
131
132run(function(err, app) {
133 if (err) handleError(err);
134 commands(app);
135
136 /**
137 * Process command line arguments
138 */
139
140 app.cli.process(app.get('cache.argv'), function(err) {
141 if (err) handleError(err);
142
143 /**
144 * Run tasks
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 * Handle CLI errors
157 */
158
159function 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}