/
command.js
57 lines (47 loc) · 1.46 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
'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 `ts-node/register` etc',
type: 'boolean',
alias: 'ts',
},
};
}
get context() {
const context = super.context;
const { argv, debugPort, execArgvObj, cwd } = context;
// compatible
if (debugPort) context.debug = debugPort;
// remove unuse args
argv.$0 = undefined;
// read `egg.typescript` from package.json
let baseDir = argv._[0] || argv.baseDir || cwd;
if (!path.isAbsolute(baseDir)) baseDir = path.join(cwd, baseDir);
const pkgFile = path.join(baseDir, 'package.json');
if (fs.existsSync(pkgFile)) {
const pkgInfo = require(pkgFile);
if (pkgInfo && pkgInfo.egg && pkgInfo.egg.typescript) {
argv.typescript = true;
}
}
// execArgv
if (argv.typescript) {
execArgvObj.require = execArgvObj.require || [];
execArgvObj.require.push(path.join(__dirname, './ts-helper.js'));
}
return context;
}
}
module.exports = Command;