-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathbaseCommand.ts
More file actions
383 lines (353 loc) · 13.7 KB
/
baseCommand.ts
File metadata and controls
383 lines (353 loc) · 13.7 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
import { fork, type ForkOptions, ChildProcess } from 'node:child_process';
import os from 'node:os';
import path from 'node:path';
import { pathToFileURL } from 'node:url';
import { debuglog } from 'node:util';
import { importResolve } from '@eggjs/utils';
import { Command, Flags, Interfaces } from '@oclif/core';
import { type PackageEgg } from './types.ts';
import { getSourceDirname, readPackageJSON, hasTsConfig } from './utils.ts';
const debug = debuglog('egg/bin/baseCommand');
// only hook once and only when ever start any child.
const children = new Set<ChildProcess>();
let hadHook = false;
function graceful(proc: ChildProcess) {
// save child ref
children.add(proc);
// only hook once
/* c8 ignore else */
if (!hadHook) {
hadHook = true;
let signal: NodeJS.Signals;
['SIGINT', 'SIGQUIT', 'SIGTERM'].forEach((event) => {
process.once(event, () => {
signal = event as NodeJS.Signals;
process.exit(0);
});
});
process.once('exit', (code: number) => {
for (const child of children) {
debug('process exit code: %o, kill child %o with %o', code, child.pid, signal);
child.kill(signal);
}
});
}
}
export class ForkError extends Error {
code: number | null;
constructor(message: string, code: number | null) {
super(message);
this.code = code;
}
}
export interface ForkNodeOptions extends ForkOptions {
dryRun?: boolean;
}
type CustomFlags<T extends typeof Command> = Interfaces.InferredFlags<(typeof BaseCommand)['baseFlags'] & T['flags']>;
type Args<T extends typeof Command> = Interfaces.InferredArgs<T['args']>;
export abstract class BaseCommand<T extends typeof Command> extends Command {
// add the --json flag
static enableJsonFlag = false;
// define flags that can be inherited by any command that extends BaseCommand
static baseFlags = {
// 'log-level': Flags.option({
// default: 'info',
// helpGroup: 'GLOBAL',
// options: ['debug', 'warn', 'error', 'info', 'trace'] as const,
// summary: 'Specify level for logging.',
// })(),
'dry-run': Flags.boolean({
default: false,
helpGroup: 'GLOBAL',
summary: 'whether show full command script only',
char: 'd',
}),
require: Flags.string({
helpGroup: 'GLOBAL',
summary: 'require the given module',
char: 'r',
multiple: true,
}),
import: Flags.string({
helpGroup: 'GLOBAL',
summary: 'import the given module, only work on ESM',
multiple: true,
}),
base: Flags.string({
helpGroup: 'GLOBAL',
summary: 'directory of application',
aliases: ['baseDir'],
default: process.cwd(),
}),
tscompiler: Flags.string({
helpGroup: 'GLOBAL',
summary: 'TypeScript compiler, like ts-node/register',
aliases: ['tsc'],
}),
// flag with no value (--typescript)
typescript: Flags.boolean({
helpGroup: 'GLOBAL',
description: '[default: true] use TypeScript to run the test',
allowNo: true,
}),
ts: Flags.string({
helpGroup: 'GLOBAL',
description: 'shortcut for --typescript, e.g.: --ts=false',
options: ['true', 'false'],
}),
javascript: Flags.boolean({
helpGroup: 'GLOBAL',
description: 'use JavaScript to run the test',
aliases: ['js'],
}),
declarations: Flags.boolean({
helpGroup: 'GLOBAL',
description: 'deprecated, no effect, will be removed in the future',
aliases: ['dts'],
}),
// https://nodejs.org/dist/latest-v18.x/docs/api/cli.html#--inspect-brkhostport
inspect: Flags.boolean({
helpGroup: 'GLOBAL',
description: 'Activate inspector',
}),
'inspect-brk': Flags.boolean({
helpGroup: 'GLOBAL',
description: 'Activate inspector and break at start of user script',
}),
};
protected flags!: CustomFlags<T>;
protected args!: Args<T>;
protected env = { ...process.env };
protected pkg: Record<string, any>;
protected isESM: boolean;
protected pkgEgg: PackageEgg;
protected globalExecArgv: string[] = [];
public async init(): Promise<void> {
await super.init();
debug('[init] raw args: %o, NODE_ENV: %o', this.argv, this.env.NODE_ENV);
const { args, flags } = await this.parse({
flags: this.ctor.flags,
baseFlags: (super.ctor as typeof BaseCommand).baseFlags,
enableJsonFlag: this.ctor.enableJsonFlag,
args: this.ctor.args,
strict: this.ctor.strict,
});
this.flags = flags as CustomFlags<T>;
this.args = args as Args<T>;
await this.#afterInit();
}
async #afterInit() {
const { args, flags } = this;
debug('before: args: %o, flags: %o', args, flags);
if (!path.isAbsolute(flags.base)) {
flags.base = path.join(process.cwd(), flags.base);
}
const pkg = await readPackageJSON(flags.base);
this.pkg = pkg;
this.pkgEgg = pkg.egg ?? {};
flags.tscompiler = flags.tscompiler ?? this.env.TS_COMPILER ?? this.pkgEgg.tscompiler;
let typescript: boolean = flags.typescript;
// keep compatible with old ts flag: `--ts=true` or `--ts=false`
if (flags.ts === 'true') {
typescript = true;
} else if (flags.ts === 'false') {
typescript = false;
}
if (typescript === undefined) {
// try to ready EGG_TYPESCRIPT env first, only accept 'true' or 'false' string
if (this.env.EGG_TYPESCRIPT === 'false') {
typescript = false;
debug('detect typescript=%o from EGG_TYPESCRIPT=%o', false, this.env.EGG_TYPESCRIPT);
} else if (this.env.EGG_TYPESCRIPT === 'true') {
typescript = true;
debug('detect typescript=%o from EGG_TYPESCRIPT=%o', true, this.env.EGG_TYPESCRIPT);
} else if (typeof this.pkgEgg.typescript === 'boolean') {
// read `egg.typescript` from package.json if not pass argv
typescript = this.pkgEgg.typescript;
debug('detect typescript=%o from pkg.egg.typescript=%o', typescript, this.pkgEgg.typescript);
} else if (pkg.dependencies?.typescript) {
// auto detect pkg.dependencies.typescript or pkg.devDependencies.typescript
typescript = true;
debug('detect typescript=%o from pkg.dependencies.typescript=%o', true, pkg.dependencies.typescript);
} else if (pkg.devDependencies?.typescript) {
typescript = true;
debug('detect typescript=%o from pkg.devDependencies.typescript=%o', true, pkg.devDependencies.typescript);
} else if (await hasTsConfig(flags.base)) {
// tsconfig.json exists
typescript = true;
debug('detect typescript=%o cause tsconfig.json exists', true);
} else if (flags.tscompiler) {
typescript = true;
debug('detect typescript=%o from --tscompiler=%o', true, flags.tscompiler);
}
}
flags.typescript = typescript;
let rootDir = path.dirname(getSourceDirname());
if (path.basename(rootDir) === 'dist') {
rootDir = path.dirname(rootDir);
}
// try app baseDir first on custom tscompiler
// then try to find tscompiler in @eggjs/bin/node_modules
const findPaths: string[] = [flags.base, rootDir];
this.isESM = pkg.type === 'module';
if (typescript) {
flags.tscompiler = flags.tscompiler ?? 'ts-node/register';
const tsNodeRegister = importResolve(flags.tscompiler, {
paths: findPaths,
});
flags.tscompiler = tsNodeRegister;
// should require tsNodeRegister on current process, let it can require *.ts files
// e.g.: dev command will execute egg loader to find configs and plugins
// await importModule(tsNodeRegister);
// let child process auto require ts-node too
this.addNodeOptions(this.formatImportModule(tsNodeRegister));
// tell egg loader to load ts file
// see https://github.com/eggjs/egg-core/blob/master/lib/loader/egg_loader.js#L443
this.env.EGG_TYPESCRIPT = 'true';
// set current process.env.EGG_TYPESCRIPT too
process.env.EGG_TYPESCRIPT = 'true';
// load files from tsconfig on startup
this.env.TS_NODE_FILES = process.env.TS_NODE_FILES ?? 'true';
// keep same logic with egg-core, test cmd load files need it
// see https://github.com/eggjs/egg-core/blob/master/lib/loader/egg_loader.js#L49
const tsConfigPathsRegister = importResolve('tsconfig-paths/register', {
paths: findPaths,
});
this.addNodeOptions(this.formatImportModule(tsConfigPathsRegister));
}
if (this.isESM) {
// use ts-node/esm loader on esm
let esmLoader = importResolve('ts-node/esm', {
paths: findPaths,
});
// ES Module loading with absolute path fails on windows
// https://github.com/nodejs/node/issues/31710#issuecomment-583916239
// https://nodejs.org/api/url.html#url_url_pathtofileurl_path
// Error [ERR_UNSUPPORTED_ESM_URL_SCHEME]: Only URLs with a scheme in: file, data, and node are supported by the default ESM loader. On Windows, absolute paths must be valid file:// URLs. Received protocol 'd:'
esmLoader = pathToFileURL(esmLoader).href;
// wait for https://github.com/nodejs/node/issues/40940
this.addNodeOptions('--no-warnings');
this.addNodeOptions(`--loader ${esmLoader}`);
}
if (this.pkgEgg.revert) {
const reverts = Array.isArray(this.pkgEgg.revert) ? this.pkgEgg.revert : [this.pkgEgg.revert];
for (const revert of reverts) {
this.globalExecArgv.push(`--security-revert=${revert}`);
}
}
let hasInspectOption = false;
if (flags.inspect) {
this.addNodeOptions('--inspect');
hasInspectOption = true;
}
if (flags['inspect-brk']) {
this.addNodeOptions('--inspect-brk');
hasInspectOption = true;
}
if (hasInspectOption) {
Reflect.set(flags, 'timeout', 0);
debug('set timeout = 0 when inspect enable');
} else if (this.env.JB_DEBUG_FILE) {
// others like WebStorm 2019 will pass NODE_OPTIONS, and @eggjs/bin itself will be debug, so could detect `process.env.JB_DEBUG_FILE`.
Reflect.set(flags, 'timeout', 0);
debug('set timeout = false when process.env.JB_DEBUG_FILE=%o', this.env.JB_DEBUG_FILE);
}
debug('baseDir: %o, isESM: %o', flags.base, this.isESM);
debug('set NODE_OPTIONS: %o', this.env.NODE_OPTIONS);
debug('after: args: %o, flags: %o', args, flags);
debug('enter real command: %o', this.id);
}
protected async catch(err: Error & { exitCode?: number }): Promise<any> {
// add any custom logic to handle errors from the command
// or simply return the parent class error handling
return super.catch(err);
}
protected async finally(_: Error | undefined): Promise<any> {
// called after run and catch regardless of whether or not the command errored
return super.finally(_);
}
protected async formatRequires(): Promise<string[]> {
const requires = this.flags.require ?? [];
const imports = this.flags.import ?? [];
let eggRequires = (this.pkgEgg.require as string[]) ?? [];
if (typeof eggRequires === 'string') {
eggRequires = [eggRequires];
}
let eggImports = (this.pkgEgg.import as string[]) ?? [];
if (typeof eggImports === 'string') {
eggImports = [eggImports];
}
return [...requires, ...imports, ...eggRequires, ...eggImports];
}
protected formatImportModule(modulePath: string) {
if (this.isESM) {
return `--import "${pathToFileURL(modulePath).href}"`;
}
if (os.platform() === 'win32') {
// windows path need to escape backslash: `node --require "C:\\path\\to\\module"`
return `--require "${path.win32.normalize(modulePath).replace(/\\/g, '\\\\')}"`;
}
return `--require "${modulePath}"`;
}
protected addNodeOptions(options: string) {
if (this.env.NODE_OPTIONS) {
if (!this.env.NODE_OPTIONS.includes(options)) {
this.env.NODE_OPTIONS = `${this.env.NODE_OPTIONS} ${options}`;
}
} else {
this.env.NODE_OPTIONS = options;
}
}
protected async buildRequiresExecArgv(): Promise<string[]> {
const requires = await this.formatRequires();
const execArgv: string[] = [];
for (const r of requires) {
const module = this.formatImportModule(r);
// Remove the quotes from the path
// --require "module path" -> ['--require', 'module path']
// --import "module path" -> ['--import', 'module path']
const splitIndex = module.indexOf(' ');
if (splitIndex !== -1) {
execArgv.push(module.slice(0, splitIndex), module.slice(splitIndex + 2, -1));
}
}
return execArgv;
}
protected async forkNode(modulePath: string, forkArgs: string[], options: ForkNodeOptions = {}) {
const env = {
...this.env,
...options.env,
};
const forkExecArgv = [...this.globalExecArgv, ...(options.execArgv || [])];
const NODE_OPTIONS = env.NODE_OPTIONS ? `NODE_OPTIONS='${env.NODE_OPTIONS}' ` : '';
const forkExecArgvString = forkExecArgv.length ? ' ' + forkExecArgv.join(' ') + ' ' : ' ';
const forkArgsString = forkArgs.map((a) => `'${a}'`).join(' ');
const fullCommand = `${NODE_OPTIONS}${process.execPath}${forkExecArgvString}${modulePath} ${forkArgsString}`;
if (options.dryRun) {
console.log('dry run: $ %s', fullCommand);
return;
}
options = {
stdio: 'inherit',
env,
cwd: this.flags.base,
...options,
execArgv: forkExecArgv,
};
const proc = fork(modulePath, forkArgs, options);
debug('Run fork pid: %o\n\n$ %s\n\n', proc.pid, fullCommand);
graceful(proc);
return new Promise<void>((resolve, reject) => {
proc.once('exit', (code) => {
debug('fork pid: %o exit code %o', proc.pid, code);
children.delete(proc);
if (code !== 0) {
const err = new ForkError(modulePath + ' ' + forkArgs.join(' ') + ' exit with code ' + code, code);
reject(err);
} else {
resolve();
}
});
});
}
}