1 | 'use strict';
|
2 |
|
3 | const path = require('path');
|
4 | const fs = require('fs');
|
5 | const BaseCommand = require('common-bin');
|
6 |
|
7 | class Command extends BaseCommand {
|
8 | constructor(rawArgv) {
|
9 | super(rawArgv);
|
10 | this.parserOptions = {
|
11 | execArgv: true,
|
12 | removeAlias: true,
|
13 | };
|
14 |
|
15 |
|
16 |
|
17 | this.options = {
|
18 | typescript: {
|
19 | description: 'whether enable typescript support, will load `ts-node/register` etc',
|
20 | type: 'boolean',
|
21 | alias: 'ts',
|
22 | default: undefined,
|
23 | },
|
24 | };
|
25 | }
|
26 |
|
27 | get context() {
|
28 | const context = super.context;
|
29 | const { argv, debugPort, execArgvObj, cwd, env } = context;
|
30 |
|
31 |
|
32 | if (debugPort) context.debug = debugPort;
|
33 |
|
34 |
|
35 | argv.$0 = undefined;
|
36 |
|
37 |
|
38 | if (argv.typescript === undefined) {
|
39 | let baseDir = argv._[0] || argv.baseDir || cwd;
|
40 | if (!path.isAbsolute(baseDir)) baseDir = path.join(cwd, baseDir);
|
41 | const pkgFile = path.join(baseDir, 'package.json');
|
42 | if (fs.existsSync(pkgFile)) {
|
43 | const pkgInfo = require(pkgFile);
|
44 | if (pkgInfo && pkgInfo.egg && pkgInfo.egg.typescript === true) {
|
45 | argv.typescript = true;
|
46 | }
|
47 | }
|
48 | }
|
49 |
|
50 |
|
51 | if (argv.typescript) {
|
52 | execArgvObj.require = execArgvObj.require || [];
|
53 | execArgvObj.require.push(require.resolve('ts-node/register'));
|
54 |
|
55 |
|
56 | env.EGG_TYPESCRIPT = 'true';
|
57 |
|
58 |
|
59 | env.TS_NODE_TYPE_CHECK = process.env.TS_NODE_TYPE_CHECK || 'true';
|
60 |
|
61 |
|
62 | env.TS_NODE_FILES = process.env.TS_NODE_FILES || 'true';
|
63 | }
|
64 |
|
65 | return context;
|
66 | }
|
67 | }
|
68 |
|
69 | module.exports = Command;
|