UNPKG

1.67 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 const { kootDev = false, args = [] } = options;
14 const customArgs = {};
15
16 // 清理临时目录
17 if (
18 !kootDev &&
19 typeof process.env.KOOT_PROJECT_CONFIG_FULL_PATHNAME !== 'string'
20 )
21 await fs.remove(getDirTemp());
22
23 // 定制环境变量
24 {
25 const keys = [];
26 args.forEach(arg => {
27 // if (arg === 'inspect') {
28 // process.env.__INSPECT = JSON.stringify(true);
29 // return;
30 // }
31 const segs = arg.split('=');
32 // envs[segs.shift()] = segs.join('=')
33 const key = segs.shift();
34 const value = segs.join('=');
35 if (typeof process.env[key] !== 'undefined')
36 throw new Error(
37 `Environment Variable "${key}" exists and cannot be set!`
38 );
39 keys.push(key);
40 process.env[key] = value;
41 customArgs[key] = value;
42 });
43
44 if (typeof process.env.KOOT_CUSTOM_ENV_KEYS === 'undefined') {
45 process.env.KOOT_CUSTOM_ENV_KEYS = JSON.stringify(keys);
46 } else {
47 const keysAll = JSON.parse(process.env.KOOT_CUSTOM_ENV_KEYS).concat(
48 keys
49 );
50 process.env.KOOT_CUSTOM_ENV_KEYS = JSON.stringify(keysAll);
51 }
52 }
53
54 return { customArgs };
55};