UNPKG

1.35 kBJavaScriptView Raw
1/** Copyright (c) 2018 Uber Technologies, Inc.
2 *
3 * This source code is licensed under the MIT license found in the
4 * LICENSE file in the root directory of this source tree.
5 *
6 * @flow
7 */
8
9/* eslint-env node */
10
11const fs = require('fs');
12const path = require('path');
13const cp = require('child_process');
14
15exports.run = async function({dir = '.', environment, port, debug} /*: any */) {
16 if (debug && !process.env.__FUSION_DEBUGGING__) {
17 const command = process.argv.shift();
18 const args = process.argv;
19 args.unshift('--inspect-brk');
20 return cp.spawn(command, args, {
21 stdio: 'inherit',
22 env: {
23 ...process.env,
24 __FUSION_DEBUGGING__: true,
25 },
26 });
27 }
28
29 const getEntry = env => {
30 const entryPath = `.fusion/dist/${env}/server/server-main.js`;
31 return path.resolve(dir, entryPath);
32 };
33
34 const env = environment
35 ? fs.existsSync(getEntry(environment)) && environment
36 : ['development', 'production'].find(e => fs.existsSync(getEntry(e)));
37
38 if (env) {
39 const entry = getEntry(env);
40 // $FlowFixMe
41 const {start} = require(entry);
42 return start({dir, port: port || process.env.PORT_HTTP || 3000}); // handle server bootstrap errors (e.g. port already in use)
43 } else {
44 throw new Error(`App can't start. JS isn't compiled`); // handle compilation errors
45 }
46};