UNPKG

1.44 kBJavaScriptView Raw
1const fs = require('fs-extra')
2
3const getDirTemp = require('../libs/get-dir-tmp')
4
5/**
6 * 所有命令启动前
7 * @async
8 * @param {Object} [options={}]
9 * @param {Boolean} [options.kootDev=false]
10 * @void
11 */
12module.exports = async (options = {}) => {
13
14 const {
15 kootDev = false,
16 args = []
17 } = options
18 const customArgs = {}
19
20 // 清理临时目录
21 if (!kootDev && typeof process.env.KOOT_PROJECT_CONFIG_FULL_PATHNAME !== 'string')
22 await fs.remove(getDirTemp())
23
24 // 定制环境变量
25 {
26 const keys = []
27 args.forEach(arg => {
28 const segs = arg.split('=')
29 // envs[segs.shift()] = segs.join('=')
30 const key = segs.shift()
31 const value = segs.join('=')
32 if (typeof process.env[key] !== 'undefined')
33 throw new Error(`Environment Variable "${key}" exists and cannot be set!`)
34 keys.push(key)
35 process.env[key] = value
36 customArgs[key] = value
37 })
38
39 if (typeof process.env.KOOT_CUSTOM_ENV_KEYS === 'undefined') {
40 process.env.KOOT_CUSTOM_ENV_KEYS = JSON.stringify(keys)
41 } else {
42 const keysAll = JSON.parse(process.env.KOOT_CUSTOM_ENV_KEYS)
43 .concat(keys)
44 process.env.KOOT_CUSTOM_ENV_KEYS = JSON.stringify(keysAll)
45 }
46 }
47
48 return { customArgs }
49}