UNPKG

3.34 kBJavaScriptView Raw
1const chalk = require('chalk');
2const parseArgs = require('./util/parse-args');
3const getBaseDir = require('./util/get-base-dir');
4const logger = require('./util/logger');
5const defined = require('defined');
6
7const availableCommands = [
8 'start', 'bundle', 'help', 'clean', 'create'
9];
10
11let prettyLoggerAttached = false;
12
13function 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
25module.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 // default to start command
34 if (!commands || commands.length === 0) {
35 commands.push('start');
36 }
37
38 // setup defaults
39 opts = Object.assign({}, opts);
40 opts.port = parseInt(defined(opts.port, 9966), 10);
41 opts.js = opts.js === false ? false : (opts.js || 'bundle.js');
42 opts.css = opts.css === false ? false : (opts.css || 'bundle.css');
43 opts.html = opts.html === false ? false : (opts.html || 'index.html');
44 opts.minify = opts.minify !== false;
45 opts.quiet = Boolean(opts.quiet);
46 opts.cwd = process.cwd();
47 opts.open = Boolean(opts.open);
48 opts.dir = getBaseDir(defined(opts.dir, 'public'), { cwd: opts.cwd });
49 opts.logger = logger(opts);
50 opts.env = Object.assign({}, process.env, opts.env, { NODE_ENV: 'development' });
51 opts.browserifyOpts = {
52 // Speed up ThreeJS apps.
53 // Perhaps this is too magical, may be removed later.
54 noParse: [
55 'node_modules/three/build/three.js',
56 'node_modules/three/build/three.min.js'
57 ],
58 bare: opts.bare,
59 node: opts.node
60 };
61
62 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`);
63
64 const start = (command = '') => {
65 opts.logger.bullet(chalk.green(`texel ${chalk.bold(command)}`));
66 switch (command) {
67 case 'start': return require('./commands/start')(entry, opts);
68 case 'bundle': return require('./commands/bundle')(entry, opts);
69 case 'clean': return require('./commands/clean')(entry, opts);
70 case 'create': return require('./commands/create')(entry, opts);
71 default:
72 throw new logger.SimpleError(`Unknown texel command: ${chalk.bold(command)}\n\n${trySome}`.trim());
73 }
74 };
75
76 return commands.reduce((p, cmd) => {
77 return p.then(result => start(cmd));
78 }, Promise.resolve());
79};
80
81module.exports.cli = function (args = []) {
82 // Parse out commands
83 const commands = [];
84 const origArgs = args.slice();
85 for (let i = 0; i < origArgs.length; i++) {
86 const arg = origArgs[i];
87 if (availableCommands.includes(arg)) {
88 // Still a command
89 args = origArgs.slice(i + 1);
90 commands.push(arg);
91 } else {
92 // No longer at any commands
93 break;
94 }
95 }
96
97 const opts = parseArgs(args);
98 const entry = opts._[0];
99 delete opts._;
100
101 const log = logger(opts);
102 attachPrettyError(log);
103
104 return module.exports(commands, entry, opts).catch(err => {
105 log.error(err);
106 process.exit(1);
107 });
108};