1 |
|
2 |
|
3 | // const fs = require('fs-extra')
|
4 | const path = require('path')
|
5 | const program = require('commander')
|
6 | const npmRunScript = require('npm-run-script')
|
7 | const chalk = require('chalk')
|
8 | // const opn = require('opn')
|
9 | const sleep = require('../utils/sleep')
|
10 | // const readBuildConfigFile = require('../utils/read-build-config-file')
|
11 | const spinner = require('../utils/spinner')
|
12 | const setEnvFromCommand = require('../utils/set-env-from-command')
|
13 | const validateConfig = require('../libs/validate-config')
|
14 | const __ = require('../utils/translate')
|
15 | // const getCwd = require('../utils/get-cwd')
|
16 |
|
17 | program
|
18 | .version(require('../package').version, '-v, --version')
|
19 | .usage('[options]')
|
20 | .option('--no-build', 'Don\'t build')
|
21 | // .option('--pm2', 'Start with pm2')
|
22 | .option('--config <config-file-path>', 'Set config file')
|
23 | .option('--type <project-type>', 'Set project type')
|
24 | .option('--port <port>', 'Set server port')
|
25 | .parse(process.argv)
|
26 |
|
27 | /**
|
28 | * 打包生产环境,并启动服务器(如果可用)
|
29 | */
|
30 | const run = async () => {
|
31 | // 清空 log
|
32 | process.stdout.write('\x1B[2J\x1B[0f')
|
33 |
|
34 | const {
|
35 | build,
|
36 | config,
|
37 | type,
|
38 | port,
|
39 | } = program
|
40 |
|
41 | setEnvFromCommand({
|
42 | config, type, port
|
43 | })
|
44 |
|
45 | // 读取构建配置
|
46 | const {
|
47 | dist,
|
48 | // server,
|
49 | } = await validateConfig()
|
50 |
|
51 | // 打包
|
52 | if (build) {
|
53 | const building = spinner(chalk.yellowBright('[koot/build] ') + __('build.building'))
|
54 | await new Promise((resolve, reject) => {
|
55 | const child = npmRunScript(
|
56 | `koot-build`, {
|
57 | stdio: 'ignore'
|
58 | }
|
59 | )
|
60 | child.once('error', (error) => {
|
61 | console.trace(error)
|
62 | process.exit(1)
|
63 | reject(error)
|
64 | })
|
65 | child.once('exit', (exitCode) => {
|
66 | // console.trace('exit in', exitCode)
|
67 | resolve(exitCode)
|
68 | // process.exit(exitCode)
|
69 | })
|
70 | })
|
71 | building.succeed()
|
72 | await sleep(100)
|
73 | }
|
74 |
|
75 | // if (!server) {
|
76 | // // console.log(chalk.redBright('× '))
|
77 | // opn(path.resolve(dist, 'public/index.html'))
|
78 | // return
|
79 | // }
|
80 |
|
81 | // 运行服务器
|
82 | const pathServerJS = path.resolve(dist, 'server/index.js')
|
83 | // if (pm2) {
|
84 | // // PM2 方式
|
85 | // console.log('--- pm2 ---')
|
86 | // const pm2 = require('pm2')
|
87 | // const packageInfo = await fs.readJson(path.resolve(getCwd(), 'package.json'))
|
88 | // const name = `${packageInfo.name}-server`
|
89 | // // const cmd = `pm2`
|
90 | // // + ` pm2.json --only ${name}`
|
91 | // pm2.start(pathServerJS, {
|
92 | // name,
|
93 | // "script": pathServerJS,
|
94 | // "max_memory_restart": "300M",
|
95 | // "instances": 1,
|
96 | // "exec_mode": "cluster",
|
97 | // "out_file": path.resolve(getCwd(), "logs/dev/server.log"),
|
98 | // "error_file": path.resolve(getCwd(), "logs/dev/server-error.log")
|
99 | // })
|
100 | // } else {
|
101 | // 正常方式
|
102 | const cmd = `node ${pathServerJS.replace(/\\/g, '/')}`
|
103 | const child = npmRunScript(cmd, {})
|
104 | await new Promise((resolve, reject) => {
|
105 | child.once('error', (error) => {
|
106 | console.trace(error)
|
107 | process.exit(1)
|
108 | reject(error)
|
109 | })
|
110 | child.once('exit', (exitCode) => {
|
111 | // console.trace('exit in', exitCode)
|
112 | resolve(exitCode)
|
113 | // process.exit(exitCode)
|
114 | })
|
115 | })
|
116 | // }
|
117 |
|
118 | /*
|
119 | await new Promise((resolve, reject) => {
|
120 | console.log({
|
121 | 'process.env': process.env
|
122 | })
|
123 | const cmd = `node ${pathServerJS.replace(/\\/g, '/')}`
|
124 | const child = spawn(
|
125 | 'node',
|
126 | [`${pathServerJS.replace(/\\/g, '/')}`]
|
127 | )
|
128 |
|
129 | // child.stdin.pipe(process.stdin)
|
130 | child.stdout.pipe(process.stdout)
|
131 | // child.stderr.pipe(process.stderr)
|
132 | console.log(process.cwd())
|
133 | console.log(cmd)
|
134 |
|
135 | // const child = npmRunScript(cmd, {})
|
136 | child.once('error', (error) => {
|
137 | console.trace(error)
|
138 | process.exit(1)
|
139 | reject(error)
|
140 | })
|
141 | child.once('exit', (exitCode) => {
|
142 | // console.trace('exit in', exitCode)
|
143 | resolve(exitCode)
|
144 | // process.exit(exitCode)
|
145 | })
|
146 | })
|
147 | */
|
148 | const exitHandler = (/*options, err*/) => {
|
149 | child.kill('SIGINT')
|
150 | process.exit(0)
|
151 | }
|
152 |
|
153 | // do something when app is closing
|
154 | process.on('exit', exitHandler);
|
155 | // catches ctrl+c event
|
156 | process.on('SIGINT', exitHandler);
|
157 | // catches "kill pid" (for example: nodemon restart)
|
158 | process.on('SIGUSR1', exitHandler);
|
159 | process.on('SIGUSR2', exitHandler);
|
160 | // catches uncaught exceptions
|
161 | process.on('uncaughtException', exitHandler);
|
162 | }
|
163 |
|
164 | run()
|