tix-react-ssr
Version:
Tiket.com React Project Scripts
131 lines (119 loc) • 4.11 kB
JavaScript
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
;
const path = require('path');
// Makes the script crash on unhandled rejections instead of silently
// ignoring them. In the future, promise rejections that are not handled will
// terminate the Node.js process with a non-zero exit code.
process.on('unhandledRejection', err => {
throw err;
});
require('../config/env');
const spawn = require('react-dev-utils/crossSpawn');
const args = process.argv.slice(2);
const scriptIndex = args.findIndex(
x => x === 'build' || x === 'eject' || x === 'start' || x === 'test'
);
const format = (time) => {
return time.toTimeString().replace(/.*(\d{2}:\d{2}:\d{2}).*/, '$1');
};
const run = (fn, options) => {
const task = typeof fn.default === 'undefined' ? fn : fn.default;
const start = new Date();
console.info(`[${format(start)}] Starting '${task.name}${options ? ` (${options})` : ''}'...`);
return task(options).then(resolution => {
const end = new Date();
const time = end.getTime() - start.getTime();
console.info(`[${format(end)}] Finished '${task.name}${options ? ` (${options})` : ''}' after ${time} ms`);
return resolution;
})
};
const script = scriptIndex === -1 ? args[0] : args[scriptIndex];
const nodeArgs = scriptIndex > 0 ? args.slice(0, scriptIndex) : [];
switch (script) {
case 'eject':
case 'test': {
const result = spawn.sync(
'node',
nodeArgs
.concat(require.resolve(__dirname + '/../scripts/' + script))
.concat(args.slice(scriptIndex + 1)),
{ stdio: 'inherit' }
);
if (result.signal) {
if (result.signal === 'SIGKILL') {
console.log(
'The build failed because the process exited too early. ' +
'This probably means the system ran out of memory or someone called ' +
'`kill -9` on the process.'
);
} else if (result.signal === 'SIGTERM') {
console.log(
'The build failed because the process exited too early. ' +
'Someone might have called `kill` or `killall`, or the system could ' +
'be shutting down.'
);
}
process.exit(1);
}
process.exit(result.status);
break;
}
case 'build':
case 'start': {
if (require.main === module && process.argv.length > 2) {
// eslint-disable-next-line no-underscore-dangle
delete require.cache[__filename];
// eslint-disable-next-line global-require, import/no-dynamic-require
const module = require(path.resolve(__dirname + '/../scripts/' + script));
run(module).catch(err => {
console.error(err.stack);
process.exit(1)
})
}
break;
}
case 'serve':
{
const result = spawn.sync(
'node',
nodeArgs
.concat('-r')
.concat('dotenv/config')
.concat(require.resolve(__dirname + '/../bin/cluster'))
.concat(require.resolve(__dirname + '/../bin/server.js'))
.concat(args.slice(scriptIndex + 1)),
{ stdio: 'inherit' }
);
if (result.signal) {
if (result.signal === 'SIGKILL') {
console.log(
'The build failed because the process exited too early. ' +
'This probably means the system ran out of memory or someone called ' +
'`kill -9` on the process.'
);
} else if (result.signal === 'SIGTERM') {
console.log(
'The build failed because the process exited too early. ' +
'Someone might have called `kill` or `killall`, or the system could ' +
'be shutting down.'
);
}
process.exit(1);
}
process.exit(result.status);
break;
}
default:
console.log('Unknown script "' + script + '".');
console.log('Perhaps you need to update tix-react-ssr?');
console.log(
'See: https://github.com/tiket/TIX_MOBILE_SSR/blob/master/packages/tix-react-ssr/template/README.md#updating-to-new-releases'
);
break;
}