-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
41 lines (40 loc) · 1.2 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/env node
const detect = require('detect-port');
const inquirer = require('react-dev-utils/inquirer');
const chalk = require('chalk');
const start = require('./server.js');
const canRunServer = require('./script/needFileCheck.js');
const question = require('./script/question.js');
const useTs = require('./script/useTs.js');
const { DEFAULT_PORT } = question; // 默认端口号
// 初始化执行函数
async function init(entryFile) {
try {
const { canUseTs } = await useTs();
const startParam = {
port: DEFAULT_PORT,
entryFile,
canUseTs,
};
// 检测默认端口是否被占用
const port = await detect(DEFAULT_PORT);
if (DEFAULT_PORT === port) {
start(startParam);
return false;
}
try {
const { changePort } = await inquirer.prompt([question.postQuestion]);
if (changePort) {
startParam.port = port;
start(startParam);
}
} catch (err) {
chalk.red('An error occurred~, please try again or contact us~');
}
} catch (err) {
chalk.red('An error occurred~, please try again or contact us~');
process.exit();
}
return Promise.resolve().then(() => ({ isOk: true }));
}
canRunServer(init);