UNPKG

5.79 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3const fs = require('fs-extra')
4const path = require('path')
5const program = require('commander')
6const chalk = require('chalk')
7
8const { keyFileProjectConfigTemp, keyConfigQuiet, filenameBuilding } = require('../defaults/before-build')
9
10const __ = require('../utils/translate')
11// const readBuildConfigFile = require('../utils/read-build-config-file')
12const sleep = require('../utils/sleep')
13const setEnvFromCommand = require('../utils/set-env-from-command')
14const getAppType = require('../utils/get-app-type')
15const validateConfig = require('../libs/validate-config')
16const validateConfigDist = require('../libs/validate-config-dist')
17const spinner = require('../utils/spinner')
18const initNodeEnv = require('../utils/init-node-env')
19
20const kootBuild = require('../core/webpack/enter')
21
22program
23 .version(require('../package').version, '-v, --version')
24 .usage('[options]')
25 .option('-c, --client', 'Set STAGE to CLIENT')
26 .option('-s, --server', 'Set STAGE to SERVER')
27 .option('--stage <stage>', 'Set STAGE')
28 .option('--env <env>', 'Set ENV')
29 .option('--dest <destination-path>', 'Set destination directory')
30 .option('--config <config-file-path>', 'Set config file pathname')
31 .option('--type <project-type>', 'Set project type')
32 .option('--koot-dev', 'Koot dev mode')
33 .option('--koot-test', 'Koot test mode')
34 .parse(process.argv)
35
36/** 判断是否是通过 koot-start 命令启动
37 * @returns {Boolean}
38 */
39const isFromCommandStart = () => (process.env.KOOT_COMMAND_START && JSON.parse(process.env.KOOT_COMMAND_START))
40
41/**
42 * 执行打包
43 */
44const run = async () => {
45
46 const {
47 client, server,
48 stage: _stage,
49 env = 'prod',
50 config,
51 type,
52 dest,
53 kootDev = false,
54 kootTest = false,
55 } = program
56
57 initNodeEnv()
58 // console.log(program)
59
60 /** @type {Boolean} 是否为通过 koot-start 命令启动 */
61 const fromCommandStart = isFromCommandStart()
62 const fromOtherCommand = kootDev || fromCommandStart
63 if (!fromOtherCommand)
64 // 清空 log
65 process.stdout.write('\x1B[2J\x1B[0f')
66
67 setEnvFromCommand({
68 config,
69 type,
70 }, fromOtherCommand)
71
72 process.env.KOOT_TEST_MODE = JSON.stringify(kootTest)
73
74 const stage = (() => {
75 if (_stage) return _stage
76 if (client) return 'client'
77 if (server) return 'server'
78 return false
79 })()
80
81 // TODO:
82
83 // console.log(stage, env)
84
85 // if (!stage) {
86 // console.log(
87 // chalk.redBright('× ')
88 // + __('build.missing_option', {
89 // option: chalk.yellowBright('stage'),
90 // example: 'koot-build ' + chalk.green('--stage client') + ' --env prod',
91 // indent: ' '
92 // })
93 // )
94 // return
95 // }
96
97 // if (!env) {
98 // console.log(
99 // chalk.redBright('× ')
100 // + __('build.missing_option', {
101 // option: chalk.yellowBright('env'),
102 // example: 'koot-build ' + chalk.green('--env prod'),
103 // indent: ' '
104 // })
105 // )
106 // return
107 // }
108
109 // 在所有操作执行之前定义环境变量
110 process.env.WEBPACK_BUILD_STAGE = stage || 'client'
111 process.env.WEBPACK_BUILD_ENV = env
112
113 // 读取构建配置
114 const buildConfig = await validateConfig()
115 await getAppType()
116 // const buildConfig = await readBuildConfigFile()
117 // const {
118 // server: hasServer
119 // } = buildConfig
120
121 if (dest) buildConfig.dist = validateConfigDist(dest)
122
123 // 如果通过 koot-start 命令启动...
124 if (fromCommandStart) {
125 // 非报错 log 不打出
126 buildConfig[keyConfigQuiet] = true
127 }
128
129 // 如果提供了 stage,仅针对 stage 执行打包
130 // SPA: 仅打包 client
131 if (process.env.WEBPACK_BUILD_TYPE === 'spa' || stage) {
132 // if (stage === 'server' && !hasServer) {
133 // console.log(chalk.redBright('× '))
134 // }
135 await kootBuild(buildConfig)
136 await after(buildConfig)
137 if (!fromCommandStart) console.log(' ')
138 return
139 }
140
141 // 如过没有提供 stage,自动相继打包 client 和 server
142 await kootBuild({ ...buildConfig })
143 await sleep(100)
144
145 // if (!hasServer) return
146
147 if (!fromCommandStart) console.log('\n' + ''.padEnd(60, '=') + '\n')
148 process.env.WEBPACK_BUILD_STAGE = 'server'
149 await kootBuild({ ...buildConfig })
150 await sleep(100)
151
152 if (!fromCommandStart) console.log('\n' + ''.padEnd(60, '=') + '\n')
153 if (!fromCommandStart) console.log(
154 chalk.green('√ ')
155 + chalk.yellowBright('[koot/build] ')
156 + __('build.complete', {
157 time: (new Date()).toLocaleString()
158 })
159 )
160
161 await after(buildConfig)
162 if (!fromCommandStart) console.log(' ')
163}
164
165const after = async (config = {}) => {
166 const ENV = process.env.WEBPACK_BUILD_ENV
167
168 const {
169 dist,
170 [keyFileProjectConfigTemp]: fileProjectConfigTemp
171 } = config
172
173 // 移除临时配置文件
174 if (ENV === 'prod' && fileProjectConfigTemp) {
175 await fs.remove(fileProjectConfigTemp)
176 }
177
178 // 移除标记文件
179 const fileBuilding = path.resolve(dist, filenameBuilding)
180
181 if (fs.existsSync(fileBuilding))
182 await fs.remove(fileBuilding)
183}
184
185run().catch(err => {
186 if (isFromCommandStart()) {
187 // throw err
188 return console.error(err)
189 }
190 spinner(chalk.yellowBright('[koot/build] ')).fail()
191 console.trace(err)
192})