1 | const chalk = require('chalk');
|
2 | const parseArgs = require('./util/parse-args');
|
3 | const getBaseDir = require('./util/get-base-dir');
|
4 | const logger = require('./util/logger');
|
5 | const defined = require('defined');
|
6 |
|
7 | const availableCommands = [
|
8 | 'start', 'bundle', 'help', 'clean', 'create'
|
9 | ];
|
10 |
|
11 | let prettyLoggerAttached = false;
|
12 |
|
13 | function attachPrettyError (log) {
|
14 | if (prettyLoggerAttached) return;
|
15 | prettyLoggerAttached = true;
|
16 | process.on('uncaughtException', errListener);
|
17 | process.on('unhandledRejection', errListener);
|
18 |
|
19 | function errListener (err) {
|
20 | console.error(err);
|
21 | process.exit(1);
|
22 | }
|
23 | }
|
24 |
|
25 | module.exports = function (commands = [], entry = '', opts = {}) {
|
26 | const trySome = `Try some of the following:
|
27 | texel help
|
28 | texel create
|
29 | texel start
|
30 | texel bundle`;
|
31 |
|
32 | commands = [].concat(commands).filter(Boolean);
|
33 |
|
34 | if (!commands || commands.length === 0) {
|
35 | commands.push('start');
|
36 | }
|
37 |
|
38 |
|
39 | opts = Object.assign({}, opts);
|
40 | opts.debug = opts.debug !== false;
|
41 | opts.port = parseInt(defined(opts.port, 9966), 10);
|
42 | opts.js = opts.js === false ? false : (opts.js || 'bundle.js');
|
43 | opts.css = opts.css === false ? false : (opts.css || 'bundle.css');
|
44 | opts.html = opts.html === false ? false : (opts.html || 'index.html');
|
45 | opts.minify = opts.minify !== false;
|
46 | opts.quiet = Boolean(opts.quiet);
|
47 | opts.cwd = process.cwd();
|
48 | opts.open = Boolean(opts.open);
|
49 | opts.dir = getBaseDir(defined(opts.dir, 'public'), { cwd: opts.cwd });
|
50 | opts.logger = logger(opts);
|
51 | opts.browserifyOpts = {
|
52 | bare: opts.bare,
|
53 | node: opts.node
|
54 | };
|
55 |
|
56 | if (!opts.js) throw new logger.SimpleError(`Oops, it looks like you specified --no-js or an empty path for the output.\n\nTry specifying a path, like this:\n texel [command] index.js`);
|
57 |
|
58 | const start = (command = '') => {
|
59 | opts.logger.bullet(chalk.green(`texel ${chalk.bold(command)}`));
|
60 | switch (command) {
|
61 | case 'start': return require('./commands/start')(entry, opts);
|
62 | case 'bundle': return require('./commands/bundle')(entry, opts);
|
63 | case 'clean': return require('./commands/clean')(entry, opts);
|
64 | case 'create': return require('./commands/create')(entry, opts);
|
65 | default:
|
66 | throw new logger.SimpleError(`Unknown texel command: ${chalk.bold(command)}\n\n${trySome}`.trim());
|
67 | }
|
68 | };
|
69 |
|
70 | return commands.reduce((p, cmd) => {
|
71 | return p.then(result => start(cmd));
|
72 | }, Promise.resolve());
|
73 | };
|
74 |
|
75 | module.exports.cli = function (args = []) {
|
76 |
|
77 | const commands = [];
|
78 | const origArgs = args.slice();
|
79 | for (let i = 0; i < origArgs.length; i++) {
|
80 | const arg = origArgs[i];
|
81 | if (availableCommands.includes(arg)) {
|
82 |
|
83 | args = origArgs.slice(i + 1);
|
84 | commands.push(arg);
|
85 | } else {
|
86 |
|
87 | break;
|
88 | }
|
89 | }
|
90 |
|
91 | const opts = parseArgs(args);
|
92 | const entry = opts._[0];
|
93 | delete opts._;
|
94 |
|
95 | const log = logger(opts);
|
96 | attachPrettyError(log);
|
97 |
|
98 | return module.exports(commands, entry, opts).catch(err => {
|
99 | log.error(err);
|
100 | process.exit(1);
|
101 | });
|
102 | };
|