@eggjs/bin 7.3.0 → 7.3.1-beta.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. package/README.md +3 -21
  2. package/bin/dev.js +0 -0
  3. package/dist/baseCommand.d.ts +52 -0
  4. package/dist/baseCommand.js +288 -0
  5. package/dist/commands/cov.d.ts +27 -0
  6. package/dist/commands/cov.js +86 -0
  7. package/dist/commands/dev.d.ts +26 -0
  8. package/dist/commands/dev.js +86 -0
  9. package/dist/commands/test.d.ts +28 -0
  10. package/dist/commands/test.js +168 -0
  11. package/dist/index.d.ts +7 -0
  12. package/dist/index.js +8 -0
  13. package/dist/scripts/postinstall.mjs +14 -7
  14. package/dist/scripts/start-cluster.cjs +3 -2
  15. package/dist/scripts/start-cluster.mjs +3 -2
  16. package/{src/types.ts → dist/types.d.ts} +4 -1
  17. package/dist/utils.js +34 -0
  18. package/package.json +53 -73
  19. package/scripts/postinstall.mjs +14 -7
  20. package/scripts/start-cluster.cjs +3 -2
  21. package/scripts/start-cluster.mjs +3 -2
  22. package/dist/commonjs/baseCommand.d.ts +0 -49
  23. package/dist/commonjs/baseCommand.js +0 -369
  24. package/dist/commonjs/commands/cov.d.ts +0 -22
  25. package/dist/commonjs/commands/cov.js +0 -98
  26. package/dist/commonjs/commands/dev.d.ts +0 -21
  27. package/dist/commonjs/commands/dev.js +0 -95
  28. package/dist/commonjs/commands/test.d.ts +0 -23
  29. package/dist/commonjs/commands/test.js +0 -222
  30. package/dist/commonjs/index.d.ts +0 -7
  31. package/dist/commonjs/index.js +0 -30
  32. package/dist/commonjs/package.json +0 -3
  33. package/dist/commonjs/types.d.ts +0 -9
  34. package/dist/commonjs/types.js +0 -3
  35. package/dist/commonjs/utils.d.ts +0 -4
  36. package/dist/commonjs/utils.js +0 -45
  37. package/dist/esm/baseCommand.d.ts +0 -49
  38. package/dist/esm/baseCommand.js +0 -361
  39. package/dist/esm/commands/cov.d.ts +0 -22
  40. package/dist/esm/commands/cov.js +0 -92
  41. package/dist/esm/commands/dev.d.ts +0 -21
  42. package/dist/esm/commands/dev.js +0 -92
  43. package/dist/esm/commands/test.d.ts +0 -23
  44. package/dist/esm/commands/test.js +0 -216
  45. package/dist/esm/index.d.ts +0 -7
  46. package/dist/esm/index.js +0 -8
  47. package/dist/esm/package.json +0 -3
  48. package/dist/esm/types.d.ts +0 -9
  49. package/dist/esm/types.js +0 -2
  50. package/dist/esm/utils.d.ts +0 -4
  51. package/dist/esm/utils.js +0 -36
  52. package/dist/package.json +0 -4
  53. package/src/baseCommand.ts +0 -390
  54. package/src/commands/cov.ts +0 -101
  55. package/src/commands/dev.ts +0 -99
  56. package/src/commands/test.ts +0 -236
  57. package/src/index.ts +0 -9
  58. package/src/utils.ts +0 -37
