UNPKG

4.12 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3const fs = require('fs-extra')
4const program = require('commander')
5const chalk = require('chalk')
6
7const { keyFileProjectConfigTemp } = require('../defaults/before-build')
8
9const __ = require('../utils/translate')
10// const readBuildConfigFile = require('../utils/read-build-config-file')
11const sleep = require('../utils/sleep')
12const setEnvFromCommand = require('../utils/set-env-from-command')
13const validateConfig = require('../libs/validate-config')
14
15const kootBuild = require('../core/webpack/enter')
16
17program
18 .version(require('../package').version, '-v, --version')
19 .usage('[options]')
20 .option('-c, --client', 'Set STAGE to CLIENT')
21 .option('-s, --server', 'Set STAGE to SERVER')
22 .option('--stage <stage>', 'Set STAGE')
23 .option('--env <env>', 'Set ENV')
24 .option('--dest <destination-path>', 'Set destination directory')
25 .option('--config <config-file-path>', 'Set config file pathname')
26 .option('--type <project-type>', 'Set project type')
27 .option('--koot-test', 'Koot test mode')
28 .parse(process.argv)
29
30/**
31 * 执行打包
32 */
33const run = async () => {
34
35 // 清空 log
36 process.stdout.write('\x1B[2J\x1B[0f')
37
38 const {
39 client, server,
40 stage: _stage,
41 env = 'prod',
42 config,
43 type,
44 dest,
45 kootTest = false
46 } = program
47 // console.log(program)
48
49 setEnvFromCommand({
50 config,
51 type,
52 })
53
54 process.env.KOOT_TEST_MODE = JSON.stringify(kootTest)
55
56 const stage = _stage ? _stage : (client ? 'client' : (server ? 'server' : false))
57
58 // TODO:
59
60 // console.log(stage, env)
61
62 // if (!stage) {
63 // console.log(
64 // chalk.redBright('× ')
65 // + __('build.missing_option', {
66 // option: chalk.yellowBright('stage'),
67 // example: 'koot-build ' + chalk.green('--stage client') + ' --env prod',
68 // indent: ' '
69 // })
70 // )
71 // return
72 // }
73
74 // if (!env) {
75 // console.log(
76 // chalk.redBright('× ')
77 // + __('build.missing_option', {
78 // option: chalk.yellowBright('env'),
79 // example: 'koot-build ' + chalk.green('--env prod'),
80 // indent: ' '
81 // })
82 // )
83 // return
84 // }
85
86 // 在所有操作执行之前定义环境变量
87 process.env.WEBPACK_BUILD_STAGE = stage || 'client'
88 process.env.WEBPACK_BUILD_ENV = env
89
90 // 读取构建配置
91 const buildConfig = await validateConfig()
92 // const buildConfig = await readBuildConfigFile()
93 // const {
94 // server: hasServer
95 // } = buildConfig
96
97 if (dest) buildConfig.dist = dest
98
99 // 如果提供了 stage,仅针对 stage 执行打包
100 if (stage) {
101 // if (stage === 'server' && !hasServer) {
102 // console.log(chalk.redBright('× '))
103 // }
104 await kootBuild(buildConfig)
105 await after(buildConfig)
106 console.log(' ')
107 return
108 }
109
110 // 如过没有提供 stage,自动相继打包 client 和 server
111 await kootBuild({ ...buildConfig })
112 await sleep(100)
113
114 // if (!hasServer) return
115
116 console.log('\n' + ''.padEnd(60, '=') + '\n')
117 process.env.WEBPACK_BUILD_STAGE = 'server'
118 await kootBuild({ ...buildConfig })
119 await sleep(100)
120
121 console.log('\n' + ''.padEnd(60, '=') + '\n')
122 console.log(
123 chalk.green('√ ')
124 + chalk.yellowBright('[koot/build] ')
125 + __('build.complete', {
126 time: (new Date()).toLocaleString()
127 })
128 )
129
130 await after(buildConfig)
131 console.log(' ')
132}
133
134const after = async (config = {}) => {
135 const ENV = process.env.WEBPACK_BUILD_ENV
136
137 const {
138 [keyFileProjectConfigTemp]: fileProjectConfigTemp
139 } = config
140
141 // 移除临时配置文件
142 if (ENV === 'prod' && fileProjectConfigTemp) {
143 await fs.remove(fileProjectConfigTemp)
144 }
145}
146
147run()