You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

97 lines
2.8 KiB

#!/usr/bin/env node
// TODO: Remove this once babel-loader updates
// https://github.com/babel/babel-loader/pull/391
process.noDeprecation = true;
const Neutrino = require('../src/neutrino');
const yargs = require('yargs');
const { join } = require('path');
const cwd = process.cwd();
let pkg = {};
try {
pkg = require(join(cwd, 'package.json'));
} catch (ex) {}
const pkgPresets = pkg.neutrino && pkg.neutrino.presets ? pkg.neutrino.presets : [];
const environments = { build: 'production', start: 'development', test: 'test' };
const args = yargs
.option('presets', {
description: 'A list of Neutrino presets used to configure the build',
array: true,
default: [],
global: true
})
.command('start', 'Build a project in development mode')
.command('build', 'Compile the source directory to a bundled build')
.command('test [files..]', 'Run all suites from the test directory or provided files', {
coverage: {
description: 'Collect test coverage information and generate report',
boolean: true,
default: false
},
watch: {
description: 'Watch source files for changes and re-run tests',
boolean: true,
default: false
}
})
.demandCommand(1, 'You must specify a command for Neutrino to run.\nUSAGE: neutrino <command>')
.recommendCommands()
.strict()
.version()
.help()
.argv;
function run(command, presets) {
process.env.NODE_ENV = environments[command];
const options = pkg.neutrino && pkg.neutrino.options ? pkg.neutrino.options : {};
const api = new Neutrino(options);
const cwd = process.cwd();
// Grab all presets and merge them into a single webpack-chain config instance
presets.forEach(preset => {
const paths = [
join(cwd, preset),
join(cwd, 'node_modules', preset),
preset
];
for (let i = 0; i < paths.length; i += 1) {
try {
return api.use(require(paths[i]));
} catch (exception) {
if (/Cannot find module/.test(exception.message)) {
continue;
}
exception.message = `Neutrino was unable to load the module '${preset}'. ` +
`Ensure this module exports a function and is free from errors.\n${exception.message}`;
throw exception;
}
}
throw new Error(`Neutrino cannot find a module with the name or path '${preset}'. ` +
`Ensure this module can be found relative to the root of the project.`);
});
// Also grab any Neutrino config from package.json and merge it into the config at a higher precedence
if (pkg.neutrino && pkg.neutrino.config) {
api.config.merge(pkg.neutrino.config);
}
api[command](args)
.then(() => process.exit(0))
.catch(err => {
if (err) {
console.error(err);
}
process.exit(1);
});
}
run(args._[0], [...new Set(pkgPresets.concat(args.presets))]);