UNPKG

3.4 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._noParseFiles = [
52 'node_modules/three/build/three.js',
53 'node_modules/three/build/three.min.js'
54 ];
55 opts.browserifyOpts = {
56 // Speed up ThreeJS apps.
57 // Perhaps this is too magical, may be removed later.
58 noParse: opts._noParseFiles,
59 fullPaths: opts.fullPaths,
60 bare: opts.bare,
61 node: opts.node
62 };
63
64 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`);
65
66 const start = (command = '') => {
67 opts.logger.bullet(chalk.green(`texel ${chalk.bold(command)}`));
68 switch (command) {
69 case 'start': return require('./commands/start')(entry, opts);
70 case 'bundle': return require('./commands/bundle')(entry, opts);
71 case 'clean': return require('./commands/clean')(entry, opts);
72 case 'create': return require('./commands/create')(entry, opts);
73 default:
74 throw new logger.SimpleError(`Unknown texel command: ${chalk.bold(command)}\n\n${trySome}`.trim());
75 }
76 };
77
78 return commands.reduce((p, cmd) => {
79 return p.then(result => start(cmd));
80 }, Promise.resolve());
81};
82
83module.exports.cli = function (args = []) {
84 // Parse out commands
85 const commands = [];
86 const origArgs = args.slice();
87 for (let i = 0; i < origArgs.length; i++) {
88 const arg = origArgs[i];
89 if (availableCommands.includes(arg)) {
90 // Still a command
91 args = origArgs.slice(i + 1);
92 commands.push(arg);
93 } else {
94 // No longer at any commands
95 break;
96 }
97 }
98
99 const opts = parseArgs(args);
100 const entry = opts._[0];
101 delete opts._;
102
103 const log = logger(opts);
104 attachPrettyError(log);
105
106 return module.exports(commands, entry, opts).catch(err => {
107 log.error(err);
108 process.exit(1);
109 });
110};