UNPKG

3.66 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3// TODO: Remove this once babel-loader updates
4// https://github.com/babel/babel-loader/pull/391
5process.noDeprecation = true;
6
7const yargs = require('yargs');
8const {
9 cond, equals, map, omit, T
10} = require('ramda');
11const build = require('./build');
12const inspect = require('./inspect');
13const start = require('./start');
14const test = require('./test');
15const execute = require('./execute');
16const { exists, req } = require('../src/utils');
17
18const cwd = process.cwd();
19const cli = yargs
20 .usage('Usage: $0 <command> [options]')
21 .help(false)
22 .option('help', {
23 description: 'Show help',
24 boolean: true,
25 default: false,
26 global: true
27 })
28 .option('inspect', {
29 description: 'Output a string representation of the configuration used by webpack and exit',
30 boolean: true,
31 default: false,
32 global: true
33 })
34 .option('use', {
35 description: 'A list of Neutrino middleware used to configure the build',
36 array: true,
37 default: [],
38 global: true
39 })
40 .option('options', {
41 description: 'Set Neutrino options, config, and environment variables, e.g. --options.env.NODE_ENV production',
42 default: {},
43 global: true
44 })
45 .option('quiet', {
46 description: 'Disable console output of CLI commands',
47 boolean: true,
48 default: false,
49 global: true
50 })
51 .option('debug', {
52 description: 'Run in debug mode',
53 boolean: true,
54 default: false,
55 global: true
56 })
57 .option('require', {
58 description: 'Preload a module prior to loading Neutrino; can be used multiple times',
59 array: true,
60 default: [],
61 global: true
62 })
63 .option('no-tty', {
64 description: 'Disable text terminal interactions',
65 boolean: true,
66 default: false,
67 global: true
68 })
69 .command('start', 'Build a project in development mode')
70 .command('build', 'Compile the source directory to a bundled build')
71 .command('test [files..]', 'Run all suites from the test directory or provided files', {
72 coverage: {
73 description: 'Collect test coverage information and generate report',
74 boolean: true,
75 default: false
76 },
77 watch: {
78 description: 'Watch source files for changes and re-run tests',
79 boolean: true,
80 default: false
81 }
82 })
83 .wrap(null);
84const args = cli.argv;
85const command = args._[0];
86const rc = '.neutrinorc.js';
87const cmd = args.inspect ? 'inspect' : command;
88const hasRc = exists(process.cwd(), rc);
89const middleware = [...new Set(hasRc ? [rc] : args.use)];
90
91if (!middleware.length) {
92 throw new Error('No middleware was found. Specify middleware with --use or create a .neutrinorc.js file.');
93}
94
95global.interactive = !args.noTty && (process.stderr && process.stderr.isTTY) && !process.env.CI;
96
97// Merge CLI config options as last piece of middleware, e.g. options.config.devServer.port 4000
98if (args.options) {
99 middleware.push({
100 options: omit(['config'], args.options),
101 use: args.options.config && (({ config }) => {
102 config.merge(args.options.config)
103 })
104 });
105}
106
107process.on('unhandledRejection', (err) => {
108 if (!args.quiet) {
109 console.error('');
110 console.error(err);
111 }
112
113 process.exit(1);
114});
115
116const promises = map((moduleId) => {
117 const module = req(moduleId, cwd);
118
119 return typeof module === 'function' ? module() : module;
120}, args.require);
121
122Promise
123 .all(promises)
124 .then(() => cond([
125 [equals('build'), () => build(middleware, args, cli)],
126 [equals('start'), () => start(middleware, args, cli)],
127 [equals('test'), () => test(middleware, args, cli)],
128 [equals('inspect'), () => inspect(middleware, args, cli)],
129 [T, () => execute(middleware, args, cli)]
130 ])(cmd));