@@ -1,390 +0,0 @@
1
- import { debuglog } from 'node:util';
2
- import { pathToFileURL } from 'node:url';
3
- import path from 'node:path';
4
- import { fork, ForkOptions, ChildProcess } from 'node:child_process';
5
- import { Command, Flags, Interfaces } from '@oclif/core';
6
- import { importResolve } from '@eggjs/utils';
7
- import { runScript } from 'runscript';
8
- import {
9
- getSourceDirname,
10
- readPackageJSON, hasTsConfig,
11
- } from './utils.js';
12
- import { PackageEgg } from './types.js';
13
-
14
- const debug = debuglog('@eggjs/bin/baseCommand');
15
-
16
- // only hook once and only when ever start any child.
17
- const children = new Set<ChildProcess>();
18
- let hadHook = false;
19
- function graceful(proc: ChildProcess) {
20
- // save child ref
21
- children.add(proc);
22
-
23
- // only hook once
24
- /* c8 ignore else */
25
- if (!hadHook) {
26
- hadHook = true;
27
- let signal: NodeJS.Signals;
28
- [ 'SIGINT', 'SIGQUIT', 'SIGTERM' ].forEach(event => {
29
- process.once(event, () => {
30
- signal = event as NodeJS.Signals;
31
- process.exit(0);
32
- });
33
- });
34
-
35
- process.once('exit', (code: number) => {
36
- for (const child of children) {
37
- debug('process exit code: %o, kill child %o with %o', code, child.pid, signal);
38
- child.kill(signal);
39
- }
40
- });
41
- }
42
- }
43
-
44
- export class ForkError extends Error {
45
- code: number | null;
46
- constructor(message: string, code: number | null) {
47
- super(message);
48
- this.code = code;
49
- }
50
- }
51
-
52
- export interface ForkNodeOptions extends ForkOptions {
53
- dryRun?: boolean;
54
- }
55
-
56
- type Flags<T extends typeof Command> = Interfaces.InferredFlags<typeof BaseCommand['baseFlags'] & T['flags']>;
57
- type Args<T extends typeof Command> = Interfaces.InferredArgs<T['args']>;
58
-
59
- export abstract class BaseCommand<T extends typeof Command> extends Command {
60
- // add the --json flag
61
- static enableJsonFlag = false;
62
-
63
- // define flags that can be inherited by any command that extends BaseCommand
64
- static baseFlags = {
65
- // 'log-level': Flags.option({
66
- // default: 'info',
67
- // helpGroup: 'GLOBAL',
68
- // options: ['debug', 'warn', 'error', 'info', 'trace'] as const,
69
- // summary: 'Specify level for logging.',
70
- // })(),
71
- 'dry-run': Flags.boolean({
72
- default: false,
73
- helpGroup: 'GLOBAL',
74
- summary: 'whether show full command script only',
75
- char: 'd',
76
- }),
77
- require: Flags.string({
78
- helpGroup: 'GLOBAL',
79
- summary: 'require the given module',
80
- char: 'r',
81
- multiple: true,
82
- }),
83
- import: Flags.string({
84
- helpGroup: 'GLOBAL',
85
- summary: 'import the given module, only work on ESM',
86
- multiple: true,
87
- }),
88
- base: Flags.string({
89
- helpGroup: 'GLOBAL',
90
- summary: 'directory of application',
91
- aliases: [ 'baseDir' ],
92
- default: process.cwd(),
93
- }),
94
- tscompiler: Flags.string({
95
- helpGroup: 'GLOBAL',
96
- summary: 'TypeScript compiler, like ts-node/register',
97
- aliases: [ 'tsc' ],
98
- }),
99
- // flag with no value (--typescript)
100
- typescript: Flags.boolean({
101
- helpGroup: 'GLOBAL',
102
- description: '[default: true] use TypeScript to run the test',
103
- allowNo: true,
104
- }),
105
- ts: Flags.string({
106
- helpGroup: 'GLOBAL',
107
- description: 'shortcut for --typescript, e.g.: --ts=false',
108
- options: [ 'true', 'false' ],
109
- }),
110
- javascript: Flags.boolean({
111
- helpGroup: 'GLOBAL',
112
- description: 'use JavaScript to run the test',
113
- aliases: [ 'js' ],
114
- }),
115
- declarations: Flags.boolean({
116
- helpGroup: 'GLOBAL',
117
- description: 'whether create typings, will add `--require egg-ts-helper/register`',
118
- aliases: [ 'dts' ],
119
- }),
120
- // https://nodejs.org/dist/latest-v18.x/docs/api/cli.html#--inspect-brkhostport
121
- inspect: Flags.boolean({
122
- helpGroup: 'GLOBAL',
123
- description: 'Activate inspector',
124
- }),
125
- 'inspect-brk': Flags.boolean({
126
- helpGroup: 'GLOBAL',
127
- description: 'Activate inspector and break at start of user script',
128
- }),
129
- };
130
-
131
- protected flags!: Flags<T>;
132
- protected args!: Args<T>;
133
-
134
- protected env = { ...process.env };
135
- protected pkg: Record<string, any>;
136
- protected isESM: boolean;
137
- protected pkgEgg: PackageEgg;
138
- protected globalExecArgv: string[] = [];
139
-
140
- public async init(): Promise<void> {
141
- await super.init();
142
- debug('[init] raw args: %o, NODE_ENV: %o', this.argv, this.env.NODE_ENV);
143
- const { args, flags } = await this.parse({
144
- flags: this.ctor.flags,
145
- baseFlags: (super.ctor as typeof BaseCommand).baseFlags,
146
- enableJsonFlag: this.ctor.enableJsonFlag,
147
- args: this.ctor.args,
148
- strict: this.ctor.strict,
149
- });
150
- this.flags = flags as Flags<T>;
151
- this.args = args as Args<T>;
152
-
153
- await this.#afterInit();
154
- }
155
-
156
- async #afterInit() {
157
- const { args, flags } = this;
158
- debug('before: args: %o, flags: %o', args, flags);
159
- if (!path.isAbsolute(flags.base)) {
160
- flags.base = path.join(process.cwd(), flags.base);
161
- }
162
- const pkg = await readPackageJSON(flags.base);
163
- this.pkg = pkg;
164
- this.pkgEgg = pkg.egg ?? {};
165
- flags.tscompiler = flags.tscompiler ?? this.env.TS_COMPILER ?? this.pkgEgg.tscompiler;
166
-
167
- let typescript: boolean = flags.typescript;
168
- // keep compatible with old ts flag: `--ts=true` or `--ts=false`
169
- if (flags.ts === 'true') {
170
- typescript = true;
171
- } else if (flags.ts === 'false') {
172
- typescript = false;
173
- }
174
-
175
- if (typescript === undefined) {
176
- // try to ready EGG_TYPESCRIPT env first, only accept 'true' or 'false' string
177
- if (this.env.EGG_TYPESCRIPT === 'false') {
178
- typescript = false;
179
- debug('detect typescript=%o from EGG_TYPESCRIPT=%o', false, this.env.EGG_TYPESCRIPT);
180
- } else if (this.env.EGG_TYPESCRIPT === 'true') {
181
- typescript = true;
182
- debug('detect typescript=%o from EGG_TYPESCRIPT=%o', true, this.env.EGG_TYPESCRIPT);
183
- } else if (typeof this.pkgEgg.typescript === 'boolean') {
184
- // read `egg.typescript` from package.json if not pass argv
185
- typescript = this.pkgEgg.typescript;
186
- debug('detect typescript=%o from pkg.egg.typescript=%o', typescript, this.pkgEgg.typescript);
187
- } else if (pkg.dependencies?.typescript) {
188
- // auto detect pkg.dependencies.typescript or pkg.devDependencies.typescript
189
- typescript = true;
190
- debug('detect typescript=%o from pkg.dependencies.typescript=%o', true, pkg.dependencies.typescript);
191
- } else if (pkg.devDependencies?.typescript) {
192
- typescript = true;
193
- debug('detect typescript=%o from pkg.devDependencies.typescript=%o', true, pkg.devDependencies.typescript);
194
- } else if (await hasTsConfig(flags.base)) {
195
- // tsconfig.json exists
196
- typescript = true;
197
- debug('detect typescript=%o cause tsconfig.json exists', true);
198
- } else if (flags.tscompiler) {
199
- typescript = true;
200
- debug('detect typescript=%o from --tscompiler=%o', true, flags.tscompiler);
201
- }
202
- }
203
- flags.typescript = typescript;
204
- let rootDir = path.dirname(getSourceDirname());
205
- if (path.basename(rootDir) === 'dist') {
206
- rootDir = path.dirname(rootDir);
207
- }
208
- // try app baseDir first on custom tscompiler
209
- // then try to find tscompiler in @eggjs/bin/node_modules
210
- const findPaths: string[] = [ flags.base, rootDir ];
211
- this.isESM = pkg.type === 'module';
212
- if (typescript) {
213
- flags.tscompiler = flags.tscompiler ?? 'ts-node/register';
214
- const tsNodeRegister = importResolve(flags.tscompiler, {
215
- paths: findPaths,
216
- });
217
- flags.tscompiler = tsNodeRegister;
218
- // should require tsNodeRegister on current process, let it can require *.ts files
219
- // e.g.: dev command will execute egg loader to find configs and plugins
220
- // await importModule(tsNodeRegister);
221
- // let child process auto require ts-node too
222
- this.addNodeOptions(this.formatImportModule(tsNodeRegister));
223
- // tell egg loader to load ts file
224
- // see https://github.com/eggjs/egg-core/blob/master/lib/loader/egg_loader.js#L443
225
- this.env.EGG_TYPESCRIPT = 'true';
226
- // set current process.env.EGG_TYPESCRIPT too
227
- process.env.EGG_TYPESCRIPT = 'true';
228
- // load files from tsconfig on startup
229
- this.env.TS_NODE_FILES = process.env.TS_NODE_FILES ?? 'true';
230
- // keep same logic with egg-core, test cmd load files need it
231
- // see https://github.com/eggjs/egg-core/blob/master/lib/loader/egg_loader.js#L49
232
- const tsConfigPathsRegister = importResolve('tsconfig-paths/register', {
233
- paths: findPaths,
234
- });
235
- this.addNodeOptions(this.formatImportModule(tsConfigPathsRegister));
236
- }
237
- if (this.isESM) {
238
- // use ts-node/esm loader on esm
239
- let esmLoader = importResolve('ts-node/esm', {
240
- paths: findPaths,
241
- });
242
- // ES Module loading with absolute path fails on windows
243
- // https://github.com/nodejs/node/issues/31710#issuecomment-583916239
244
- // https://nodejs.org/api/url.html#url_url_pathtofileurl_path
245
- // 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:'
246
- esmLoader = pathToFileURL(esmLoader).href;
247
- // wait for https://github.com/nodejs/node/issues/40940
248
- this.addNodeOptions('--no-warnings');
249
- this.addNodeOptions(`--loader ${esmLoader}`);
250
- }
251
-
252
- if (flags.declarations === undefined) {
253
- if (typeof this.pkgEgg.declarations === 'boolean') {
254
- // read `egg.declarations` from package.json if not pass argv
255
- flags.declarations = this.pkgEgg.declarations;
256
- debug('detect declarations from pkg.egg.declarations=%o', this.pkgEgg.declarations);
257
- }
258
- }
259
- if (flags.declarations) {
260
- const etsBin = importResolve('egg-ts-helper/dist/bin', {
261
- paths: findPaths,
262
- });
263
- debug('run ets first: %o', etsBin);
264
- await runScript(`node ${etsBin}`);
265
- }
266
-
267
- if (this.pkgEgg.revert) {
268
- const reverts = Array.isArray(this.pkgEgg.revert) ? this.pkgEgg.revert : [ this.pkgEgg.revert ];
269
- for (const revert of reverts) {
270
- this.globalExecArgv.push(`--security-revert=${revert}`);
271
- }
272
- }
273
-
274
- let hasInspectOption = false;
275
- if (flags.inspect) {
276
- this.addNodeOptions('--inspect');
277
- hasInspectOption = true;
278
- }
279
- if (flags['inspect-brk']) {
280
- this.addNodeOptions('--inspect-brk');
281
- hasInspectOption = true;
282
- }
283
- if (hasInspectOption) {
284
- Reflect.set(flags, 'timeout', 0);
285
- debug('set timeout = 0 when inspect enable');
286
- } else if (this.env.JB_DEBUG_FILE) {
287
- // others like WebStorm 2019 will pass NODE_OPTIONS, and @eggjs/bin itself will be debug, so could detect `process.env.JB_DEBUG_FILE`.
288
- Reflect.set(flags, 'timeout', 0);
289
- debug('set timeout = false when process.env.JB_DEBUG_FILE=%o', this.env.JB_DEBUG_FILE);
290
- }
291
-
292
- debug('baseDir: %o, isESM: %o', flags.base, this.isESM);
293
- debug('set NODE_OPTIONS: %o', this.env.NODE_OPTIONS);
294
- debug('after: args: %o, flags: %o', args, flags);
295
- debug('enter real command: %o', this.id);
296
- }
297
-
298
- protected async catch(err: Error & {exitCode?: number}): Promise<any> {
299
- // add any custom logic to handle errors from the command
300
- // or simply return the parent class error handling
301
- return super.catch(err);
302
- }
303
-
304
- protected async finally(_: Error | undefined): Promise<any> {
305
- // called after run and catch regardless of whether or not the command errored
306
- return super.finally(_);
307
- }
308
-
309
- protected async formatRequires(): Promise<string[]> {
310
- const requires = this.flags.require ?? [];
311
- const imports = this.flags.import ?? [];
312
- let eggRequires = this.pkgEgg.require as string[] ?? [];
313
- if (typeof eggRequires === 'string') {
314
- eggRequires = [ eggRequires ];
315
- }
316
- let eggImports = this.pkgEgg.import as string[] ?? [];
317
- if (typeof eggImports === 'string') {
318
- eggImports = [ eggImports ];
319
- }
320
- return [
321
- ...requires,
322
- ...imports,
323
- ...eggRequires,
324
- ...eggImports,
325
- ];
326
- }
327
-
328
- protected formatImportModule(modulePath: string) {
329
- if (this.isESM) {
330
- return `--import ${pathToFileURL(modulePath).href}`;
331
- }
332
- return `--require ${modulePath}`;
333
- }
334
-
335
- protected addNodeOptions(options: string) {
336
- if (this.env.NODE_OPTIONS) {
337
- if (!this.env.NODE_OPTIONS.includes(options)) {
338
- this.env.NODE_OPTIONS = `${this.env.NODE_OPTIONS} ${options}`;
339
- }
340
- } else {
341
- this.env.NODE_OPTIONS = options;
342
- }
343
- }
344
-
345
- protected async forkNode(modulePath: string, forkArgs: string[], options: ForkNodeOptions = {}) {
346
- const env = {
347
- ...this.env,
348
- ...options.env,
349
- };
350
- const forkExecArgv = [
351
- ...this.globalExecArgv,
352
- ...options.execArgv || [],
353
- ];
354
- const NODE_OPTIONS = env.NODE_OPTIONS ? `NODE_OPTIONS='${env.NODE_OPTIONS}' ` : '';
355
- const forkExecArgvString = forkExecArgv.length ? ' ' + forkExecArgv.join(' ') + ' ' : ' ';
356
- const forkArgsString = forkArgs.map(a => `'${a}'`).join(' ');
357
- const fullCommand = `${NODE_OPTIONS}${process.execPath}${forkExecArgvString}${modulePath} ${forkArgsString}`;
358
- if (options.dryRun) {
359
- console.log('dry run: $ %s', fullCommand);
360
- return;
361
- }
362
-
363
- options = {
364
- stdio: 'inherit',
365
- env,
366
- cwd: this.flags.base,
367
- ...options,
368
- execArgv: forkExecArgv,
369
- };
370
- const proc = fork(modulePath, forkArgs, options);
371
- debug('Run fork pid: %o\n\n$ %s\n\n',
372
- proc.pid,
373
- fullCommand);
374
- graceful(proc);
375
-
376
- return new Promise<void>((resolve, reject) => {
377
- proc.once('exit', code => {
378
- debug('fork pid: %o exit code %o', proc.pid, code);
379
- children.delete(proc);
380
- if (code !== 0) {
381
- const err = new ForkError(modulePath + ' ' + forkArgs.join(' ') + ' exit with code ' + code, code);
382
- reject(err);
383
- } else {
384
- resolve();
385
- }
386
- });
387
- });
388
- }
389
- }
390
-
@@ -1,101 +0,0 @@
1
- import path from 'node:path';
2
- import fs from 'node:fs/promises';
3
- import { Flags } from '@oclif/core';
4
- import Test from './test.js';
5
- import { importResolve } from '@eggjs/utils';
6
- import { ForkNodeOptions } from '../baseCommand.js';
7
-
8
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
9
- // @ts-ignore
10
- export default class Cov<T extends typeof Cov> extends Test<T> {
11
- static override description = 'Run the test with coverage';
12
-
13
- static override examples = [
14
- '<%= config.bin %> <%= command.id %>',
15
- '<%= config.bin %> <%= command.id %> test/index.test.ts',
16
- '<%= config.bin %> <%= command.id %> test/index.test.ts,test/user.test.ts,...',
17
- ];
18
-
19
- static override flags = {
20
- ...Test.flags,
21
- // will use on egg-mock https://github.com/eggjs/egg-mock/blob/84a64bd19d0569ec94664c898fb1b28367b95d60/index.js#L7
22
- prerequire: Flags.boolean({
23
- description: 'prerequire files for coverage instrument',
24
- }),
25
- exclude: Flags.string({
26
- description: 'coverage ignore, one or more files patterns`',
27
- multiple: true,
28
- char: 'x',
29
- }),
30
- c8: Flags.string({
31
- description: 'c8 instruments passthrough`',
32
- default: '--temp-directory node_modules/.c8_output -r text-summary -r json-summary -r json -r lcov -r cobertura',
33
- }),
34
- };
35
-
36
- protected get defaultExcludes() {
37
- return [
38
- 'example/',
39
- 'examples/',
40
- 'mocks**/',
41
- 'docs/',
42
- // https://github.com/JaKXz/test-exclude/blob/620a7be412d4fc2070d50f0f63e3228314066fc9/index.js#L73
43
- 'test/**',
44
- 'test{,-*}.js',
45
- '**/*.test.js',
46
- '**/__tests__/**',
47
- '**/node_modules/**',
48
- 'typings',
49
- '**/*.d.ts',
50
- ];
51
- }
52
-
53
- protected override async forkNode(modulePath: string, forkArgs: string[], options: ForkNodeOptions = {}) {
54
- const { flags } = this;
55
- if (flags.prerequire) {
56
- this.env.EGG_BIN_PREREQUIRE = 'true';
57
- }
58
-
59
- // add c8 args
60
- // https://github.com/eggjs/egg/issues/3930
61
- const c8Args = [
62
- // '--show-process-tree',
63
- ...flags.c8.split(' ').filter(a => a.trim()),
64
- ];
65
- if (flags.typescript) {
66
- this.env.SPAWN_WRAP_SHIM_ROOT = path.join(flags.base, 'node_modules');
67
- c8Args.push('--extension');
68
- c8Args.push('.ts');
69
- }
70
-
71
- const excludes = new Set([
72
- ...process.env.COV_EXCLUDES?.split(',') ?? [],
73
- ...this.defaultExcludes,
74
- ...Array.from(flags.exclude ?? []),
75
- ]);
76
- for (const exclude of excludes) {
77
- c8Args.push('-x');
78
- c8Args.push(exclude);
79
- }
80
- const c8File = importResolve('c8/bin/c8.js');
81
- const outputDir = path.join(flags.base, 'node_modules/.c8_output');
82
- await fs.rm(outputDir, { force: true, recursive: true });
83
- const coverageDir = path.join(flags.base, 'coverage');
84
- await fs.rm(coverageDir, { force: true, recursive: true });
85
-
86
- const execArgv = [
87
- ...this.globalExecArgv,
88
- ...options.execArgv || [],
89
- ];
90
- this.globalExecArgv = [];
91
-
92
- // $ c8 node mocha
93
- await super.forkNode(c8File, [
94
- ...c8Args,
95
- process.execPath,
96
- ...execArgv,
97
- modulePath,
98
- ...forkArgs,
99
- ]);
100
- }
101
- }
@@ -1,99 +0,0 @@
1
- import { debuglog } from 'node:util';
2
- import { Flags } from '@oclif/core';
3
- import { getConfig, getFrameworkPath } from '@eggjs/utils';
4
- import { detect } from 'detect-port';
5
- import { getSourceFilename } from '../utils.js';
6
- import { BaseCommand } from '../baseCommand.js';
7
-
8
- const debug = debuglog('@eggjs/bin/commands/dev');
9
-
10
- export default class Dev<T extends typeof Dev> extends BaseCommand<T> {
11
- static override description = 'Start server at local dev mode';
12
-
13
- static override examples = [
14
- '<%= config.bin %> <%= command.id %>',
15
- ];
16
-
17
- static override flags = {
18
- port: Flags.integer({
19
- description: 'listening port, default to 7001',
20
- char: 'p',
21
- }),
22
- workers: Flags.integer({
23
- char: 'c',
24
- aliases: [ 'cluster' ],
25
- description: 'numbers of app workers',
26
- default: 1,
27
- }),
28
- framework: Flags.string({
29
- description: 'specify framework that can be absolute path or npm package, default is "egg"',
30
- }),
31
- sticky: Flags.boolean({
32
- description: 'start a sticky cluster server',
33
- }),
34
- };
35
-
36
- public async run(): Promise<void> {
37
- debug('NODE_ENV: %o', this.env);
38
- this.env.NODE_ENV = this.env.NODE_ENV ?? 'development';
39
- this.env.EGG_MASTER_CLOSE_TIMEOUT = '1000';
40
- const ext = this.isESM ? 'mjs' : 'cjs';
41
- const serverBin = getSourceFilename(`../scripts/start-cluster.${ext}`);
42
- const eggStartOptions = await this.formatEggStartOptions();
43
- const args = [ JSON.stringify(eggStartOptions) ];
44
- const requires = await this.formatRequires();
45
- const execArgv: string[] = [];
46
- for (const r of requires) {
47
- const imports = this.formatImportModule(r).split(' ');
48
- execArgv.push(...imports);
49
- }
50
- await this.forkNode(serverBin, args, { execArgv });
51
- }
52
-
53
- protected async formatEggStartOptions() {
54
- const { flags } = this;
55
- flags.framework = getFrameworkPath({
56
- framework: flags.framework,
57
- baseDir: flags.base,
58
- });
59
-
60
- if (!flags.port) {
61
- let configuredPort: number | undefined;
62
- try {
63
- const configuration = await getConfig({
64
- framework: flags.framework,
65
- baseDir: flags.base,
66
- env: 'local',
67
- });
68
- configuredPort = configuration?.cluster?.listen?.port;
69
- } catch (err) {
70
- /** skip when failing to read the configuration */
71
- debug('getConfig error: %s, framework: %o, baseDir: %o, env: local',
72
- err, flags.framework, flags.base);
73
- }
74
- if (configuredPort) {
75
- flags.port = configuredPort;
76
- debug(`use port ${flags.port} from configuration file`);
77
- } else {
78
- const defaultPort = parseInt(process.env.EGG_BIN_DEFAULT_PORT ?? '7001');
79
- debug('detect available port');
80
- flags.port = await detect(defaultPort);
81
- if (flags.port !== defaultPort) {
82
- console.warn('[@eggjs/bin] server port %o is unavailable, now using port %o',
83
- defaultPort, flags.port);
84
- }
85
- debug(`use available port ${flags.port}`);
86
- }
87
- }
88
-
89
- return {
90
- baseDir: flags.base,
91
- workers: flags.workers,
92
- port: flags.port,
93
- framework: flags.framework,
94
- typescript: flags.typescript,
95
- tscompiler: flags.tscompiler,
96
- sticky: flags.sticky,
97
- };
98
- }
99
- }