forked from zhennann/egg-bin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.js
108 lines (88 loc) · 2.99 KB
/
command.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
'use strict';
const path = require('path');
const fs = require('fs');
const BaseCommand = require('common-bin');
class Command extends BaseCommand {
constructor(rawArgv) {
super(rawArgv);
this.parserOptions = {
execArgv: true,
removeAlias: true,
};
// common-bin setter, don't care about override at sub class
// https://github.com/node-modules/common-bin/blob/master/lib/command.js#L158
this.options = {
typescript: {
description: 'whether enable typescript support, will load tscompiler on startup',
type: 'boolean',
alias: 'ts',
default: undefined,
},
declarations: {
description: 'whether create dts, will load `egg-ts-helper/register`',
type: 'boolean',
alias: 'dts',
default: undefined,
},
tscompiler: {
description: 'ts compiler, like ts-node、ts-eager、esbuild-register etc.',
type: 'string',
alias: 'tsc',
default: undefined,
},
};
}
/**
* default error handler
* @param {Error} err - err obj
*/
errorHandler(err) {
console.error(err);
process.nextTick(() => process.exit(1));
}
get context() {
const context = super.context;
const { argv, debugPort, execArgvObj, cwd, env } = context;
// compatible
if (debugPort) context.debug = debugPort;
// remove unuse args
argv.$0 = undefined;
// read package.json
let baseDir = argv.baseDir || cwd;
if (!path.isAbsolute(baseDir)) baseDir = path.join(cwd, baseDir);
const pkgFile = path.join(baseDir, 'package.json');
const pkgInfo = fs.existsSync(pkgFile) ? require(pkgFile) : null;
const eggInfo = (pkgInfo && pkgInfo.egg) || {};
execArgvObj.require = execArgvObj.require || [];
// read `egg.typescript` from package.json if not pass argv
if (argv.typescript === undefined && typeof eggInfo.typescript === 'boolean') {
argv.typescript = eggInfo.typescript;
}
// read `egg.declarations` from package.json if not pass argv
if (argv.declarations === undefined && typeof eggInfo.declarations === 'boolean') {
argv.declarations = eggInfo.declarations;
}
// read `egg.tscompiler` from package.json if not pass argv
if (argv.tscompiler === undefined) {
argv.tscompiler = eggInfo.tscompiler || 'ts-node/register';
}
// read `egg.require` from package.json
if (eggInfo.require && Array.isArray(eggInfo.require)) {
execArgvObj.require = execArgvObj.require.concat(eggInfo.require);
}
// load ts-node
if (argv.typescript) {
execArgvObj.require.push(require.resolve(argv.tscompiler));
// tell egg loader to load ts file
env.EGG_TYPESCRIPT = 'true';
// load files from tsconfig on startup
env.TS_NODE_FILES = process.env.TS_NODE_FILES || 'true';
}
// load egg-ts-helper
if (argv.declarations) {
execArgvObj.require.push(require.resolve('egg-ts-helper/register'));
}
return context;
}
}
module.exports = Command;