1 | #!/usr/bin/env node
|
2 | const detect = require('detect-port');
|
3 | const inquirer = require('inquirer');
|
4 | const parse = require('yargs-parser');
|
5 | const start = require('../dist/commands/start');
|
6 | const log = require('../dist/utils/log');
|
7 |
|
8 | const rawArgv = parse(process.argv.slice(2), {
|
9 | configuration: { 'strip-dashed': true },
|
10 | });
|
11 |
|
12 | const DEFAULT_PORT = rawArgv.port || process.env.PORT || 3333;
|
13 | const defaultPort = parseInt(DEFAULT_PORT, 10);
|
14 |
|
15 | (async () => {
|
16 | let newPort = await detect(defaultPort);
|
17 | if (newPort !== defaultPort) {
|
18 | const question = {
|
19 | type: 'confirm',
|
20 | name: 'shouldChangePort',
|
21 | message: `${defaultPort} 端口已被占用,是否使用 ${newPort} 端口启动?`,
|
22 | default: true,
|
23 | };
|
24 | const answer = await inquirer.prompt(question);
|
25 | if (!answer.shouldChangePort) {
|
26 | newPort = null;
|
27 | }
|
28 | }
|
29 | if (newPort === null) {
|
30 | process.exit(1);
|
31 | }
|
32 |
|
33 | process.env.NODE_ENV = 'development';
|
34 | rawArgv.port = parseInt(newPort, 10);
|
35 |
|
36 |
|
37 | delete rawArgv._;
|
38 | try {
|
39 | const devServer = await start({
|
40 | args: { ...rawArgv },
|
41 | });
|
42 |
|
43 | ['SIGINT', 'SIGTERM'].forEach(function(sig) {
|
44 | process.on(sig, function() {
|
45 | devServer.close();
|
46 | process.exit(0);
|
47 | });
|
48 | });
|
49 | } catch (err) {
|
50 | log.error(err.message);
|
51 | console.error(err);
|
52 | process.exit(1);
|
53 | }
|
54 | })();
|