UNPKG

3.56 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3// const fs = require('fs-extra')
4const path = require('path')
5const program = require('commander')
6const npmRunScript = require('npm-run-script')
7const chalk = require('chalk')
8const opn = require('opn')
9const sleep = require('../utils/sleep')
10const readBuildConfigFile = require('../utils/read-build-config-file')
11const spinner = require('../utils/spinner')
12const setEnvFromCommand = require('../utils/set-env-from-command')
13const __ = require('../utils/translate')
14
15program
16 .version(require('../package').version, '-v, --version')
17 .usage('[options]')
18 .option('--no-build', 'Don\'t build')
19 // .option('--pm2', 'Start with pm2')
20 .option('--config <config-file-path>', 'Set config file')
21 .option('--type <project-type>', 'Set project type')
22 .parse(process.argv)
23
24/**
25 * 打包生产环境,并启动服务器(如果可用)
26 */
27const run = async () => {
28 // 清空 log
29 process.stdout.write('\x1B[2J\x1B[0f')
30
31 const {
32 build,
33 config,
34 type,
35 } = program
36
37 setEnvFromCommand({
38 config, type
39 })
40
41 // 读取构建配置
42 const {
43 dist,
44 // server,
45 } = await readBuildConfigFile()
46
47 // 打包
48 if (build) {
49 const building = spinner(chalk.yellowBright('[super/build] ') + __('build.building'))
50 await new Promise((resolve, reject) => {
51 const child = npmRunScript(
52 `super-build`, {
53 stdio: 'ignore'
54 }
55 )
56 child.once('error', (error) => {
57 console.trace(error)
58 process.exit(1)
59 reject(error)
60 })
61 child.once('exit', (exitCode) => {
62 // console.trace('exit in', exitCode)
63 resolve(exitCode)
64 // process.exit(exitCode)
65 })
66 })
67 building.succeed()
68 await sleep(100)
69 }
70
71 // if (!server) {
72 // // console.log(chalk.redBright('× '))
73 // opn(path.resolve(dist, 'public/index.html'))
74 // return
75 // }
76
77 // 运行服务器
78 const pathServerJS = path.resolve(dist, 'server/index.js')
79 // if (pm2) {
80 // // PM2 方式
81 // console.log('--- pm2 ---')
82 // const pm2 = require('pm2')
83 // const packageInfo = await fs.readJson(path.resolve(process.cwd(), 'package.json'))
84 // const name = `${packageInfo.name}-server`
85 // // const cmd = `pm2`
86 // // + ` pm2.json --only ${name}`
87 // pm2.start(pathServerJS, {
88 // name,
89 // "script": pathServerJS,
90 // "max_memory_restart": "300M",
91 // "instances": 1,
92 // "exec_mode": "cluster",
93 // "out_file": path.resolve(process.cwd(), "logs/dev/server.log"),
94 // "error_file": path.resolve(process.cwd(), "logs/dev/server-error.log")
95 // })
96 // } else {
97 // 正常方式
98 const cmd = `node ${pathServerJS.replace(/\\/g, '/')}`
99 await new Promise((resolve, reject) => {
100 const child = npmRunScript(cmd, {})
101 child.once('error', (error) => {
102 console.trace(error)
103 process.exit(1)
104 reject(error)
105 })
106 child.once('exit', (exitCode) => {
107 // console.trace('exit in', exitCode)
108 resolve(exitCode)
109 // process.exit(exitCode)
110 })
111 })
112 // }
113
114}
115
116run()