├── test ├── fixtures │ ├── custom-node-dir │ │ ├── .node │ │ │ └── bin │ │ │ │ └── foo │ │ ├── config │ │ │ └── config.default.js │ │ ├── package.json │ │ └── app │ │ │ └── router.js │ ├── pkg-config-esm │ │ ├── inject1.js │ │ ├── inject2.js │ │ ├── node_modules │ │ │ ├── inject │ │ │ │ ├── index.js │ │ │ │ └── package.json │ │ │ └── custom-framework │ │ │ │ ├── package.json │ │ │ │ └── index.js │ │ ├── config │ │ │ ├── config.default.js │ │ │ └── plugin.js │ │ └── package.json │ ├── subdir-as-basedir │ │ ├── base-dir │ │ │ ├── package.json │ │ │ ├── config │ │ │ │ └── config.default.js │ │ │ └── app │ │ │ │ └── router.js │ │ └── package.json │ ├── example │ │ ├── config │ │ │ ├── config.pre.js │ │ │ └── config.default.js │ │ ├── app.js │ │ ├── node_modules │ │ │ ├── yadan │ │ │ │ ├── config │ │ │ │ │ └── config.default.js │ │ │ │ ├── package.json │ │ │ │ └── index.js │ │ │ └── custom-framework │ │ │ │ ├── package.json │ │ │ │ └── index.js │ │ ├── package.json │ │ └── app │ │ │ └── router.js │ ├── pkg-config │ │ ├── node_modules │ │ │ ├── inject │ │ │ │ └── index.js │ │ │ └── custom-framework │ │ │ │ ├── package.json │ │ │ │ └── index.js │ │ ├── inject1.js │ │ ├── inject2.js │ │ ├── config │ │ │ ├── config.default.js │ │ │ └── plugin.js │ │ └── package.json │ ├── ts │ │ ├── config │ │ │ └── config.default.js │ │ ├── app │ │ │ ├── router.js │ │ │ └── controller │ │ │ │ └── home.ts │ │ ├── package.json │ │ └── tsconfig.json │ ├── ts-pkg │ │ ├── config │ │ │ └── config.default.js │ │ ├── app │ │ │ ├── router.js │ │ │ └── controller │ │ │ │ └── home.ts │ │ ├── package.json │ │ └── tsconfig.json │ ├── pkg-config-sourcemap │ │ ├── node_modules │ │ │ ├── inject │ │ │ │ └── index.js │ │ │ └── custom-framework │ │ │ │ ├── package.json │ │ │ │ └── index.js │ │ ├── config │ │ │ ├── config.default.js │ │ │ └── plugin.js │ │ └── package.json │ ├── egg-app │ │ ├── node_modules │ │ │ └── egg │ │ │ │ ├── package.json │ │ │ │ └── index.js │ │ ├── package.json │ │ └── config │ │ │ └── config.default.js │ ├── trace-warnings │ │ ├── package.json │ │ └── app.js │ ├── egg-scripts-node-options │ │ ├── config │ │ │ └── config.default.js │ │ ├── app.js │ │ └── package.json │ ├── cluster-config │ │ ├── config │ │ │ ├── config.prod.js │ │ │ └── config.default.js │ │ ├── package.json │ │ └── app │ │ │ └── router.js │ ├── status │ │ ├── config │ │ │ └── config.default.js │ │ ├── node_modules │ │ │ └── custom-framework │ │ │ │ ├── package.json │ │ │ │ └── index.js │ │ ├── package.json │ │ └── app.js │ ├── stop-timeout │ │ ├── package.json │ │ ├── config │ │ │ └── config.default.js │ │ ├── app.js │ │ └── app │ │ │ └── router.js │ ├── egg-revert │ │ ├── config │ │ │ └── config.default.js │ │ ├── node_modules │ │ │ └── custom-framework │ │ │ │ ├── package.json │ │ │ │ └── index.js │ │ └── package.json │ ├── egg-scripts-config │ │ ├── config │ │ │ └── config.default.js │ │ ├── node_modules │ │ │ └── custom-framework │ │ │ │ ├── package.json │ │ │ │ └── index.js │ │ ├── package.json │ │ └── app.js │ └── ipc-bin │ │ └── start.js ├── egg-scripts.test.ts ├── utils.ts ├── ts.test.ts ├── stop.test.ts └── start.test.ts ├── bin ├── run.cmd ├── dev.cmd ├── run.js └── dev.js ├── .eslintignore ├── .eslintrc ├── src ├── index.ts ├── types.ts ├── helper.ts ├── baseCommand.ts └── commands │ ├── stop.ts │ └── start.ts ├── tsconfig.json ├── .gitignore ├── .github └── workflows │ ├── release.yml │ ├── nodejs.yml │ └── pkg.pr.new.yml ├── scripts ├── start-cluster.mjs └── start-cluster.cjs ├── LICENSE ├── package.json ├── README.md └── CHANGELOG.md /test/fixtures/custom-node-dir/.node/bin/foo: -------------------------------------------------------------------------------- 1 | bar 2 | -------------------------------------------------------------------------------- /bin/run.cmd: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | node "%~dp0\run" %* 4 | -------------------------------------------------------------------------------- /test/fixtures/pkg-config-esm/inject1.js: -------------------------------------------------------------------------------- 1 | console.log('@@@ inject script1'); 2 | -------------------------------------------------------------------------------- /test/fixtures/pkg-config-esm/inject2.js: -------------------------------------------------------------------------------- 1 | console.log('@@@ inject script2'); 2 | -------------------------------------------------------------------------------- /test/fixtures/custom-node-dir/config/config.default.js: -------------------------------------------------------------------------------- 1 | exports.keys = '123456'; 2 | -------------------------------------------------------------------------------- /test/fixtures/subdir-as-basedir/base-dir/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "base-dir" 3 | } -------------------------------------------------------------------------------- /test/fixtures/subdir-as-basedir/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "subdir-as-basedir" 3 | } -------------------------------------------------------------------------------- /test/fixtures/example/config/config.pre.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | exports.pre = true; 4 | -------------------------------------------------------------------------------- /test/fixtures/pkg-config/node_modules/inject/index.js: -------------------------------------------------------------------------------- 1 | console.log('@@@ inject script!'); 2 | -------------------------------------------------------------------------------- /test/fixtures/ts/config/config.default.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | exports.keys = '123456'; 4 | -------------------------------------------------------------------------------- /test/fixtures/pkg-config-esm/node_modules/inject/index.js: -------------------------------------------------------------------------------- 1 | console.log('@@@ inject script!'); 2 | -------------------------------------------------------------------------------- /test/fixtures/pkg-config/inject1.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | console.log('@@@ inject script1'); 4 | -------------------------------------------------------------------------------- /test/fixtures/pkg-config/inject2.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | console.log('@@@ inject script2'); 4 | -------------------------------------------------------------------------------- /test/fixtures/ts-pkg/config/config.default.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | exports.keys = '123456'; 4 | -------------------------------------------------------------------------------- /test/fixtures/pkg-config-sourcemap/node_modules/inject/index.js: -------------------------------------------------------------------------------- 1 | console.log('@@@ inject script!'); 2 | -------------------------------------------------------------------------------- /.eslintignore: -------------------------------------------------------------------------------- 1 | coverage 2 | test/fixtures/ts/app/controller/home.js 3 | test/fixtures/ts-pkg/app/controller/home.js -------------------------------------------------------------------------------- /bin/dev.cmd: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | node --loader ts-node/esm --no-warnings=ExperimentalWarning "%~dp0\dev" %* 4 | -------------------------------------------------------------------------------- /test/fixtures/egg-app/node_modules/egg/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "egg", 3 | "version": "1.0.0" 4 | } 5 | -------------------------------------------------------------------------------- /test/fixtures/trace-warnings/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "trace-warnings", 3 | "version": "1.0.0" 4 | } 5 | -------------------------------------------------------------------------------- /test/fixtures/egg-scripts-node-options/config/config.default.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | exports.keys = '123456'; 4 | -------------------------------------------------------------------------------- /test/fixtures/example/app.js: -------------------------------------------------------------------------------- 1 | module.exports = () => { 2 | // --no-deprecation 3 | new Buffer('aaa'); 4 | }; 5 | -------------------------------------------------------------------------------- /test/fixtures/subdir-as-basedir/base-dir/config/config.default.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | exports.keys = '123456'; 4 | -------------------------------------------------------------------------------- /test/fixtures/example/node_modules/yadan/config/config.default.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | exports.framework = 'yadan'; 4 | -------------------------------------------------------------------------------- /test/fixtures/pkg-config-esm/node_modules/inject/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "inject", 3 | "type": "module" 4 | } 5 | -------------------------------------------------------------------------------- /test/fixtures/cluster-config/config/config.prod.js: -------------------------------------------------------------------------------- 1 | exports.cluster = { 2 | listen: { 3 | port: 8000, 4 | }, 5 | }; 6 | -------------------------------------------------------------------------------- /test/fixtures/egg-app/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "example", 3 | "dependencies": { 4 | "egg": "^1.0.0" 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /bin/run.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | import { execute } from '@oclif/core'; 4 | 5 | await execute({ dir: import.meta.url }); 6 | -------------------------------------------------------------------------------- /test/fixtures/egg-app/node_modules/egg/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = require('../../../../../node_modules/egg'); 4 | -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends": [ 3 | "eslint-config-egg/typescript", 4 | "eslint-config-egg/lib/rules/enforce-node-prefix" 5 | ] 6 | } 7 | -------------------------------------------------------------------------------- /test/fixtures/status/config/config.default.js: -------------------------------------------------------------------------------- 1 | exports.keys = '123456'; 2 | 3 | exports.logger = { 4 | level: 'WARN', 5 | consoleLevel: 'WARN', 6 | }; 7 | -------------------------------------------------------------------------------- /test/fixtures/cluster-config/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "example", 3 | "version": "1.0.0", 4 | "dependencies": { 5 | "egg": "^1.0.0" 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /test/fixtures/custom-node-dir/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "example", 3 | "version": "1.0.0", 4 | "dependencies": { 5 | "egg": "^1.0.0" 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /test/fixtures/example/node_modules/yadan/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "yadan", 3 | "version": "1.0.0", 4 | "dependencies": { 5 | "egg": "*" 6 | } 7 | } -------------------------------------------------------------------------------- /test/fixtures/stop-timeout/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "stop-timeout", 3 | "version": "1.0.0", 4 | "dependencies": { 5 | "egg": "^1.0.0" 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /test/fixtures/cluster-config/config/config.default.js: -------------------------------------------------------------------------------- 1 | exports.keys = '123456'; 2 | 3 | exports.logger = { 4 | level: 'WARN', 5 | consoleLevel: 'WARN', 6 | }; 7 | -------------------------------------------------------------------------------- /test/fixtures/custom-node-dir/app/router.js: -------------------------------------------------------------------------------- 1 | module.exports = app => { 2 | app.get('/', async function() { 3 | this.body = `hi, ${process.env.PATH}`; 4 | }); 5 | }; 6 | -------------------------------------------------------------------------------- /test/fixtures/ts/app/router.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = app => { 4 | const { router, controller } = app; 5 | router.get('/', controller.home.index); 6 | }; 7 | -------------------------------------------------------------------------------- /test/fixtures/egg-app/config/config.default.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | exports.keys = '123456'; 4 | 5 | exports.logger = { 6 | level: 'WARN', 7 | consoleLevel: 'WARN', 8 | }; 9 | -------------------------------------------------------------------------------- /test/fixtures/example/config/config.default.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | exports.keys = '123456'; 4 | 5 | exports.logger = { 6 | level: 'WARN', 7 | consoleLevel: 'WARN', 8 | }; 9 | -------------------------------------------------------------------------------- /test/fixtures/example/node_modules/custom-framework/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "custom-framework", 3 | "version": "1.0.0", 4 | "dependencies": { 5 | "egg": "*" 6 | } 7 | } -------------------------------------------------------------------------------- /test/fixtures/pkg-config-esm/config/config.default.js: -------------------------------------------------------------------------------- 1 | export default { 2 | keys: '123456', 3 | logger: { 4 | level: 'WARN', 5 | consoleLevel: 'WARN', 6 | }, 7 | }; 8 | -------------------------------------------------------------------------------- /test/fixtures/status/node_modules/custom-framework/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "custom-framework", 3 | "version": "1.0.0", 4 | "dependencies": { 5 | "egg": "*" 6 | } 7 | } -------------------------------------------------------------------------------- /test/fixtures/ts-pkg/app/router.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = app => { 4 | const { router, controller } = app; 5 | router.get('/', controller.home.index); 6 | }; 7 | -------------------------------------------------------------------------------- /test/fixtures/cluster-config/app/router.js: -------------------------------------------------------------------------------- 1 | module.exports = app => { 2 | app.get('/', async function() { 3 | this.body = `hi, ${app.config.framework || 'egg'}`; 4 | }); 5 | }; 6 | -------------------------------------------------------------------------------- /test/fixtures/egg-revert/config/config.default.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | exports.keys = '123456'; 4 | 5 | exports.logger = { 6 | level: 'WARN', 7 | consoleLevel: 'WARN', 8 | }; 9 | -------------------------------------------------------------------------------- /test/fixtures/egg-revert/node_modules/custom-framework/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "custom-framework", 3 | "version": "1.0.0", 4 | "dependencies": { 5 | "egg": "*" 6 | } 7 | } -------------------------------------------------------------------------------- /test/fixtures/pkg-config/config/config.default.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | exports.keys = '123456'; 4 | 5 | exports.logger = { 6 | level: 'WARN', 7 | consoleLevel: 'WARN', 8 | }; 9 | -------------------------------------------------------------------------------- /test/fixtures/stop-timeout/config/config.default.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | exports.keys = '123456'; 4 | 5 | exports.logger = { 6 | level: 'WARN', 7 | consoleLevel: 'WARN', 8 | }; 9 | -------------------------------------------------------------------------------- /test/fixtures/egg-scripts-config/config/config.default.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | exports.keys = '123456'; 4 | 5 | exports.logger = { 6 | level: 'WARN', 7 | consoleLevel: 'WARN', 8 | }; 9 | -------------------------------------------------------------------------------- /test/fixtures/egg-scripts-config/node_modules/custom-framework/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "custom-framework", 3 | "version": "1.0.0", 4 | "dependencies": { 5 | "egg": "*" 6 | } 7 | } -------------------------------------------------------------------------------- /test/fixtures/pkg-config/node_modules/custom-framework/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "custom-framework", 3 | "version": "1.0.0", 4 | "dependencies": { 5 | "egg": "*" 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /test/fixtures/pkg-config-sourcemap/config/config.default.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | exports.keys = '123456'; 4 | 5 | exports.logger = { 6 | level: 'WARN', 7 | consoleLevel: 'WARN', 8 | }; 9 | -------------------------------------------------------------------------------- /test/fixtures/pkg-config-sourcemap/node_modules/custom-framework/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "custom-framework", 3 | "version": "1.0.0", 4 | "dependencies": { 5 | "egg": "*" 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /bin/dev.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env -S node --loader ts-node/esm --disable-warning=ExperimentalWarning 2 | 3 | import { execute } from '@oclif/core'; 4 | 5 | await execute({ development: true, dir: import.meta.url }); 6 | -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | import Start from './commands/start.js'; 2 | 3 | // exports.StopCommand = require('./lib/cmd/stop'); 4 | 5 | export * from './baseCommand.js'; 6 | export { 7 | Start, Start as StartCommand, 8 | }; 9 | -------------------------------------------------------------------------------- /test/fixtures/stop-timeout/app.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const sleep = require('mz-modules/sleep'); 4 | module.exports = app => { 5 | app.beforeClose(function* () { 6 | yield sleep(6000); 7 | }); 8 | }; 9 | -------------------------------------------------------------------------------- /test/fixtures/example/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "example", 3 | "version": "1.0.0", 4 | "dependencies": { 5 | "egg": "^1.0.0" 6 | }, 7 | "egg": { 8 | "framework": "custom-framework" 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /test/fixtures/status/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "example", 3 | "version": "1.0.0", 4 | "dependencies": { 5 | "egg": "^1.0.0" 6 | }, 7 | "egg": { 8 | "framework": "custom-framework" 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /test/fixtures/egg-scripts-node-options/app.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = () => { 4 | console.log('process.execArgv:', process.execArgv); 5 | console.log('maxHeaderSize:', require('http').maxHeaderSize); 6 | }; 7 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "@eggjs/tsconfig", 3 | "compilerOptions": { 4 | "strict": true, 5 | "noImplicitAny": true, 6 | "target": "ES2022", 7 | "module": "NodeNext", 8 | "moduleResolution": "NodeNext" 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /test/fixtures/egg-revert/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "example", 3 | "version": "1.0.0", 4 | "dependencies": { 5 | "egg": "^1.0.0" 6 | }, 7 | "egg": { 8 | "framework": "custom-framework", 9 | "revert": "CVE-2023-46809" 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/types.ts: -------------------------------------------------------------------------------- 1 | export interface PackageEgg { 2 | framework?: boolean; 3 | typescript?: boolean; 4 | tscompiler?: string; 5 | declarations?: boolean; 6 | revert?: string | string[]; 7 | require?: string | string[]; 8 | import?: string | string[]; 9 | } 10 | -------------------------------------------------------------------------------- /test/fixtures/pkg-config-esm/node_modules/custom-framework/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "custom-framework-esm", 3 | "version": "1.0.0", 4 | "dependencies": { 5 | "egg": "*" 6 | }, 7 | "type": "module", 8 | "egg": { 9 | "framework": true 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /test/fixtures/pkg-config-sourcemap/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "pkg-config-sourcemap", 3 | "version": "1.0.0", 4 | "egg": { 5 | "typescript": true, 6 | "framework": "custom-framework" 7 | }, 8 | "eggScriptsConfig": { 9 | "sourcemap": false 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /test/fixtures/pkg-config/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "example", 3 | "version": "1.0.0", 4 | "egg": { 5 | "framework": "custom-framework" 6 | }, 7 | "eggScriptsConfig": { 8 | "require": [ 9 | "./inject1.js", 10 | "inject" 11 | ] 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /test/fixtures/egg-scripts-node-options/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "example", 3 | "version": "1.0.0", 4 | "dependencies": { 5 | "egg": "^1.0.0" 6 | }, 7 | "eggScriptsConfig": { 8 | "workers": 1, 9 | "node-options--max-http-header-size": "20000" 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /test/fixtures/ts/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ts", 3 | "version": "1.0.0", 4 | "dependencies": { 5 | "egg": "^1.0.0" 6 | }, 7 | "scripts": { 8 | "build": "./../../../node_modules/.bin/tsc", 9 | "windows-build": "call ../../../node_modules/.bin/tsc.cmd" 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /test/fixtures/pkg-config-esm/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "example-esm", 3 | "version": "1.0.0", 4 | "egg": { 5 | "framework": "custom-framework" 6 | }, 7 | "eggScriptsConfig": { 8 | "require": [ 9 | "./inject1.js", 10 | "inject" 11 | ] 12 | }, 13 | "type": "module" 14 | } 15 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | logs/ 2 | npm-debug.log 3 | node_modules 4 | coverage/ 5 | .idea/ 6 | run/ 7 | .DS_Store 8 | *.swp 9 | test/fixtures/ts/app/controller/home.js 10 | test/fixtures/ts-pkg/app/controller/home.js 11 | !test/fixtures/**/node_modules 12 | package-lock.json 13 | .package-lock.json 14 | .tshy* 15 | .eslintcache 16 | dist 17 | -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | name: Release 2 | 3 | on: 4 | push: 5 | branches: [ master ] 6 | 7 | jobs: 8 | release: 9 | name: Node.js 10 | uses: eggjs/github-actions/.github/workflows/node-release.yml@master 11 | secrets: 12 | NPM_TOKEN: ${{ secrets.NPM_TOKEN }} 13 | GIT_TOKEN: ${{ secrets.GIT_TOKEN }} 14 | -------------------------------------------------------------------------------- /test/fixtures/status/app.js: -------------------------------------------------------------------------------- 1 | const { scheduler } = require('node:timers/promises'); 2 | 3 | module.exports = app => { 4 | if (process.env.ERROR) { 5 | app.logger.error(new Error(process.env.ERROR)); 6 | } 7 | 8 | app.beforeStart(async () => { 9 | await scheduler.wait(parseInt(process.env.WAIT_TIME)); 10 | }); 11 | }; 12 | -------------------------------------------------------------------------------- /test/fixtures/egg-scripts-config/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "example", 3 | "version": "1.0.0", 4 | "dependencies": { 5 | "egg": "^1.0.0" 6 | }, 7 | "egg": { 8 | "framework": "custom-framework" 9 | }, 10 | "eggScriptsConfig": { 11 | "ignore-stderr": true, 12 | "daemon": true, 13 | "workers": 1 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /test/fixtures/egg-scripts-config/app.js: -------------------------------------------------------------------------------- 1 | const { scheduler } = require('node:timers/promises'); 2 | 3 | module.exports = app => { 4 | if (process.env.ERROR) { 5 | app.logger.error(new Error(process.env.ERROR)); 6 | } 7 | 8 | app.beforeStart(async () => { 9 | await scheduler.wait(parseInt(process.env.WAIT_TIME)); 10 | }); 11 | }; 12 | -------------------------------------------------------------------------------- /test/fixtures/example/node_modules/yadan/index.js: -------------------------------------------------------------------------------- 1 | const { Application: _Application, Agent, startCluster } = require('egg'); 2 | 3 | module.exports = { 4 | Agent, 5 | startCluster, 6 | Application: class CustomApplication extends _Application { 7 | get [Symbol.for('egg#eggPath')]() { 8 | return __dirname; 9 | } 10 | }, 11 | }; 12 | -------------------------------------------------------------------------------- /test/fixtures/ts-pkg/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ts-pkg", 3 | "version": "1.0.0", 4 | "dependencies": { 5 | "egg": "^1.0.0" 6 | }, 7 | "egg": { 8 | "typescript": true 9 | }, 10 | "scripts": { 11 | "build": "./../../../node_modules/.bin/tsc", 12 | "windows-build": "call ../../../node_modules/.bin/tsc.cmd" 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /test/fixtures/example/app/router.js: -------------------------------------------------------------------------------- 1 | module.exports = app => { 2 | app.get('/', async function() { 3 | this.body = `hi, ${app.config.framework || 'egg'}`; 4 | }); 5 | 6 | app.get('/env', async function() { 7 | this.body = app.config.env + ', ' + app.config.pre; 8 | }); 9 | 10 | app.get('/path', async function() { 11 | this.body = process.env.PATH; 12 | }); 13 | }; 14 | -------------------------------------------------------------------------------- /test/fixtures/pkg-config/node_modules/custom-framework/index.js: -------------------------------------------------------------------------------- 1 | const { Application: _Application, Agent, startCluster } = require('egg'); 2 | 3 | const EGG_PATH = Symbol.for('egg#eggPath'); 4 | 5 | class Application extends _Application { 6 | get [EGG_PATH]() { 7 | return __dirname; 8 | } 9 | } 10 | 11 | module.exports = { 12 | Application, 13 | Agent, 14 | startCluster, 15 | }; 16 | -------------------------------------------------------------------------------- /test/fixtures/stop-timeout/app/router.js: -------------------------------------------------------------------------------- 1 | module.exports = app => { 2 | app.get('/', async function() { 3 | this.body = `hi, ${app.config.framework || 'egg'}`; 4 | }); 5 | 6 | app.get('/env', async function() { 7 | this.body = app.config.env + ', ' + app.config.pre; 8 | }); 9 | 10 | app.get('/path', async function() { 11 | this.body = process.env.PATH; 12 | }); 13 | }; 14 | -------------------------------------------------------------------------------- /test/fixtures/pkg-config-sourcemap/node_modules/custom-framework/index.js: -------------------------------------------------------------------------------- 1 | const { Application: _Application, Agent, startCluster } = require('egg'); 2 | 3 | const EGG_PATH = Symbol.for('egg#eggPath'); 4 | 5 | class Application extends _Application { 6 | get [EGG_PATH]() { 7 | return __dirname; 8 | } 9 | } 10 | 11 | module.exports = { 12 | Application, 13 | Agent, 14 | startCluster, 15 | }; 16 | -------------------------------------------------------------------------------- /test/fixtures/subdir-as-basedir/base-dir/app/router.js: -------------------------------------------------------------------------------- 1 | module.exports = app => { 2 | app.get('/', async function() { 3 | this.body = `hi, ${app.config.framework || 'egg'}`; 4 | }); 5 | 6 | app.get('/env', async function() { 7 | this.body = app.config.env + ', ' + app.config.pre; 8 | }); 9 | 10 | app.get('/path', async function() { 11 | this.body = process.env.PATH; 12 | }); 13 | }; 14 | -------------------------------------------------------------------------------- /test/fixtures/ts/app/controller/home.ts: -------------------------------------------------------------------------------- 1 | import { Controller } from 'egg'; 2 | 3 | export default class AppController extends Controller { 4 | public index() { 5 | try { 6 | throw new Error('some err'); 7 | } catch (err: any) { 8 | this.ctx.logger.error(err); 9 | this.ctx.body = { 10 | msg: err.message, 11 | stack: err.stack, 12 | }; 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /test/fixtures/ts-pkg/app/controller/home.ts: -------------------------------------------------------------------------------- 1 | import { Controller } from 'egg'; 2 | 3 | export default class AppController extends Controller { 4 | public index() { 5 | try { 6 | throw new Error('some err'); 7 | } catch (err: any) { 8 | this.ctx.logger.error(err); 9 | this.ctx.body = { 10 | msg: err.message, 11 | stack: err.stack, 12 | }; 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /test/fixtures/trace-warnings/app.js: -------------------------------------------------------------------------------- 1 | const EventEmitter = require('events'); 2 | 3 | module.exports = () => { 4 | console.log('app loaded'); 5 | const event = new EventEmitter(); 6 | event.setMaxListeners(1); 7 | 8 | // --trace-warnings test about MaxListenersExceededWarning 9 | event.on('xx', () => {}); 10 | event.on('xx', () => {}); 11 | 12 | // will not effect --no-deprecation argv 13 | new Buffer('aaa'); 14 | }; 15 | -------------------------------------------------------------------------------- /.github/workflows/nodejs.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: 4 | push: 5 | branches: [ master ] 6 | pull_request: 7 | branches: [ master ] 8 | 9 | jobs: 10 | Job: 11 | name: Node.js 12 | uses: node-modules/github-actions/.github/workflows/node-test.yml@master 13 | with: 14 | version: '18.19.0, 18, 20, 22' 15 | os: 'ubuntu-latest, macos-latest' 16 | secrets: 17 | CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} 18 | -------------------------------------------------------------------------------- /scripts/start-cluster.mjs: -------------------------------------------------------------------------------- 1 | import { debuglog } from 'node:util'; 2 | import { importModule } from '@eggjs/utils'; 3 | 4 | const debug = debuglog('@eggjs/scripts/scripts/start-cluster'); 5 | 6 | async function main() { 7 | debug('argv: %o', process.argv); 8 | const options = JSON.parse(process.argv[2]); 9 | debug('start cluster options: %o', options); 10 | const { startCluster } = await importModule(options.framework); 11 | await startCluster(options); 12 | } 13 | 14 | main(); 15 | -------------------------------------------------------------------------------- /scripts/start-cluster.cjs: -------------------------------------------------------------------------------- 1 | /* eslint-disable @typescript-eslint/no-var-requires */ 2 | const { debuglog } = require('node:util'); 3 | const { importModule } = require('@eggjs/utils'); 4 | 5 | const debug = debuglog('@eggjs/scripts/scripts/start-cluster'); 6 | 7 | async function main() { 8 | debug('argv: %o', process.argv); 9 | const options = JSON.parse(process.argv[2]); 10 | debug('start cluster options: %o', options); 11 | const { startCluster } = await importModule(options.framework); 12 | await startCluster(options); 13 | } 14 | 15 | main(); 16 | -------------------------------------------------------------------------------- /.github/workflows/pkg.pr.new.yml: -------------------------------------------------------------------------------- 1 | name: Publish Any Commit 2 | on: [push, pull_request] 3 | 4 | jobs: 5 | build: 6 | runs-on: ubuntu-latest 7 | 8 | steps: 9 | - name: Checkout code 10 | uses: actions/checkout@v4 11 | 12 | - run: corepack enable 13 | - uses: actions/setup-node@v4 14 | with: 15 | node-version: 20 16 | 17 | - name: Install dependencies 18 | run: npm install 19 | 20 | - name: Build 21 | run: npm run prepublishOnly --if-present 22 | 23 | - run: npx pkg-pr-new publish 24 | -------------------------------------------------------------------------------- /test/fixtures/pkg-config-esm/node_modules/custom-framework/index.js: -------------------------------------------------------------------------------- 1 | import path from 'node:path'; 2 | import { fileURLToPath } from 'node:url'; 3 | import { Application as _Application, Agent, startCluster } from 'egg'; 4 | 5 | const EGG_PATH = Symbol.for('egg#eggPath'); 6 | const __filename = fileURLToPath(import.meta.url); 7 | const __dirname = path.dirname(__filename); 8 | 9 | class Application extends _Application { 10 | get [EGG_PATH]() { 11 | return __dirname; 12 | } 13 | } 14 | 15 | export { 16 | Application, 17 | Agent, 18 | startCluster, 19 | }; 20 | -------------------------------------------------------------------------------- /test/fixtures/example/node_modules/custom-framework/index.js: -------------------------------------------------------------------------------- 1 | const { Application: _Application, Agent, startCluster: originStartCluster } = require('egg'); 2 | 3 | module.exports = { 4 | Agent, 5 | Application: class CustomApplication extends _Application { 6 | get [Symbol.for('egg#eggPath')]() { 7 | return __dirname; 8 | } 9 | }, 10 | startCluster(...args) { 11 | if (process.env.CUSTOM_ENV && !process.env.EGG_SERVER_ENV) { 12 | console.log('## EGG_SERVER_ENV is not pass'); 13 | console.log('## CUSTOM_ENV:', process.env.CUSTOM_ENV); 14 | process.env.EGG_SERVER_ENV = process.env.CUSTOM_ENV; 15 | } 16 | return originStartCluster(...args); 17 | }, 18 | }; 19 | -------------------------------------------------------------------------------- /test/fixtures/status/node_modules/custom-framework/index.js: -------------------------------------------------------------------------------- 1 | const { Application: _Application, Agent, startCluster: originStartCluster } = require('egg'); 2 | 3 | const EGG_PATH = Symbol.for('egg#eggPath'); 4 | 5 | class Application extends _Application { 6 | get [EGG_PATH]() { 7 | return __dirname; 8 | } 9 | } 10 | 11 | function startCluster(...args) { 12 | if (process.env.CUSTOM_ENV && !process.env.EGG_SERVER_ENV) { 13 | console.log('## EGG_SERVER_ENV is not pass'); 14 | console.log('## CUSTOM_ENV:', process.env.CUSTOM_ENV); 15 | process.env.EGG_SERVER_ENV = process.env.CUSTOM_ENV; 16 | } 17 | return originStartCluster(...args); 18 | } 19 | 20 | module.exports = { 21 | Application, 22 | Agent, 23 | startCluster, 24 | }; 25 | -------------------------------------------------------------------------------- /test/fixtures/egg-revert/node_modules/custom-framework/index.js: -------------------------------------------------------------------------------- 1 | const { Application: _Application, Agent, startCluster: originStartCluster } = require('egg'); 2 | 3 | const EGG_PATH = Symbol.for('egg#eggPath'); 4 | 5 | class Application extends _Application { 6 | get [EGG_PATH]() { 7 | return __dirname; 8 | } 9 | } 10 | 11 | function startCluster(...args) { 12 | if (process.env.CUSTOM_ENV && !process.env.EGG_SERVER_ENV) { 13 | console.log('## EGG_SERVER_ENV is not pass'); 14 | console.log('## CUSTOM_ENV:', process.env.CUSTOM_ENV); 15 | process.env.EGG_SERVER_ENV = process.env.CUSTOM_ENV; 16 | } 17 | return originStartCluster(...args); 18 | } 19 | 20 | module.exports = { 21 | Application, 22 | Agent, 23 | startCluster, 24 | }; 25 | -------------------------------------------------------------------------------- /test/fixtures/egg-scripts-config/node_modules/custom-framework/index.js: -------------------------------------------------------------------------------- 1 | const { Application: _Application, Agent, startCluster: originStartCluster } = require('egg'); 2 | 3 | const EGG_PATH = Symbol.for('egg#eggPath'); 4 | 5 | class Application extends _Application { 6 | get [EGG_PATH]() { 7 | return __dirname; 8 | } 9 | } 10 | 11 | function startCluster(...args) { 12 | if (process.env.CUSTOM_ENV && !process.env.EGG_SERVER_ENV) { 13 | console.log('## EGG_SERVER_ENV is not pass'); 14 | console.log('## CUSTOM_ENV:', process.env.CUSTOM_ENV); 15 | process.env.EGG_SERVER_ENV = process.env.CUSTOM_ENV; 16 | } 17 | return originStartCluster(...args); 18 | } 19 | 20 | module.exports = { 21 | Application, 22 | Agent, 23 | startCluster, 24 | }; 25 | -------------------------------------------------------------------------------- /test/egg-scripts.test.ts: -------------------------------------------------------------------------------- 1 | import coffee from 'coffee'; 2 | import { getSourceFilename } from '../src/helper.js'; 3 | 4 | describe('test/egg-scripts.test.ts', () => { 5 | const eggBin = getSourceFilename('../bin/run.js'); 6 | 7 | it('show help', async () => { 8 | await coffee.fork(eggBin, [ '--help' ]) 9 | .debug() 10 | .expect('stdout', /\$ eggctl \[COMMAND]/) 11 | .expect('code', 0) 12 | .end(); 13 | 14 | await coffee.fork(eggBin, [ 'start', '-h' ]) 15 | .debug() 16 | .expect('stdout', /\$ eggctl start \[BASEDIR] /) 17 | .expect('code', 0) 18 | .end(); 19 | 20 | await coffee.fork(eggBin, [ 'stop', '-h' ]) 21 | .debug() 22 | .expect('stdout', /\$ eggctl stop \[BASEDIR] /) 23 | .expect('code', 0) 24 | .end(); 25 | }); 26 | }); 27 | -------------------------------------------------------------------------------- /test/fixtures/ts/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compileOnSave": true, 3 | "compilerOptions": { 4 | "target": "es2017", 5 | "module": "commonjs", 6 | "strict": true, 7 | "noImplicitAny": false, 8 | "experimentalDecorators": true, 9 | "emitDecoratorMetadata": true, 10 | "allowJs": false, 11 | "pretty": true, 12 | "noEmitOnError": false, 13 | "noUnusedLocals": true, 14 | "noUnusedParameters": true, 15 | "allowUnreachableCode": false, 16 | "allowUnusedLabels": false, 17 | "strictPropertyInitialization": false, 18 | "noFallthroughCasesInSwitch": true, 19 | "skipLibCheck": true, 20 | "skipDefaultLibCheck": true, 21 | "inlineSourceMap": true, 22 | "importHelpers": true 23 | }, 24 | "exclude": [ 25 | "app/public", 26 | "app/views" 27 | ] 28 | } 29 | -------------------------------------------------------------------------------- /test/fixtures/ts-pkg/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compileOnSave": true, 3 | "compilerOptions": { 4 | "target": "es2017", 5 | "module": "commonjs", 6 | "strict": true, 7 | "noImplicitAny": false, 8 | "experimentalDecorators": true, 9 | "emitDecoratorMetadata": true, 10 | "charset": "utf8", 11 | "allowJs": false, 12 | "pretty": true, 13 | "noEmitOnError": false, 14 | "noUnusedLocals": true, 15 | "noUnusedParameters": true, 16 | "allowUnreachableCode": false, 17 | "allowUnusedLabels": false, 18 | "strictPropertyInitialization": false, 19 | "noFallthroughCasesInSwitch": true, 20 | "skipLibCheck": true, 21 | "skipDefaultLibCheck": true, 22 | "inlineSourceMap": true, 23 | "importHelpers": true 24 | }, 25 | "exclude": [ 26 | "app/public", 27 | "app/views" 28 | ] 29 | } -------------------------------------------------------------------------------- /test/fixtures/ipc-bin/start.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const co = require('co'); 4 | 5 | const BaseStartCommand = require('../../../lib/cmd/start'); 6 | 7 | class StartCommand extends BaseStartCommand { 8 | * run(context) { 9 | yield super.run(context); 10 | const child = this.child; 11 | child.on('message', msg => { 12 | if (msg && msg.action === 'egg-ready') { 13 | console.log('READY!!!'); 14 | } 15 | }); 16 | } 17 | } 18 | 19 | const start = new StartCommand(); 20 | 21 | co(function* () { 22 | yield start.run({ 23 | argv: { 24 | framework: 'custom-framework', 25 | _: [ process.env.BASE_DIR ], 26 | workers: 2, 27 | title: 'egg-server-example', 28 | }, 29 | cwd: process.env.BASE_DIR, 30 | // FIXME: overide run argv so execArgvObj is missing 31 | // execArgv: [], 32 | env: { 33 | PATH: process.env.PATH, 34 | }, 35 | }); 36 | }); 37 | 38 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017-present Alibaba Group Holding Limited and other contributors. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /test/utils.ts: -------------------------------------------------------------------------------- 1 | import { scheduler } from 'node:timers/promises'; 2 | import { ChildProcess } from 'node:child_process'; 3 | import { Coffee as _Coffee } from 'coffee'; 4 | import { isWindows, findNodeProcess } from '../src/helper.js'; 5 | 6 | export type Coffee = _Coffee & { proc: ChildProcess, stderr: string, stdout: string, code?: number }; 7 | 8 | export async function cleanup(baseDir: string) { 9 | const processList = await findNodeProcess(x => { 10 | const dir = isWindows ? baseDir.replace(/\\/g, '\\\\') : baseDir; 11 | const prefix = isWindows ? '\\"baseDir\\":\\"' : '"baseDir":"'; 12 | return x.cmd.includes(`${prefix}${dir}`); 13 | }); 14 | 15 | if (processList.length) { 16 | console.log(`cleanup: ${processList.length} to kill`); 17 | for (const item of processList) { 18 | const pid = item.pid; 19 | const cmd = item.cmd; 20 | let type = 'unknown: ' + cmd; 21 | if (cmd.includes('start-cluster')) { 22 | type = 'master'; 23 | } else if (cmd.includes('app_worker.js')) { 24 | type = 'worker'; 25 | } else if (cmd.includes('agent_worker.js')) { 26 | type = 'agent'; 27 | } 28 | 29 | try { 30 | process.kill(pid, type === 'master' ? '' : 'SIGKILL'); 31 | console.log(`cleanup ${type} ${pid}`); 32 | } catch (err: any) { 33 | console.log(`cleanup ${type} ${pid} got error ${err.code || err.message || err}`); 34 | if (err.code !== 'ESRCH') { 35 | throw err; 36 | } 37 | } 38 | } 39 | 40 | await scheduler.wait(5000); 41 | } 42 | } 43 | 44 | export function replaceWeakRefMessage(stderr: string) { 45 | // Using compatibility WeakRef and FinalizationRegistry\r\n 46 | if (stderr.includes('Using compatibility WeakRef and FinalizationRegistry')) { 47 | stderr = stderr.replace(/Using compatibility WeakRef and FinalizationRegistry[\r\n]*/g, ''); 48 | } 49 | return stderr; 50 | } 51 | -------------------------------------------------------------------------------- /src/helper.ts: -------------------------------------------------------------------------------- 1 | import { runScript } from 'runscript'; 2 | import path from 'node:path'; 3 | import { fileURLToPath } from 'node:url'; 4 | 5 | export const isWindows = process.platform === 'win32'; 6 | 7 | const REGEX = isWindows ? /^(.*)\s+(\d+)\s*$/ : /^\s*(\d+)\s+(.*)/; 8 | 9 | export interface NodeProcess { 10 | pid: number; 11 | cmd: string; 12 | } 13 | 14 | export type FilterFunction = (item: NodeProcess) => boolean; 15 | 16 | export async function findNodeProcess(filterFn?: FilterFunction): Promise { 17 | const command = isWindows ? 18 | 'wmic Path win32_process Where "Name = \'node.exe\'" Get CommandLine,ProcessId' : 19 | // command, cmd are alias of args, not POSIX standard, so we use args 20 | 'ps -wweo "pid,args"'; 21 | const stdio = await runScript(command, { stdio: 'pipe' }); 22 | const processList = stdio.stdout!.toString().split('\n') 23 | .reduce((arr, line) => { 24 | if (!!line && !line.includes('/bin/sh') && line.includes('node')) { 25 | const m = line.match(REGEX); 26 | if (m) { 27 | const item: NodeProcess = isWindows ? { pid: parseInt(m[2]), cmd: m[1] } : { pid: parseInt(m[1]), cmd: m[2] }; 28 | if (filterFn?.(item)) { 29 | arr.push(item); 30 | } 31 | } 32 | } 33 | return arr; 34 | }, []); 35 | return processList; 36 | } 37 | 38 | export function kill(pids: number[], signal?: string | number) { 39 | pids.forEach(pid => { 40 | try { 41 | process.kill(pid, signal); 42 | } catch (err: any) { 43 | if (err.code !== 'ESRCH') { 44 | throw err; 45 | } 46 | } 47 | }); 48 | } 49 | 50 | export function getSourceDirname() { 51 | if (typeof __dirname === 'string') { 52 | return __dirname; 53 | } 54 | // eslint-disable-next-line @typescript-eslint/ban-ts-comment 55 | // @ts-ignore 56 | const __filename = fileURLToPath(import.meta.url); 57 | return path.dirname(__filename); 58 | } 59 | 60 | export function getSourceFilename(filename: string) { 61 | return path.join(getSourceDirname(), filename); 62 | } 63 | -------------------------------------------------------------------------------- /src/baseCommand.ts: -------------------------------------------------------------------------------- 1 | import { debuglog } from 'node:util'; 2 | import { Command, Flags, Interfaces } from '@oclif/core'; 3 | import { PackageEgg } from './types.js'; 4 | import { readJSON } from 'utility'; 5 | import path from 'node:path'; 6 | 7 | const debug = debuglog('@eggjs/scripts/baseCommand'); 8 | 9 | type Flags = Interfaces.InferredFlags; 10 | type Args = Interfaces.InferredArgs; 11 | 12 | export abstract class BaseCommand extends Command { 13 | // add the --json flag 14 | static enableJsonFlag = false; 15 | 16 | // define flags that can be inherited by any command that extends BaseCommand 17 | static baseFlags = { 18 | // 'log-level': Flags.option({ 19 | // default: 'info', 20 | // helpGroup: 'GLOBAL', 21 | // options: ['debug', 'warn', 'error', 'info', 'trace'] as const, 22 | // summary: 'Specify level for logging.', 23 | // })(), 24 | }; 25 | 26 | protected flags!: Flags; 27 | protected args!: Args; 28 | 29 | protected env = { ...process.env }; 30 | protected pkg: Record; 31 | protected isESM: boolean; 32 | protected pkgEgg: PackageEgg; 33 | protected globalExecArgv: string[] = []; 34 | 35 | public async init(): Promise { 36 | await super.init(); 37 | debug('[init] raw args: %o, NODE_ENV: %o', this.argv, this.env.NODE_ENV); 38 | const { args, flags } = await this.parse({ 39 | flags: this.ctor.flags, 40 | baseFlags: (super.ctor as typeof BaseCommand).baseFlags, 41 | enableJsonFlag: this.ctor.enableJsonFlag, 42 | args: this.ctor.args, 43 | strict: this.ctor.strict, 44 | }); 45 | this.flags = flags as Flags; 46 | this.args = args as Args; 47 | } 48 | 49 | protected async initBaseInfo(baseDir: string) { 50 | const pkg = await readJSON(path.join(baseDir, 'package.json')); 51 | this.pkg = pkg; 52 | this.pkgEgg = pkg.egg ?? {}; 53 | this.isESM = pkg.type === 'module'; 54 | debug('[initBaseInfo] baseDir: %o, pkgEgg: %o, isESM: %o', baseDir, this.pkgEgg, this.isESM); 55 | } 56 | 57 | protected async catch(err: Error & {exitCode?: number}): Promise { 58 | // add any custom logic to handle errors from the command 59 | // or simply return the parent class error handling 60 | return super.catch(err); 61 | } 62 | 63 | protected async finally(_: Error | undefined): Promise { 64 | // called after run and catch regardless of whether or not the command errored 65 | return super.finally(_); 66 | } 67 | } 68 | 69 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@eggjs/scripts", 3 | "version": "4.0.0", 4 | "publishConfig": { 5 | "access": "public" 6 | }, 7 | "description": "deploy tool for egg project", 8 | "dependencies": { 9 | "@eggjs/utils": "^4.2.1", 10 | "@oclif/core": "^4.2.0", 11 | "common-bin": "^3.0.1", 12 | "mz": "^2.7.0", 13 | "mz-modules": "^2.1.0", 14 | "node-homedir": "^2.0.0", 15 | "runscript": "^2.0.1", 16 | "source-map-support": "^0.5.21", 17 | "utility": "^2.4.0" 18 | }, 19 | "devDependencies": { 20 | "@arethetypeswrong/cli": "^0.17.1", 21 | "@eggjs/bin": "^7.0.1", 22 | "@eggjs/tsconfig": "1", 23 | "@types/mocha": "10", 24 | "@types/node": "22", 25 | "coffee": "^5.5.1", 26 | "egg": "beta", 27 | "eslint": "8", 28 | "eslint-config-egg": "14", 29 | "mm": "^4.0.1", 30 | "rimraf": "6", 31 | "ts-node": "^10.9.2", 32 | "tshy": "3", 33 | "tshy-after": "1", 34 | "typescript": "5", 35 | "urllib": "4" 36 | }, 37 | "engines": { 38 | "node": ">=18.19.0" 39 | }, 40 | "scripts": { 41 | "lint": "eslint --cache src test --ext .ts", 42 | "pretest": "npm run clean && npm run lint -- --fix && npm run prepublishOnly", 43 | "test": "egg-bin test", 44 | "posttest": "npm run clean", 45 | "preci": "npm run clean && npm run lint && npm run prepublishOnly", 46 | "ci": "egg-bin test", 47 | "postci": "npm run clean", 48 | "clean": "rimraf dist", 49 | "prepublishOnly": "tshy && tshy-after && attw --pack" 50 | }, 51 | "bug": { 52 | "url": "https://github.com/eggjs/egg/issues" 53 | }, 54 | "homepage": "https://github.com/eggjs/scripts", 55 | "repository": { 56 | "type": "git", 57 | "url": "git@github.com:eggjs/scripts.git" 58 | }, 59 | "author": "TZ ", 60 | "license": "MIT", 61 | "oclif": { 62 | "bin": "eggctl", 63 | "commands": "./dist/esm/commands", 64 | "dirname": "eggctl", 65 | "topicSeparator": " ", 66 | "additionalHelpFlags": [ 67 | "-h" 68 | ] 69 | }, 70 | "bin": { 71 | "egg-scripts": "./bin/run.js", 72 | "eggctl": "./bin/run.js" 73 | }, 74 | "type": "module", 75 | "tshy": { 76 | "exports": { 77 | ".": "./src/index.ts", 78 | "./package.json": "./package.json" 79 | } 80 | }, 81 | "exports": { 82 | ".": { 83 | "import": { 84 | "types": "./dist/esm/index.d.ts", 85 | "default": "./dist/esm/index.js" 86 | }, 87 | "require": { 88 | "types": "./dist/commonjs/index.d.ts", 89 | "default": "./dist/commonjs/index.js" 90 | } 91 | }, 92 | "./package.json": "./package.json" 93 | }, 94 | "files": [ 95 | "bin", 96 | "dist", 97 | "src", 98 | "scripts" 99 | ], 100 | "types": "./dist/commonjs/index.d.ts", 101 | "main": "./dist/commonjs/index.js", 102 | "module": "./dist/esm/index.js" 103 | } 104 | -------------------------------------------------------------------------------- /test/fixtures/pkg-config-esm/config/plugin.js: -------------------------------------------------------------------------------- 1 | export default { 2 | // enable plugins 3 | 4 | /** 5 | * app global Error Handling 6 | * @member {Object} Plugin#onerror 7 | * @property {Boolean} enable - `true` by default 8 | */ 9 | onerror: { 10 | enable: false, 11 | package: 'egg-onerror', 12 | path: 'xxxxx', 13 | }, 14 | 15 | /** 16 | * session 17 | * @member {Object} Plugin#session 18 | * @property {Boolean} enable - `true` by default 19 | * @since 1.0.0 20 | */ 21 | session: { 22 | enable: false, 23 | package: 'egg-session', 24 | path: 'xxxxx', 25 | }, 26 | 27 | /** 28 | * i18n 29 | * @member {Object} Plugin#i18n 30 | * @property {Boolean} enable - `true` by default 31 | * @since 1.0.0 32 | */ 33 | i18n: { 34 | enable: false, 35 | package: 'egg-i18n', 36 | path: 'xxxxx', 37 | }, 38 | 39 | /** 40 | * file and dir watcher 41 | * @member {Object} Plugin#watcher 42 | * @property {Boolean} enable - `true` by default 43 | * @since 1.0.0 44 | */ 45 | watcher: { 46 | enable: false, 47 | package: 'egg-watcher', 48 | path: 'xxxxx', 49 | }, 50 | 51 | /** 52 | * multipart 53 | * @member {Object} Plugin#multipart 54 | * @property {Boolean} enable - `true` by default 55 | * @since 1.0.0 56 | */ 57 | multipart: { 58 | enable: false, 59 | package: 'egg-multipart', 60 | path: 'xxxxx', 61 | }, 62 | 63 | /** 64 | * security middlewares and extends 65 | * @member {Object} Plugin#security 66 | * @property {Boolean} enable - `true` by default 67 | * @since 1.0.0 68 | */ 69 | security: { 70 | enable: false, 71 | package: 'egg-security', 72 | path: 'xxxxx', 73 | }, 74 | 75 | /** 76 | * local development helper 77 | * @member {Object} Plugin#development 78 | * @property {Boolean} enable - `true` by default 79 | * @since 1.0.0 80 | */ 81 | development: { 82 | enable: false, 83 | package: 'egg-development', 84 | path: 'xxxxx', 85 | }, 86 | 87 | /** 88 | * logger file rotator 89 | * @member {Object} Plugin#logrotator 90 | * @property {Boolean} enable - `true` by default 91 | * @since 1.0.0 92 | */ 93 | logrotator: { 94 | enable: false, 95 | package: 'egg-logrotator', 96 | path: 'xxxxx', 97 | }, 98 | 99 | /** 100 | * schedule tasks 101 | * @member {Object} Plugin#schedule 102 | * @property {Boolean} enable - `true` by default 103 | * @since 2.7.0 104 | */ 105 | schedule: { 106 | enable: false, 107 | package: 'egg-schedule', 108 | path: 'xxxxx', 109 | }, 110 | 111 | /** 112 | * `app/public` dir static serve 113 | * @member {Object} Plugin#static 114 | * @property {Boolean} enable - `true` by default 115 | * @since 1.0.0 116 | */ 117 | static: { 118 | enable: false, 119 | package: 'egg-static', 120 | path: 'xxxxx', 121 | }, 122 | 123 | /** 124 | * jsonp support for egg 125 | * @member {Function} Plugin#jsonp 126 | * @property {Boolean} enable - `true` by default 127 | * @since 1.0.0 128 | */ 129 | jsonp: { 130 | enable: false, 131 | package: 'egg-jsonp', 132 | path: 'xxxxx', 133 | }, 134 | 135 | /** 136 | * view plugin 137 | * @member {Function} Plugin#view 138 | * @property {Boolean} enable - `true` by default 139 | * @since 1.0.0 140 | */ 141 | view: { 142 | enable: false, 143 | package: 'egg-view', 144 | path: 'xxxxx', 145 | }, 146 | }; 147 | -------------------------------------------------------------------------------- /test/fixtures/pkg-config/config/plugin.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { 4 | // enable plugins 5 | 6 | /** 7 | * app global Error Handling 8 | * @member {Object} Plugin#onerror 9 | * @property {Boolean} enable - `true` by default 10 | */ 11 | onerror: { 12 | enable: false, 13 | package: 'egg-onerror', 14 | path: 'xxxxx', 15 | }, 16 | 17 | /** 18 | * session 19 | * @member {Object} Plugin#session 20 | * @property {Boolean} enable - `true` by default 21 | * @since 1.0.0 22 | */ 23 | session: { 24 | enable: false, 25 | package: 'egg-session', 26 | path: 'xxxxx', 27 | }, 28 | 29 | /** 30 | * i18n 31 | * @member {Object} Plugin#i18n 32 | * @property {Boolean} enable - `true` by default 33 | * @since 1.0.0 34 | */ 35 | i18n: { 36 | enable: false, 37 | package: 'egg-i18n', 38 | path: 'xxxxx', 39 | }, 40 | 41 | /** 42 | * file and dir watcher 43 | * @member {Object} Plugin#watcher 44 | * @property {Boolean} enable - `true` by default 45 | * @since 1.0.0 46 | */ 47 | watcher: { 48 | enable: false, 49 | package: 'egg-watcher', 50 | path: 'xxxxx', 51 | }, 52 | 53 | /** 54 | * multipart 55 | * @member {Object} Plugin#multipart 56 | * @property {Boolean} enable - `true` by default 57 | * @since 1.0.0 58 | */ 59 | multipart: { 60 | enable: false, 61 | package: 'egg-multipart', 62 | path: 'xxxxx', 63 | }, 64 | 65 | /** 66 | * security middlewares and extends 67 | * @member {Object} Plugin#security 68 | * @property {Boolean} enable - `true` by default 69 | * @since 1.0.0 70 | */ 71 | security: { 72 | enable: false, 73 | package: 'egg-security', 74 | path: 'xxxxx', 75 | }, 76 | 77 | /** 78 | * local development helper 79 | * @member {Object} Plugin#development 80 | * @property {Boolean} enable - `true` by default 81 | * @since 1.0.0 82 | */ 83 | development: { 84 | enable: false, 85 | package: 'egg-development', 86 | path: 'xxxxx', 87 | }, 88 | 89 | /** 90 | * logger file rotator 91 | * @member {Object} Plugin#logrotator 92 | * @property {Boolean} enable - `true` by default 93 | * @since 1.0.0 94 | */ 95 | logrotator: { 96 | enable: false, 97 | package: 'egg-logrotator', 98 | path: 'xxxxx', 99 | }, 100 | 101 | /** 102 | * schedule tasks 103 | * @member {Object} Plugin#schedule 104 | * @property {Boolean} enable - `true` by default 105 | * @since 2.7.0 106 | */ 107 | schedule: { 108 | enable: false, 109 | package: 'egg-schedule', 110 | path: 'xxxxx', 111 | }, 112 | 113 | /** 114 | * `app/public` dir static serve 115 | * @member {Object} Plugin#static 116 | * @property {Boolean} enable - `true` by default 117 | * @since 1.0.0 118 | */ 119 | static: { 120 | enable: false, 121 | package: 'egg-static', 122 | path: 'xxxxx', 123 | }, 124 | 125 | /** 126 | * jsonp support for egg 127 | * @member {Function} Plugin#jsonp 128 | * @property {Boolean} enable - `true` by default 129 | * @since 1.0.0 130 | */ 131 | jsonp: { 132 | enable: false, 133 | package: 'egg-jsonp', 134 | path: 'xxxxx', 135 | }, 136 | 137 | /** 138 | * view plugin 139 | * @member {Function} Plugin#view 140 | * @property {Boolean} enable - `true` by default 141 | * @since 1.0.0 142 | */ 143 | view: { 144 | enable: false, 145 | package: 'egg-view', 146 | path: 'xxxxx', 147 | }, 148 | }; 149 | -------------------------------------------------------------------------------- /test/fixtures/pkg-config-sourcemap/config/plugin.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { 4 | // enable plugins 5 | 6 | /** 7 | * app global Error Handling 8 | * @member {Object} Plugin#onerror 9 | * @property {Boolean} enable - `true` by default 10 | */ 11 | onerror: { 12 | enable: false, 13 | package: 'egg-onerror', 14 | path: 'xxxxx', 15 | }, 16 | 17 | /** 18 | * session 19 | * @member {Object} Plugin#session 20 | * @property {Boolean} enable - `true` by default 21 | * @since 1.0.0 22 | */ 23 | session: { 24 | enable: false, 25 | package: 'egg-session', 26 | path: 'xxxxx', 27 | }, 28 | 29 | /** 30 | * i18n 31 | * @member {Object} Plugin#i18n 32 | * @property {Boolean} enable - `true` by default 33 | * @since 1.0.0 34 | */ 35 | i18n: { 36 | enable: false, 37 | package: 'egg-i18n', 38 | path: 'xxxxx', 39 | }, 40 | 41 | /** 42 | * file and dir watcher 43 | * @member {Object} Plugin#watcher 44 | * @property {Boolean} enable - `true` by default 45 | * @since 1.0.0 46 | */ 47 | watcher: { 48 | enable: false, 49 | package: 'egg-watcher', 50 | path: 'xxxxx', 51 | }, 52 | 53 | /** 54 | * multipart 55 | * @member {Object} Plugin#multipart 56 | * @property {Boolean} enable - `true` by default 57 | * @since 1.0.0 58 | */ 59 | multipart: { 60 | enable: false, 61 | package: 'egg-multipart', 62 | path: 'xxxxx', 63 | }, 64 | 65 | /** 66 | * security middlewares and extends 67 | * @member {Object} Plugin#security 68 | * @property {Boolean} enable - `true` by default 69 | * @since 1.0.0 70 | */ 71 | security: { 72 | enable: false, 73 | package: 'egg-security', 74 | path: 'xxxxx', 75 | }, 76 | 77 | /** 78 | * local development helper 79 | * @member {Object} Plugin#development 80 | * @property {Boolean} enable - `true` by default 81 | * @since 1.0.0 82 | */ 83 | development: { 84 | enable: false, 85 | package: 'egg-development', 86 | path: 'xxxxx', 87 | }, 88 | 89 | /** 90 | * logger file rotator 91 | * @member {Object} Plugin#logrotator 92 | * @property {Boolean} enable - `true` by default 93 | * @since 1.0.0 94 | */ 95 | logrotator: { 96 | enable: false, 97 | package: 'egg-logrotator', 98 | path: 'xxxxx', 99 | }, 100 | 101 | /** 102 | * schedule tasks 103 | * @member {Object} Plugin#schedule 104 | * @property {Boolean} enable - `true` by default 105 | * @since 2.7.0 106 | */ 107 | schedule: { 108 | enable: false, 109 | package: 'egg-schedule', 110 | path: 'xxxxx', 111 | }, 112 | 113 | /** 114 | * `app/public` dir static serve 115 | * @member {Object} Plugin#static 116 | * @property {Boolean} enable - `true` by default 117 | * @since 1.0.0 118 | */ 119 | static: { 120 | enable: false, 121 | package: 'egg-static', 122 | path: 'xxxxx', 123 | }, 124 | 125 | /** 126 | * jsonp support for egg 127 | * @member {Function} Plugin#jsonp 128 | * @property {Boolean} enable - `true` by default 129 | * @since 1.0.0 130 | */ 131 | jsonp: { 132 | enable: false, 133 | package: 'egg-jsonp', 134 | path: 'xxxxx', 135 | }, 136 | 137 | /** 138 | * view plugin 139 | * @member {Function} Plugin#view 140 | * @property {Boolean} enable - `true` by default 141 | * @since 1.0.0 142 | */ 143 | view: { 144 | enable: false, 145 | package: 'egg-view', 146 | path: 'xxxxx', 147 | }, 148 | }; 149 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # @eggjs/scripts 2 | 3 | [![NPM version][npm-image]][npm-url] 4 | [![Node.js CI](https://github.com/eggjs/scripts/actions/workflows/nodejs.yml/badge.svg)](https://github.com/eggjs/scripts/actions/workflows/nodejs.yml) 5 | [![Test coverage][codecov-image]][codecov-url] 6 | [![npm download][download-image]][download-url] 7 | [![Node.js Version](https://img.shields.io/node/v/@eggjs/scripts.svg?style=flat)](https://nodejs.org/en/download/) 8 | [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](https://makeapullrequest.com) 9 | 10 | [npm-image]: https://img.shields.io/npm/v/@eggjs/scripts.svg?style=flat-square 11 | [npm-url]: https://npmjs.org/package/@eggjs/scripts 12 | [codecov-image]: https://codecov.io/github/eggjs/scripts/coverage.svg?branch=master 13 | [codecov-url]: https://codecov.io/github/eggjs/scripts?branch=master 14 | [download-image]: https://img.shields.io/npm/dm/@eggjs/scripts.svg?style=flat-square 15 | [download-url]: https://npmjs.org/package/@eggjs/scripts 16 | 17 | deploy tool for egg project. 18 | 19 | **Note: Windows is partially supported, see [#22](https://github.com/eggjs/egg-scripts/pull/22)** 20 | 21 | ## Install 22 | 23 | ```bash 24 | npm i @eggjs/scripts --save 25 | ``` 26 | 27 | ## Usage 28 | 29 | Add `eggctl` to `package.json` scripts: 30 | 31 | ```json 32 | { 33 | "scripts": { 34 | "start": "eggctl start --daemon", 35 | "stop": "eggctl stop" 36 | } 37 | } 38 | ``` 39 | 40 | Then run as: 41 | 42 | - `npm start` 43 | - `npm stop` 44 | 45 | **Note:** `egg-scripts` is not recommended to install global, you should install and use it as npm scripts. 46 | 47 | ## Command 48 | 49 | ### start 50 | 51 | Start egg at prod mode. 52 | 53 | ```bash 54 | $ eggctl start [options] [baseDir] 55 | 56 | # Usage 57 | # eggctl start --port=7001 58 | # eggctl start ./server 59 | ``` 60 | 61 | - **Arguments** 62 | - `baseDir` - directory of application, default to `process.cwd()`. 63 | - **Options** 64 | - `port` - listening port, default to `process.env.PORT`, if unset, egg will use `7001` as default. 65 | - `title` - process title description, use for kill grep, default to `egg-server-${APP_NAME}`. 66 | - `workers` - numbers of app workers, default to `process.env.EGG_WORKERS`, if unset, egg will use `os.cpus().length` as default. 67 | - `daemon` - whether run at background daemon mode, don't use it if in docker mode. 68 | - `framework` - specify framework that can be absolute path or npm package, default to auto detect. 69 | - `env` - server env, default to `process.env.EGG_SERVER_ENV`, recommended to keep empty then use framwork default env. 70 | - `stdout` - customize stdout file, default to `$HOME/logs/master-stdout.log`. 71 | - `stderr` - customize stderr file, default to `$HOME/logs/master-stderr.log`. 72 | - `timeout` - the maximum timeout when app starts, default to 300s. 73 | - `ignore-stderr` - whether ignore stderr when app starts. 74 | - `sourcemap` / `typescript` / `ts` - provides source map support for stack traces. 75 | - `node` - customize node command path, default will find node from $PATH 76 | 77 | ### stop 78 | 79 | Stop egg gracefull. 80 | 81 | **Note:** if exec without `--title`, it will kill all egg process. 82 | 83 | ```bash 84 | $ eggctl stop [options] 85 | 86 | # Usage 87 | # eggctl stop --title=example 88 | ``` 89 | 90 | - **Options** 91 | - `title` - process title description, use for kill grep. 92 | - `timeout` - the maximum timeout when app stop, default to 5s. 93 | 94 | ## Options in `package.json` 95 | 96 | In addition to the command line specification, options can also be specified in `package.json`. However, the command line designation takes precedence. 97 | 98 | ```js 99 | { 100 | "eggScriptsConfig": { 101 | "port": 1234, 102 | "ignore-stderr": true, 103 | // will pass as `node --max-http-header-size=20000` 104 | "node-options--max-http-header-size": "20000", 105 | // will pass as `node --allow-wasi` 106 | "node-options--allow-wasi": true 107 | } 108 | } 109 | ``` 110 | 111 | ## Questions & Suggestions 112 | 113 | Please open an issue [here](https://github.com/eggjs/egg/issues?q=is%3Aissue+is%3Aopen+sort%3Aupdated-desc). 114 | 115 | ## License 116 | 117 | [MIT](LICENSE) 118 | 119 | ## Contributors 120 | 121 | [![Contributors](https://contrib.rocks/image?repo=eggjs/scripts)](https://github.com/eggjs/scripts/graphs/contributors) 122 | 123 | Made with [contributors-img](https://contrib.rocks). 124 | -------------------------------------------------------------------------------- /test/ts.test.ts: -------------------------------------------------------------------------------- 1 | import path from 'node:path'; 2 | import { fileURLToPath } from 'node:url'; 3 | import { strict as assert } from 'node:assert'; 4 | import fs from 'node:fs/promises'; 5 | import cp from 'node:child_process'; 6 | import { scheduler } from 'node:timers/promises'; 7 | import coffee from 'coffee'; 8 | import { request } from 'urllib'; 9 | import { mm, restore } from 'mm'; 10 | import { cleanup, replaceWeakRefMessage, Coffee } from './utils.js'; 11 | import { isWindows, getSourceFilename } from '../src/helper.js'; 12 | 13 | const __filename = fileURLToPath(import.meta.url); 14 | const __dirname = path.dirname(__filename); 15 | 16 | describe('test/ts.test.ts', () => { 17 | const eggBin = getSourceFilename('../bin/run.js'); 18 | const homePath = path.join(__dirname, 'fixtures/home'); 19 | const waitTime = 5000; 20 | let fixturePath: string; 21 | 22 | beforeEach(() => mm(process.env, 'MOCK_HOME_DIR', homePath)); 23 | afterEach(restore); 24 | 25 | before(() => fs.mkdir(homePath, { recursive: true })); 26 | after(() => fs.rm(homePath, { recursive: true, force: true })); 27 | 28 | describe('should display correct stack traces', () => { 29 | let app: Coffee; 30 | beforeEach(async () => { 31 | fixturePath = path.join(__dirname, 'fixtures/ts'); 32 | await cleanup(fixturePath); 33 | const result = cp.spawnSync('npm', [ 'run', isWindows ? 'windows-build' : 'build' ], { 34 | cwd: fixturePath, 35 | shell: isWindows, 36 | }); 37 | assert.equal(result.stderr.toString(), ''); 38 | }); 39 | 40 | afterEach(async () => { 41 | app && app.proc.kill('SIGTERM'); 42 | await cleanup(fixturePath); 43 | }); 44 | 45 | it('--ts', async () => { 46 | app = coffee.fork(eggBin, [ 'start', '--workers=1', '--ts', fixturePath ]) as Coffee; 47 | // app.debug(); 48 | app.expect('code', 0); 49 | 50 | await scheduler.wait(waitTime); 51 | 52 | assert.equal(replaceWeakRefMessage(app.stderr), ''); 53 | assert.match(app.stdout, /egg started on http:\/\/127\.0\.0\.1:7001/); 54 | const result = await request('http://127.0.0.1:7001', { dataType: 'json' }); 55 | // console.log(result.data); 56 | assert(result.data.stack.includes(path.normalize('app/controller/home.ts:6:13'))); 57 | }); 58 | 59 | it('--typescript', async () => { 60 | app = coffee.fork(eggBin, [ 'start', '--workers=1', '--typescript', fixturePath ]) as Coffee; 61 | // app.debug(); 62 | app.expect('code', 0); 63 | 64 | await scheduler.wait(waitTime); 65 | 66 | assert.equal(replaceWeakRefMessage(app.stderr), ''); 67 | assert.match(app.stdout, /egg started on http:\/\/127\.0\.0\.1:7001/); 68 | const result = await request('http://127.0.0.1:7001', { dataType: 'json' }); 69 | // console.log(result.data); 70 | assert(result.data.stack.includes(path.normalize('app/controller/home.ts:6:13'))); 71 | }); 72 | 73 | it('--sourcemap', async () => { 74 | app = coffee.fork(eggBin, [ 'start', '--workers=1', '--sourcemap', fixturePath ]) as Coffee; 75 | // app.debug(); 76 | app.expect('code', 0); 77 | 78 | await scheduler.wait(waitTime); 79 | 80 | assert.equal(replaceWeakRefMessage(app.stderr), ''); 81 | assert.match(app.stdout, /egg started on http:\/\/127\.0\.0\.1:7001/); 82 | const result = await request('http://127.0.0.1:7001', { dataType: 'json' }); 83 | // console.log(result.data); 84 | assert(result.data.stack.includes(path.normalize('app/controller/home.ts:6:13'))); 85 | }); 86 | }); 87 | 88 | describe('pkg.egg.typescript', () => { 89 | let app: Coffee; 90 | beforeEach(async () => { 91 | fixturePath = path.join(__dirname, 'fixtures/ts-pkg'); 92 | await cleanup(fixturePath); 93 | const result = cp.spawnSync('npm', [ 'run', isWindows ? 'windows-build' : 'build' ], { 94 | cwd: fixturePath, 95 | shell: isWindows, 96 | }); 97 | assert.equal(result.stderr.toString(), ''); 98 | }); 99 | 100 | afterEach(async () => { 101 | app && app.proc.kill('SIGTERM'); 102 | await cleanup(fixturePath); 103 | }); 104 | 105 | it('should got correct stack', async () => { 106 | app = coffee.fork(eggBin, [ 'start', '--workers=1', fixturePath ]) as Coffee; 107 | // app.debug(); 108 | app.expect('code', 0); 109 | 110 | await scheduler.wait(waitTime); 111 | 112 | assert.equal(replaceWeakRefMessage(app.stderr), ''); 113 | assert.match(app.stdout, /egg started on http:\/\/127\.0\.0\.1:7001/); 114 | const result = await request('http://127.0.0.1:7001', { dataType: 'json' }); 115 | console.log(result.data); 116 | assert.match(result.data.stack, /home\.ts:6:13/); 117 | // assert(result.data.stack.includes(path.normalize('app/controller/home.ts:6:13'))); 118 | }); 119 | }); 120 | }); 121 | 122 | -------------------------------------------------------------------------------- /src/commands/stop.ts: -------------------------------------------------------------------------------- 1 | import { debuglog, format } from 'node:util'; 2 | import { scheduler } from 'node:timers/promises'; 3 | import { Args, Flags } from '@oclif/core'; 4 | import { BaseCommand } from '../baseCommand.js'; 5 | import { isWindows, findNodeProcess, NodeProcess, kill } from '../helper.js'; 6 | 7 | const debug = debuglog('@eggjs/scripts/commands/stop'); 8 | 9 | const osRelated = { 10 | titleTemplate: isWindows ? '\\"title\\":\\"%s\\"' : '"title":"%s"', 11 | // node_modules/@eggjs/cluster/dist/commonjs/app_worker.js 12 | // node_modules/@eggjs/cluster/dist/esm/app_worker.js 13 | appWorkerPath: /@eggjs[\/\\]cluster[\/\\]dist[\/\\](commonjs|esm)[\/\\]app_worker\.js/i, 14 | // node_modules/@eggjs/cluster/dist/commonjs/agent_worker.js 15 | // node_modules/@eggjs/cluster/dist/esm/agent_worker.js 16 | agentWorkerPath: /@eggjs[\/\\]cluster[\/\\]dist[\/\\](commonjs|esm)[\/\\]agent_worker\.js/i, 17 | }; 18 | 19 | export default class Stop extends BaseCommand { 20 | static override description = 'Stop server'; 21 | 22 | static override examples = [ 23 | '<%= config.bin %> <%= command.id %>', 24 | ]; 25 | 26 | static override args = { 27 | baseDir: Args.string({ 28 | description: 'directory of application', 29 | required: false, 30 | }), 31 | }; 32 | 33 | static override flags = { 34 | title: Flags.string({ 35 | description: 'process title description, use for kill grep', 36 | }), 37 | timeout: Flags.integer({ 38 | description: 'the maximum timeout(ms) when app stop', 39 | default: 5000, 40 | }), 41 | }; 42 | 43 | public async run(): Promise { 44 | const { flags } = this; 45 | 46 | this.log(`stopping egg application${flags.title ? ` with --title=${flags.title}` : ''}`); 47 | 48 | // node ~/eggjs/scripts/scripts/start-cluster.cjs {"title":"egg-server","workers":4,"port":7001,"baseDir":"~/eggjs/test/showcase","framework":"~/eggjs/test/showcase/node_modules/egg"} 49 | let processList = await this.findNodeProcesses(item => { 50 | const cmd = item.cmd; 51 | const matched = flags.title ? 52 | cmd.includes('start-cluster') && cmd.includes(format(osRelated.titleTemplate, flags.title)) : 53 | cmd.includes('start-cluster'); 54 | if (matched) { 55 | debug('find master process: %o', item); 56 | } 57 | return matched; 58 | }); 59 | let pids = processList.map(x => x.pid); 60 | 61 | if (pids.length) { 62 | this.log('got master pid %j, list:', pids); 63 | this.log(''); 64 | for (const item of processList) { 65 | this.log('- %s: %o', item.pid, item.cmd); 66 | } 67 | this.log(''); 68 | this.killProcesses(pids); 69 | // wait for 5s to confirm whether any worker process did not kill by master 70 | await scheduler.wait(flags.timeout); 71 | } else { 72 | this.logToStderr('can\'t detect any running egg process'); 73 | } 74 | 75 | // node --debug-port=5856 /Users/tz/Workspaces/eggjs/test/showcase/node_modules/_egg-cluster@1.8.0@egg-cluster/lib/agent_worker.js {"framework":"/Users/tz/Workspaces/eggjs/test/showcase/node_modules/egg","baseDir":"/Users/tz/Workspaces/eggjs/test/showcase","port":7001,"workers":2,"plugins":null,"https":false,"key":"","cert":"","title":"egg-server","clusterPort":52406} 76 | // node /Users/tz/Workspaces/eggjs/test/showcase/node_modules/_egg-cluster@1.8.0@egg-cluster/lib/app_worker.js {"framework":"/Users/tz/Workspaces/eggjs/test/showcase/node_modules/egg","baseDir":"/Users/tz/Workspaces/eggjs/test/showcase","port":7001,"workers":2,"plugins":null,"https":false,"key":"","cert":"","title":"egg-server","clusterPort":52406} 77 | // ~/bin/node --no-deprecation --trace-warnings ~/eggjs/examples/helloworld/node_modules/@eggjs/cluster/dist/commonjs/agent_worker.js {"baseDir":"~/eggjs/examples/helloworld","startMode":"process","framework":"~/eggjs/examples/helloworld/node_modules/egg","title":"egg-server-helloworld","workers":10,"clusterPort":58977} 78 | processList = await this.findNodeProcesses(item => { 79 | const cmd = item.cmd; 80 | const matched = flags.title ? 81 | (osRelated.appWorkerPath.test(cmd) || osRelated.agentWorkerPath.test(cmd)) && cmd.includes(format(osRelated.titleTemplate, flags.title)) : 82 | (osRelated.appWorkerPath.test(cmd) || osRelated.agentWorkerPath.test(cmd)); 83 | if (matched) { 84 | debug('find app/agent worker process: %o', item); 85 | } 86 | return matched; 87 | }); 88 | pids = processList.map(x => x.pid); 89 | 90 | if (pids.length) { 91 | this.log('got worker/agent pids %j that is not killed by master', pids); 92 | this.killProcesses(pids); 93 | } 94 | 95 | this.log('stopped'); 96 | } 97 | 98 | protected async findNodeProcesses(filter: (item: NodeProcess) => boolean): Promise { 99 | return findNodeProcess(filter); 100 | } 101 | 102 | protected killProcesses(pids: number[], signal: NodeJS.Signals = 'SIGTERM') { 103 | kill(pids, signal); 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | ## [4.0.0](https://github.com/eggjs/scripts/compare/v3.1.0...v4.0.0) (2024-12-31) 4 | 5 | 6 | ### ⚠ BREAKING CHANGES 7 | 8 | * drop Node.js < 18.19.0 support 9 | 10 | part of https://github.com/eggjs/egg/issues/3644 11 | 12 | https://github.com/eggjs/egg/issues/5257 13 | 14 | 15 | 17 | ## Summary by CodeRabbit 18 | 19 | ## Release Notes 20 | 21 | - **New Features** 22 | - Added support for ECMAScript modules (ESM). 23 | - Enhanced CLI with more robust start and stop commands. 24 | - Improved TypeScript integration and type safety. 25 | - Introduced new commands for stopping an Egg.js server application. 26 | - Added new configuration options for logging and process management. 27 | 28 | - **Improvements** 29 | - Updated package configuration for better modularity. 30 | - Modernized test infrastructure with TypeScript support. 31 | - Refined error handling and logging mechanisms. 32 | - Enhanced process management capabilities. 33 | - Improved documentation with updated installation instructions and 34 | usage examples. 35 | 36 | - **Breaking Changes** 37 | - Renamed package from `egg-scripts` to `@eggjs/scripts`. 38 | - Requires Node.js version 18.19.0 or higher. 39 | - Significant changes to project structure and module exports. 40 | 41 | - **Bug Fixes** 42 | - Improved process management for server start and stop operations. 43 | - Enhanced cross-platform compatibility. 44 | - Fixed issues with asynchronous route handlers in various applications. 45 | 46 | 47 | ### Features 48 | 49 | * support cjs and esm both by tshy ([#63](https://github.com/eggjs/scripts/issues/63)) ([d9d0bc6](https://github.com/eggjs/scripts/commit/d9d0bc65aefd1d73be2c40b86366af566cf471c1)) 50 | 51 | ## [3.1.0](https://github.com/eggjs/egg-scripts/compare/v3.0.1...v3.1.0) (2024-12-10) 52 | 53 | 54 | ### Features 55 | 56 | * use runscript v2 ([#61](https://github.com/eggjs/egg-scripts/issues/61)) ([ebbb7f6](https://github.com/eggjs/egg-scripts/commit/ebbb7f60446a2bf5ec8eaac40c85c6224dd91c9d)) 57 | 58 | ## [3.0.1](https://github.com/eggjs/egg-scripts/compare/v3.0.0...v3.0.1) (2024-05-11) 59 | 60 | 61 | ### Bug Fixes 62 | 63 | * head 100 stderr when startup failed ([#59](https://github.com/eggjs/egg-scripts/issues/59)) ([7f2cecf](https://github.com/eggjs/egg-scripts/commit/7f2cecfeb68f07e9b69871f77b66f8a221c51b90)) 64 | 65 | ## [3.0.0](https://github.com/eggjs/egg-scripts/compare/v2.17.0...v3.0.0) (2024-02-19) 66 | 67 | 68 | ### ⚠ BREAKING CHANGES 69 | 70 | * drop Node.js 14 support 71 | 72 | ### Features 73 | 74 | * support configure egg.revert in package.json ([#58](https://github.com/eggjs/egg-scripts/issues/58)) ([a294691](https://github.com/eggjs/egg-scripts/commit/a29469134293a9dec3a7dd5cf6ce71810e913498)) 75 | 76 | 77 | --- 78 | 79 | 80 | 2.17.0 / 2022-04-28 81 | ================== 82 | 83 | **features** 84 | * [[`47f8e82`](http://github.com/eggjs/egg-scripts/commit/47f8e823e01b74028bf8dee7123fc3f9469fb3b6)] - feat: eggScriptsConfig support node-options (#54) (TZ | 天猪 <>) 85 | 86 | 2.16.0 / 2022-03-27 87 | ================== 88 | 89 | **features** 90 | * [[`bb1ba0a`](http://github.com/eggjs/egg-scripts/commit/bb1ba0a665cab9530639d98f38b76c3c72176f76)] - feat: --trace-warnings (#53) (mansonchor.github.com <>) 91 | 92 | 2.15.3 / 2022-03-08 93 | ================== 94 | 95 | **fixes** 96 | * [[`ef5496d`](http://github.com/eggjs/egg-scripts/commit/ef5496d1838a508a859cd5d77886098d7de8fec5)] - fix: ps-cmd result may be truncated (#52) (W <>) 97 | 98 | **others** 99 | * [[`be89f9d`](http://github.com/eggjs/egg-scripts/commit/be89f9d6bb88810ffa3237deab9e4e0d9c4000c2)] - docs(doc): 修改readme文档中的stop脚本处的描述,并增加示例. (#51) (shuidian <<18842643145@163.com>>) 100 | 101 | 2.15.2 / 2021-11-17 102 | ================== 103 | 104 | **fixes** 105 | * [[`b122d86`](http://github.com/eggjs/egg-scripts/commit/b122d86d300df4018291d8f8d8e98ab813048f67)] - fix: sourcemap default value should respect eggScriptConfig (#50) (killa <>) 106 | 107 | **others** 108 | * [[`78c3284`](http://github.com/eggjs/egg-scripts/commit/78c3284cb68748f4487141f5481d6e44288c9e47)] - test: case for injecting incorrect script (#49) (hyj1991 <>) 109 | 110 | 2.15.1 / 2021-09-15 111 | ================== 112 | 113 | **features** 114 | * [[`1a7f09c`](http://github.com/eggjs/egg-scripts/commit/1a7f09c707becaca42522ee415da0fe5961a6ad5)] - feat: support pkgInfo.eggScriptsConfig.require (#47) (hyj1991 <>) 115 | 116 | **others** 117 | * [[`a68ac67`](http://github.com/eggjs/egg-scripts/commit/a68ac679b0eee4eff19c9e5d40ca80409ddf02eb)] - Revert "feat: support pkgInfo.egg.require (#45)" (#48) (hyj1991 <>) 118 | 119 | 2.15.0 / 2021-09-13 120 | ================== 121 | 122 | **features** 123 | * [[`fe179fd`](http://github.com/eggjs/egg-scripts/commit/fe179fda909cd7eb5b6497357202185a4ecf5ec6)] - feat: support pkgInfo.egg.require (#45) (TZ | 天猪 <>) 124 | 125 | 2.14.0 / 2021-06-11 126 | ================== 127 | 128 | **features** 129 | * [[`f0a342f`](http://github.com/eggjs/egg-scripts/commit/f0a342ffcd3ec1823eb2d42a9dd96c075cea3754)] - feat: --no-deprecation (#44) (TZ | 天猪 <>) 130 | 131 | 2.13.0 / 2020-02-25 132 | ================== 133 | 134 | **features** 135 | * [[`c0ba739`](http://github.com/eggjs/egg-scripts/commit/c0ba73900642e488b0e6306ea028ef547ceedfae)] - feat: support stop timeout (#43) (hui <>) 136 | 137 | 2.12.0 / 2019-12-16 138 | ================== 139 | 140 | **features** 141 | * [[`20483fd`](http://github.com/eggjs/egg-scripts/commit/20483fd56ce51238431fb095ede1c768a99470f2)] - feat: support eggScriptsConfig in package.json (#41) (Yiyu He <>) 142 | 143 | 2.11.1 / 2019-10-10 144 | ================== 145 | 146 | **fixes** 147 | * [[`de61980`](http://github.com/eggjs/egg-scripts/commit/de61980f772c8a24010d3f078658f8c55b072067)] - fix: start command should exit after child process exit when no daemon mode (#39) (killa <>) 148 | 149 | **others** 150 | * [[`7ae9cb0`](http://github.com/eggjs/egg-scripts/commit/7ae9cb054cb91ea7ac1e615e1e3a7fcdaba5f980)] - test: add egg@1 and egg@2 with travis (#36) (Maledong <>) 151 | 152 | 2.11.0 / 2018-12-17 153 | =================== 154 | 155 | * feat(stop): only sleep when master process exists (#34) 156 | * fix: stop process only if the title matches exactly (#35) 157 | 158 | 2.10.0 / 2018-10-10 159 | ================== 160 | 161 | **fixes** 162 | * [[`4768950`](http://github.com/eggjs/egg-scripts/commit/4768950d29398031fd6ae129a981c60e308bff0a)] - fix: replace command by args in ps (#29) (Baffin Lee <>) 163 | 164 | **others** 165 | * [[`f31efb9`](http://github.com/eggjs/egg-scripts/commit/f31efb9133c5edc6176371ca725198f1b43b9aab)] - feat: support customize node path (#32) (Yiyu He <>) 166 | * [[`c2479dc`](http://github.com/eggjs/egg-scripts/commit/c2479dc6416386b654fc6e918a4dbd575cc0639e)] - chore: update version (TZ <>) 167 | 168 | 2.9.1 / 2018-08-24 169 | ================== 170 | 171 | * fix: replace command by args in ps (#29) 172 | 173 | 2.9.0 / 2018-08-23 174 | ================== 175 | 176 | **features** 177 | * [[`1367883`](http://github.com/eggjs/egg-scripts/commit/1367883804e5ab1ece88831ea4d1a934ee757f81)] - feat: add ipc channel in nonDaemon mode (#28) (Khaidi Chu <>) 178 | 179 | **others** 180 | * [[`262ef4c`](http://github.com/eggjs/egg-scripts/commit/262ef4c97179dbf6f8de2eb0547eef4cbc56bf92)] - chore: add license and issues link (#27) (Haoliang Gao <>) 181 | 182 | 2.8.1 / 2018-08-19 183 | ================== 184 | 185 | **fixes** 186 | * [[`b98fd03`](http://github.com/eggjs/egg-scripts/commit/b98fd03d1e3aaed68004b881f0b3d42fe47341dd)] - fix: use execFile instead of exec for security reason (#26) (fengmk2 <>) 187 | 188 | 2.8.0 / 2018-08-10 189 | ================== 190 | 191 | **others** 192 | * [[`dac29f7`](http://github.com/eggjs/egg-scripts/commit/dac29f73ed2dfc18edc2e8743ffd509af8ab0f4a)] - refactor: add `this.exit` to instead of `process.exit` (#25) (Khaidi Chu <>) 193 | 194 | 2.7.0 / 2018-08-10 195 | ================== 196 | 197 | **features** 198 | * [[`22faa4c`](http://github.com/eggjs/egg-scripts/commit/22faa4cfbb84cc5bc819d981dce962d8f95f8357)] - feat: stop command support windows (#22) (Baffin Lee <>) 199 | 200 | **others** 201 | * [[`e07726c`](http://github.com/eggjs/egg-scripts/commit/e07726c176a89dd63482b588868fd1feaab1fba6)] - refactor: raw spawn call to instead of helper.spawn in start non-daemon mode (#23) (Khaidi Chu <>) 202 | 203 | 2.6.0 / 2018-04-03 204 | ================== 205 | 206 | * feat: provides source map support for stack traces (#19) 207 | 208 | 2.5.1 / 2018-02-06 209 | ================== 210 | 211 | * chore: add description for ignore-stderr (#18) 212 | 213 | 2.5.0 / 2017-12-12 214 | ================== 215 | 216 | **features** 217 | * [[`b5559d5`](http://github.com/eggjs/egg-scripts/commit/b5559d54228543b5422047e6f056829df11f8c87)] - feat: support --ignore-error (#17) (TZ | 天猪 <>) 218 | 219 | 2.4.0 / 2017-11-30 220 | ================== 221 | 222 | **features** 223 | * [[`8eda3d1`](https://github.com/eggjs/egg-scripts/commit/8eda3d10cfea5757f220fd82b562fd5fef433440)] - feat: add `${baseDir}/.node/bin` to PATH if exists (#14) (fengmk2 <>) 224 | 225 | **others** 226 | * [[`4dd24a4`](https://github.com/eggjs/egg-scripts/commit/4dd24a45d92b2c2a8e1e450e0f13ba4143550ca9)] - test: add testcase for #12 (#13) (Haoliang Gao <>) 227 | 228 | 2.3.0 / 2017-11-29 229 | ================== 230 | 231 | **features** 232 | * [[`4c41319`](http://github.com/eggjs/egg-scripts/commit/4c41319f9e309402b2ccb5c7afd5a6d3cda2453f)] - feat: support stop --title (#16) (TZ | 天猪 <>) 233 | 234 | 2.2.0 / 2017-11-22 235 | ================== 236 | 237 | **features** 238 | * [[`ac58d00`](http://github.com/eggjs/egg-scripts/commit/ac58d00a974fdfff6b5c722743e4b32174963c52)] - feat: cwd maybe not baseDir (#15) (zhennann <>) 239 | 240 | 2.1.1 / 2017-11-14 241 | ================== 242 | 243 | **fixes** 244 | * [[`7324d99`](http://github.com/eggjs/egg-scripts/commit/7324d99b504cac5fef7dbf280f7d9e6243c16bb7)] - fix: should stop app when baseDir is symlink (#12) (Haoliang Gao <>) 245 | 246 | 2.1.0 / 2017-10-16 247 | ================== 248 | 249 | **features** 250 | * [[`ac40135`](http://github.com/eggjs/egg-scripts/commit/ac40135d5b9a3200ea1bdfdb19d0f7e12d0c511a)] - feat: add eggctl bin (#10) (Haoliang Gao <>) 251 | 252 | 2.0.0 / 2017-10-13 253 | ================== 254 | 255 | **features** 256 | * [[`0f7ca50`](http://github.com/eggjs/egg-scripts/commit/0f7ca502999c06a9cb05d8e5617f6045704511df)] - feat: [BREAKING_CHANGE] check the status of app when start on daemon (#9) (Haoliang Gao <>) 257 | 258 | **others** 259 | * [[`cfd0d2f`](http://github.com/eggjs/egg-scripts/commit/cfd0d2f67845fffb9d5974514b65e43b22ed8040)] - refactor: modify the directory of logDir (#8) (Haoliang Gao <>) 260 | 261 | 1.2.0 / 2017-09-11 262 | ================== 263 | 264 | **features** 265 | * [[`c0300b8`](http://github.com/eggjs/egg-scripts/commit/c0300b8c657fe4f75ca388061f6cb3de9864a743)] - feat: log success at daemon mode (#7) (TZ | 天猪 <>) 266 | 267 | **others** 268 | * [[`fdd273c`](http://github.com/eggjs/egg-scripts/commit/fdd273c2d6f15d104288fef4d699627a7cf701d9)] - test: add cluster-config fixture (#4) (TZ | 天猪 <>) 269 | 270 | 1.1.2 / 2017-09-01 271 | ================== 272 | 273 | * fix: should not pass undefined env (#6) 274 | * docs: fix stop typo (#5) 275 | 276 | 1.1.1 / 2017-08-29 277 | ================== 278 | 279 | * fix: should set title correct (#3) 280 | 281 | 1.1.0 / 2017-08-16 282 | ================== 283 | 284 | * feat: remove env default value (#2) 285 | 286 | 1.0.0 / 2017-08-02 287 | ================== 288 | 289 | * feat: first implementation (#1) 290 | -------------------------------------------------------------------------------- /src/commands/start.ts: -------------------------------------------------------------------------------- 1 | import { debuglog, promisify } from 'node:util'; 2 | import path from 'node:path'; 3 | import { scheduler } from 'node:timers/promises'; 4 | import { spawn, SpawnOptions, ChildProcess, execFile as _execFile } from 'node:child_process'; 5 | import { mkdir, rename, stat, open } from 'node:fs/promises'; 6 | import { homedir } from 'node-homedir'; 7 | import { Args, Flags } from '@oclif/core'; 8 | import { getFrameworkPath, importResolve } from '@eggjs/utils'; 9 | import { readJSON, exists, getDateStringParts } from 'utility'; 10 | import { BaseCommand } from '../baseCommand.js'; 11 | import { getSourceDirname } from '../helper.js'; 12 | 13 | const debug = debuglog('@eggjs/scripts/commands/start'); 14 | 15 | const execFile = promisify(_execFile); 16 | 17 | export interface FrameworkOptions { 18 | baseDir: string; 19 | framework?: string; 20 | } 21 | 22 | export default class Start extends BaseCommand { 23 | static override description = 'Start server at prod mode'; 24 | 25 | static override examples = [ 26 | '<%= config.bin %> <%= command.id %>', 27 | ]; 28 | 29 | static override args = { 30 | baseDir: Args.string({ 31 | description: 'directory of application', 32 | required: false, 33 | }), 34 | }; 35 | 36 | static override flags = { 37 | title: Flags.string({ 38 | description: 'process title description, use for kill grep, default to `egg-server-${APP_NAME}`', 39 | }), 40 | framework: Flags.string({ 41 | description: 'specify framework that can be absolute path or npm package', 42 | }), 43 | port: Flags.integer({ 44 | description: 'listening port, default to `process.env.PORT`', 45 | char: 'p', 46 | }), 47 | workers: Flags.integer({ 48 | char: 'c', 49 | aliases: [ 'cluster' ], 50 | description: 'numbers of app workers, default to `process.env.EGG_WORKERS` or `os.cpus().length`', 51 | }), 52 | env: Flags.string({ 53 | description: 'server env, default to `process.env.EGG_SERVER_ENV`', 54 | default: process.env.EGG_SERVER_ENV, 55 | }), 56 | daemon: Flags.boolean({ 57 | description: 'whether run at background daemon mode', 58 | }), 59 | stdout: Flags.string({ 60 | description: 'customize stdout file', 61 | }), 62 | stderr: Flags.string({ 63 | description: 'customize stderr file', 64 | }), 65 | timeout: Flags.integer({ 66 | description: 'the maximum timeout(ms) when app starts', 67 | default: 300 * 1000, 68 | }), 69 | 'ignore-stderr': Flags.boolean({ 70 | description: 'whether ignore stderr when app starts', 71 | }), 72 | node: Flags.string({ 73 | description: 'customize node command path', 74 | default: 'node', 75 | }), 76 | require: Flags.string({ 77 | summary: 'require the given module', 78 | char: 'r', 79 | multiple: true, 80 | }), 81 | sourcemap: Flags.boolean({ 82 | summary: 'whether enable sourcemap support, will load `source-map-support` etc', 83 | aliases: [ 'ts', 'typescript' ], 84 | }), 85 | }; 86 | 87 | isReady = false; 88 | #child: ChildProcess; 89 | 90 | protected async getFrameworkPath(options: FrameworkOptions) { 91 | return getFrameworkPath(options); 92 | } 93 | 94 | protected async getFrameworkName(frameworkPath: string) { 95 | const pkgPath = path.join(frameworkPath, 'package.json'); 96 | let name = 'egg'; 97 | try { 98 | const pkg = await readJSON(pkgPath); 99 | if (pkg.name) { 100 | name = pkg.name; 101 | } 102 | } catch { 103 | // ignore 104 | } 105 | return name; 106 | } 107 | 108 | protected async getServerBin() { 109 | const serverBinName = this.isESM ? 'start-cluster.mjs' : 'start-cluster.cjs'; 110 | // for src paths, `./src/commands/start.js` 111 | let serverBin = path.join(getSourceDirname(), '../scripts', serverBinName); 112 | if (!(await exists(serverBin))) { 113 | // for dist paths, `./dist/esm/commands/start.js` 114 | serverBin = path.join(getSourceDirname(), '../../scripts', serverBinName); 115 | } 116 | return serverBin; 117 | } 118 | 119 | public async run(): Promise { 120 | const { args, flags } = this; 121 | // context.execArgvObj = context.execArgvObj || {}; 122 | // const { argv, env, cwd, execArgvObj } = context; 123 | const HOME = homedir(); 124 | const logDir = path.join(HOME, 'logs'); 125 | 126 | // eggctl start 127 | // eggctl start ./server 128 | // eggctl start /opt/app 129 | const cwd = process.cwd(); 130 | let baseDir = args.baseDir || cwd; 131 | if (!path.isAbsolute(baseDir)) { 132 | baseDir = path.join(cwd, baseDir); 133 | } 134 | await this.initBaseInfo(baseDir); 135 | 136 | flags.framework = await this.getFrameworkPath({ 137 | framework: flags.framework, 138 | baseDir, 139 | }); 140 | 141 | const frameworkName = await this.getFrameworkName(flags.framework); 142 | 143 | flags.title = flags.title || `egg-server-${this.pkg.name}`; 144 | 145 | flags.stdout = flags.stdout || path.join(logDir, 'master-stdout.log'); 146 | flags.stderr = flags.stderr || path.join(logDir, 'master-stderr.log'); 147 | 148 | if (flags.workers === undefined && process.env.EGG_WORKERS) { 149 | flags.workers = Number(process.env.EGG_WORKERS); 150 | } 151 | 152 | // normalize env 153 | this.env.HOME = HOME; 154 | this.env.NODE_ENV = 'production'; 155 | 156 | // it makes env big but more robust 157 | this.env.PATH = this.env.Path = [ 158 | // for nodeinstall 159 | path.join(baseDir, 'node_modules/.bin'), 160 | // support `.node/bin`, due to npm5 will remove `node_modules/.bin` 161 | path.join(baseDir, '.node/bin'), 162 | // adjust env for win 163 | this.env.PATH || this.env.Path, 164 | ].filter(x => !!x).join(path.delimiter); 165 | 166 | // for alinode 167 | this.env.ENABLE_NODE_LOG = 'YES'; 168 | this.env.NODE_LOG_DIR = this.env.NODE_LOG_DIR || path.join(logDir, 'alinode'); 169 | await mkdir(this.env.NODE_LOG_DIR, { recursive: true }); 170 | 171 | // cli argv -> process.env.EGG_SERVER_ENV -> `undefined` then egg will use `prod` 172 | if (flags.env) { 173 | // if undefined, should not pass key due to `spawn`, https://github.com/nodejs/node/blob/master/lib/child_process.js#L470 174 | this.env.EGG_SERVER_ENV = flags.env; 175 | } 176 | 177 | // additional execArgv 178 | const execArgv: string[] = [ 179 | '--no-deprecation', 180 | '--trace-warnings', 181 | ]; 182 | if (this.pkgEgg.revert) { 183 | const reverts = Array.isArray(this.pkgEgg.revert) ? this.pkgEgg.revert : [ this.pkgEgg.revert ]; 184 | for (const revert of reverts) { 185 | execArgv.push(`--security-revert=${revert}`); 186 | } 187 | } 188 | 189 | // pkg.eggScriptsConfig.require 190 | const scriptsConfig: Record = this.pkg.eggScriptsConfig; 191 | if (scriptsConfig?.require) { 192 | scriptsConfig.require = Array.isArray(scriptsConfig.require) ? scriptsConfig.require : [ scriptsConfig.require ]; 193 | flags.require = [ ...scriptsConfig.require, ...(flags.require ?? []) ]; 194 | } 195 | 196 | // read argv from eggScriptsConfig in package.json 197 | if (scriptsConfig) { 198 | for (const key in scriptsConfig) { 199 | const v = scriptsConfig[key]; 200 | if (key.startsWith('node-options--')) { 201 | const newKey = key.replace('node-options--', ''); 202 | if (v === true) { 203 | // "node-options--allow-wasi": true 204 | // => --allow-wasi 205 | execArgv.push(`--${newKey}`); 206 | } else { 207 | // "node-options--max-http-header-size": "20000" 208 | // => --max-http-header-size=20000 209 | execArgv.push(`--${newKey}=${v}`); 210 | } 211 | continue; 212 | } 213 | const existsValue = Reflect.get(flags, key); 214 | if (existsValue === undefined) { 215 | // only set if key is not pass from command line 216 | Reflect.set(flags, key, v); 217 | } 218 | } 219 | } 220 | 221 | // read `egg.typescript` from package.json 222 | if (this.pkgEgg.typescript && flags.sourcemap === undefined) { 223 | flags.sourcemap = true; 224 | } 225 | if (flags.sourcemap) { 226 | const sourceMapSupport = importResolve('source-map-support/register.js', { 227 | paths: [ getSourceDirname() ], 228 | }); 229 | if (this.isESM) { 230 | execArgv.push('--import', sourceMapSupport); 231 | } else { 232 | execArgv.push('--require', sourceMapSupport); 233 | } 234 | } 235 | 236 | if (flags.port === undefined && process.env.PORT) { 237 | flags.port = parseInt(process.env.PORT); 238 | } 239 | 240 | debug('flags: %o, framework: %o, baseDir: %o, execArgv: %o', 241 | flags, frameworkName, baseDir, execArgv); 242 | 243 | const command = flags.node; 244 | const options: SpawnOptions = { 245 | env: this.env, 246 | stdio: 'inherit', 247 | detached: false, 248 | cwd: baseDir, 249 | }; 250 | 251 | this.log('Starting %s application at %s', frameworkName, baseDir); 252 | 253 | // remove unused properties from stringify, alias had been remove by `removeAlias` 254 | const ignoreKeys = [ 'env', 'daemon', 'stdout', 'stderr', 'timeout', 'ignore-stderr', 'node' ]; 255 | const clusterOptions = stringify({ 256 | ...flags, 257 | baseDir, 258 | }, ignoreKeys); 259 | // Note: `spawn` is not like `fork`, had to pass `execArgv` yourself 260 | const serverBin = await this.getServerBin(); 261 | const eggArgs = [ ...execArgv, serverBin, clusterOptions, `--title=${flags.title}` ]; 262 | const spawnScript = `${command} ${eggArgs.map(a => `'${a}'`).join(' ')}`; 263 | this.log('Spawn %o', spawnScript); 264 | 265 | // whether run in the background. 266 | if (flags.daemon) { 267 | this.log(`Save log file to ${logDir}`); 268 | const [ stdout, stderr ] = await Promise.all([ 269 | getRotateLog(flags.stdout), 270 | getRotateLog(flags.stderr), 271 | ]); 272 | options.stdio = [ 'ignore', stdout, stderr, 'ipc' ]; 273 | options.detached = true; 274 | const child = this.#child = spawn(command, eggArgs, options); 275 | this.isReady = false; 276 | child.on('message', (msg: any) => { 277 | // https://github.com/eggjs/cluster/blob/master/src/master.ts#L119 278 | if (msg && msg.action === 'egg-ready') { 279 | this.isReady = true; 280 | this.log('%s started on %s', frameworkName, msg.data.address); 281 | child.unref(); 282 | child.disconnect(); 283 | } 284 | }); 285 | 286 | // check start status 287 | await this.checkStatus(); 288 | } else { 289 | options.stdio = [ 'inherit', 'inherit', 'inherit', 'ipc' ]; 290 | const child = this.#child = spawn(command, eggArgs, options); 291 | child.once('exit', code => { 292 | if (!code) return; 293 | // command should exit after child process exit 294 | this.exit(code); 295 | }); 296 | 297 | // attach master signal to child 298 | let signal; 299 | const signals = [ 'SIGINT', 'SIGQUIT', 'SIGTERM' ] as NodeJS.Signals[]; 300 | signals.forEach(event => { 301 | process.once(event, () => { 302 | debug('Kill child %s with %s', child.pid, signal); 303 | child.kill(event); 304 | }); 305 | }); 306 | } 307 | } 308 | 309 | protected async checkStatus() { 310 | let count = 0; 311 | let hasError = false; 312 | let isSuccess = true; 313 | const timeout = this.flags.timeout / 1000; 314 | const stderrFile = this.flags.stderr!; 315 | while (!this.isReady) { 316 | try { 317 | const stats = await stat(stderrFile); 318 | if (stats && stats.size > 0) { 319 | hasError = true; 320 | break; 321 | } 322 | } catch (_) { 323 | // nothing 324 | } 325 | 326 | if (count >= timeout) { 327 | this.logToStderr('Start failed, %ds timeout', timeout); 328 | isSuccess = false; 329 | break; 330 | } 331 | 332 | await scheduler.wait(1000); 333 | this.log('Wait Start: %d...', ++count); 334 | } 335 | 336 | if (hasError) { 337 | try { 338 | const args = [ '-n', '100', stderrFile ]; 339 | this.logToStderr('tail %s', args.join(' ')); 340 | const { stdout: headStdout } = await execFile('head', args); 341 | const { stdout: tailStdout } = await execFile('tail', args); 342 | this.logToStderr('Got error when startup: '); 343 | this.logToStderr(headStdout); 344 | this.logToStderr('...'); 345 | this.logToStderr(tailStdout); 346 | } catch (err) { 347 | this.logToStderr('ignore tail error: %s', err); 348 | } 349 | isSuccess = this.flags['ignore-stderr']; 350 | this.logToStderr('Start got error, see %o', stderrFile); 351 | this.logToStderr('Or use `--ignore-stderr` to ignore stderr at startup.'); 352 | } 353 | 354 | if (!isSuccess) { 355 | this.#child.kill('SIGTERM'); 356 | await scheduler.wait(1000); 357 | this.exit(1); 358 | } 359 | } 360 | } 361 | 362 | function stringify(obj: Record, ignore: string[]) { 363 | const result: Record = {}; 364 | Object.keys(obj).forEach(key => { 365 | if (!ignore.includes(key)) { 366 | result[key] = obj[key]; 367 | } 368 | }); 369 | return JSON.stringify(result); 370 | } 371 | 372 | async function getRotateLog(logFile: string) { 373 | await mkdir(path.dirname(logFile), { recursive: true }); 374 | 375 | if (await exists(logFile)) { 376 | // format style: .20150602.193100 377 | const [ YYYY, MM, DD, HH, mm, ss ] = getDateStringParts(); 378 | const timestamp = `.${YYYY}${MM}${DD}.${HH}${mm}${ss}`; 379 | // Note: rename last log to next start time, not when last log file created 380 | await rename(logFile, logFile + timestamp); 381 | } 382 | 383 | return (await open(logFile, 'a')).fd; 384 | } 385 | -------------------------------------------------------------------------------- /test/stop.test.ts: -------------------------------------------------------------------------------- 1 | import path from 'node:path'; 2 | import { fileURLToPath } from 'node:url'; 3 | import { strict as assert } from 'node:assert'; 4 | import fs from 'node:fs/promises'; 5 | import { scheduler } from 'node:timers/promises'; 6 | import coffee from 'coffee'; 7 | import { request } from 'urllib'; 8 | import { mm, restore } from 'mm'; 9 | import { cleanup, replaceWeakRefMessage, Coffee } from './utils.js'; 10 | import { isWindows, getSourceFilename } from '../src/helper.js'; 11 | 12 | const __filename = fileURLToPath(import.meta.url); 13 | const __dirname = path.dirname(__filename); 14 | 15 | describe('test/stop.test.ts', () => { 16 | const eggBin = getSourceFilename('../bin/run.js'); 17 | const fixturePath = path.join(__dirname, 'fixtures/example'); 18 | const timeoutPath = path.join(__dirname, 'fixtures/stop-timeout'); 19 | const homePath = path.join(__dirname, 'fixtures/home'); 20 | const logDir = path.join(homePath, 'logs'); 21 | const waitTime = 10000; 22 | 23 | before(async () => { 24 | await fs.mkdir(homePath, { recursive: true }); 25 | }); 26 | after(async () => { 27 | await fs.rm(homePath, { force: true, recursive: true }); 28 | }); 29 | beforeEach(() => mm(process.env, 'MOCK_HOME_DIR', homePath)); 30 | afterEach(restore); 31 | 32 | describe('stop without daemon', () => { 33 | let app: Coffee; 34 | let killer: Coffee; 35 | 36 | beforeEach(async () => { 37 | await cleanup(fixturePath); 38 | app = coffee.fork(eggBin, [ 'start', '--workers=2', fixturePath ]) as Coffee; 39 | // app.debug(); 40 | app.expect('code', 0); 41 | await scheduler.wait(waitTime); 42 | 43 | assert.equal(replaceWeakRefMessage(app.stderr), ''); 44 | assert(app.stdout.match(/custom-framework started on http:\/\/127\.0\.0\.1:7001/)); 45 | const result = await request('http://127.0.0.1:7001'); 46 | assert.equal(result.data.toString(), 'hi, egg'); 47 | }); 48 | 49 | afterEach(async () => { 50 | app.proc.kill('SIGTERM'); 51 | await cleanup(fixturePath); 52 | }); 53 | 54 | it('should stop', async () => { 55 | killer = coffee.fork(eggBin, [ 'stop', fixturePath ]) as Coffee; 56 | // killer.debug(); 57 | killer.expect('code', 0); 58 | await killer.end(); 59 | 60 | // make sure is kill not auto exist 61 | assert.doesNotMatch(app.stdout, /exist by env/); 62 | 63 | // no way to handle the SIGTERM signal in windows ? 64 | if (!isWindows) { 65 | assert.match(app.stdout, /\[master] master is killed by signal SIGTERM, closing/); 66 | assert.match(app.stdout, /\[master] exit with code:0/); 67 | assert.match(app.stdout, /\[app_worker] exit with code:0/); 68 | // assert(app.stdout.includes('[agent_worker] exit with code:0')); 69 | } 70 | 71 | assert.match(killer.stdout, /stopping egg application/); 72 | assert.match(killer.stdout, /got master pid \[\d+\]/); 73 | }); 74 | }); 75 | 76 | describe('stop with daemon', () => { 77 | beforeEach(async () => { 78 | await cleanup(fixturePath); 79 | await fs.rm(logDir, { force: true, recursive: true }); 80 | await coffee.fork(eggBin, [ 'start', '--daemon', '--workers=2', fixturePath ]) 81 | // .debug() 82 | .expect('code', 0) 83 | .end(); 84 | 85 | const result = await request('http://127.0.0.1:7001'); 86 | assert(result.data.toString() === 'hi, egg'); 87 | }); 88 | afterEach(async () => { 89 | await cleanup(fixturePath); 90 | }); 91 | 92 | it('should stop', async () => { 93 | await coffee.fork(eggBin, [ 'stop', fixturePath ]) 94 | .debug() 95 | .expect('stdout', /stopping egg application/) 96 | .expect('stdout', /got master pid \[\d+\]/i) 97 | .expect('code', 0) 98 | .end(); 99 | 100 | // master log 101 | const stdout = await fs.readFile(path.join(logDir, 'master-stdout.log'), 'utf-8'); 102 | 103 | // no way to handle the SIGTERM signal in windows ? 104 | if (!isWindows) { 105 | assert.match(stdout, /\[master] master is killed by signal SIGTERM, closing/); 106 | assert.match(stdout, /\[master] exit with code:0/); 107 | assert.match(stdout, /\[app_worker] exit with code:0/); 108 | } 109 | 110 | await coffee.fork(eggBin, [ 'stop', fixturePath ]) 111 | .debug() 112 | .expect('stderr', /can't detect any running egg process/) 113 | .expect('code', 0) 114 | .end(); 115 | }); 116 | }); 117 | 118 | describe('stop with not exist', () => { 119 | it('should work', async () => { 120 | await cleanup(fixturePath); 121 | await coffee.fork(eggBin, [ 'stop', fixturePath ]) 122 | // .debug() 123 | .expect('stdout', /stopping egg application/) 124 | .expect('stderr', /can't detect any running egg process/) 125 | .expect('code', 0) 126 | .end(); 127 | }); 128 | }); 129 | 130 | describe('stop --title', () => { 131 | let app: Coffee; 132 | let killer: Coffee; 133 | 134 | beforeEach(async () => { 135 | await cleanup(fixturePath); 136 | app = coffee.fork(eggBin, [ 'start', '--workers=2', '--title=example', fixturePath ]) as Coffee; 137 | // app.debug(); 138 | app.expect('code', 0); 139 | await scheduler.wait(waitTime); 140 | 141 | assert.equal(replaceWeakRefMessage(app.stderr), ''); 142 | assert.match(app.stdout, /custom-framework started on http:\/\/127\.0\.0\.1:7001/); 143 | const result = await request('http://127.0.0.1:7001'); 144 | assert(result.data.toString() === 'hi, egg'); 145 | }); 146 | 147 | afterEach(async () => { 148 | app.proc.kill('SIGTERM'); 149 | await cleanup(fixturePath); 150 | }); 151 | 152 | it('should stop only if the title matches exactly', async () => { 153 | // Because of'exmaple'.inclues('exmap') === true,if egg-scripts <= 2.1.0 and you run `.. stop --title=exmap`,the process with 'title:example' will also be killed unexpectedly 154 | await coffee.fork(eggBin, [ 'stop', '--title=examp', fixturePath ]) 155 | // .debug() 156 | .expect('stdout', /stopping egg application with --title=examp/) 157 | .expect('stderr', /can't detect any running egg process/) 158 | .expect('code', 0) 159 | .end(); 160 | 161 | // stop only if the title matches exactly 162 | await coffee.fork(eggBin, [ 'stop', '--title=example', fixturePath ]) 163 | // .debug() 164 | .expect('stdout', /stopping egg application with --title=example/) 165 | .expect('stdout', /got master pid \[/) 166 | .expect('code', 0) 167 | .end(); 168 | }); 169 | 170 | it('should stop', async () => { 171 | await coffee.fork(eggBin, [ 'stop', '--title=random', fixturePath ]) 172 | .debug() 173 | .expect('stdout', /stopping egg application with --title=random/) 174 | .expect('stderr', /can't detect any running egg process/) 175 | .expect('code', 0) 176 | .end(); 177 | 178 | killer = coffee.fork(eggBin, [ 'stop', '--title=example' ], { cwd: fixturePath }) as Coffee; 179 | killer.debug(); 180 | // killer.expect('code', 0); 181 | await killer.end(); 182 | 183 | // make sure is kill not auto exist 184 | assert.doesNotMatch(app.stdout, /exist by env/); 185 | 186 | // no way to handle the SIGTERM signal in windows ? 187 | if (!isWindows) { 188 | assert(app.stdout.includes('[master] master is killed by signal SIGTERM, closing')); 189 | assert(app.stdout.includes('[master] exit with code:0')); 190 | assert(app.stdout.includes('[app_worker] exit with code:0')); 191 | // assert(app.stdout.includes('[agent_worker] exit with code:0')); 192 | } 193 | 194 | assert(killer.stdout.includes('stopping egg application with --title=example')); 195 | assert(killer.stdout.match(/got master pid \[\d+\]/i)); 196 | }); 197 | }); 198 | 199 | describe('stop all', () => { 200 | let app: Coffee; 201 | let app2: Coffee; 202 | let killer: Coffee; 203 | 204 | beforeEach(async () => { 205 | await cleanup(fixturePath); 206 | app = coffee.fork(eggBin, [ 'start', '--workers=2', '--title=example', fixturePath ]) as Coffee; 207 | app.debug(); 208 | app.expect('code', 0); 209 | 210 | app2 = coffee.fork(eggBin, [ 'start', '--workers=2', '--title=test', '--port=7002', fixturePath ]) as Coffee; 211 | app2.expect('code', 0); 212 | 213 | await scheduler.wait(10000); 214 | 215 | assert.equal(replaceWeakRefMessage(app.stderr), ''); 216 | assert.match(app.stdout, /custom-framework started on http:\/\/127\.0\.0\.1:7001/); 217 | const result = await request('http://127.0.0.1:7001'); 218 | assert.equal(result.data.toString(), 'hi, egg'); 219 | 220 | assert.equal(replaceWeakRefMessage(app2.stderr), ''); 221 | assert.match(app2.stdout, /custom-framework started on http:\/\/127\.0\.0\.1:7002/); 222 | const result2 = await request('http://127.0.0.1:7002'); 223 | assert.equal(result2.data.toString(), 'hi, egg'); 224 | }); 225 | 226 | afterEach(async () => { 227 | app.proc.kill('SIGTERM'); 228 | app2.proc.kill('SIGTERM'); 229 | await cleanup(fixturePath); 230 | }); 231 | 232 | it('should stop', async () => { 233 | killer = coffee.fork(eggBin, [ 'stop' ], { cwd: fixturePath }) as Coffee; 234 | killer.debug(); 235 | // killer.expect('code', 0); 236 | await killer.end(); 237 | 238 | // make sure is kill not auto exist 239 | assert(!app.stdout.includes('exist by env')); 240 | 241 | // no way to handle the SIGTERM signal in windows ? 242 | if (!isWindows) { 243 | assert(app.stdout.includes('[master] master is killed by signal SIGTERM, closing')); 244 | assert(app.stdout.includes('[master] exit with code:0')); 245 | assert(app.stdout.includes('[app_worker] exit with code:0')); 246 | // assert(app.stdout.includes('[agent_worker] exit with code:0')); 247 | } 248 | 249 | assert(killer.stdout.includes('stopping egg application')); 250 | assert(killer.stdout.match(/got master pid \[\d+,\d+\]/i)); 251 | 252 | assert(!app2.stdout.includes('exist by env')); 253 | 254 | // no way to handle the SIGTERM signal in windows ? 255 | if (!isWindows) { 256 | assert(app2.stdout.includes('[master] master is killed by signal SIGTERM, closing')); 257 | assert(app2.stdout.includes('[master] exit with code:0')); 258 | assert(app2.stdout.includes('[app_worker] exit with code:0')); 259 | } 260 | }); 261 | }); 262 | 263 | describe('stop all with timeout', function() { 264 | let app: Coffee; 265 | let killer: Coffee; 266 | this.timeout(17000); 267 | beforeEach(async () => { 268 | await cleanup(timeoutPath); 269 | app = coffee.fork(eggBin, [ 'start', '--workers=2', '--title=stop-timeout', timeoutPath ]) as Coffee; 270 | // app.debug(); 271 | app.expect('code', 0); 272 | 273 | await scheduler.wait(waitTime); 274 | 275 | // assert.equal(replaceWeakRefMessage(app.stderr), ''); 276 | assert(app.stdout.match(/http:\/\/127\.0\.0\.1:7001/)); 277 | const result = await request('http://127.0.0.1:7001'); 278 | assert(result.data.toString() === 'hi, egg'); 279 | }); 280 | 281 | afterEach(async () => { 282 | app.proc.kill('SIGTERM'); 283 | await cleanup(timeoutPath); 284 | }); 285 | 286 | it('should stop error without timeout', async () => { 287 | killer = coffee.fork(eggBin, [ 'stop' ], { cwd: timeoutPath }) as Coffee; 288 | killer.debug(); 289 | killer.expect('code', 0); 290 | await killer.end(); 291 | await scheduler.wait(waitTime); 292 | 293 | // make sure is kill not auto exist 294 | assert(!app.stdout.includes('exist by env')); 295 | 296 | // no way to handle the SIGTERM signal in windows ? 297 | if (!isWindows) { 298 | assert(app.stdout.includes('[master] master is killed by signal SIGTERM, closing')); 299 | assert(app.stdout.match(/app_worker#\d+:\d+ disconnect/)); 300 | assert(app.stdout.match(/don't fork, because worker:\d+ will be kill soon/)); 301 | } 302 | 303 | assert(killer.stdout.includes('stopping egg application')); 304 | assert(killer.stdout.match(/got master pid \[\d+\]/i)); 305 | }); 306 | 307 | it('should stop success', async () => { 308 | killer = coffee.fork(eggBin, [ 'stop', '--timeout=10000' ], { cwd: timeoutPath }) as Coffee; 309 | killer.debug(); 310 | killer.expect('code', 0); 311 | 312 | // await killer.end(); 313 | await scheduler.wait(waitTime); 314 | 315 | // make sure is kill not auto exist 316 | assert(!app.stdout.includes('exist by env')); 317 | 318 | // no way to handle the SIGTERM signal in windows ? 319 | if (!isWindows) { 320 | assert(app.stdout.includes('[master] master is killed by signal SIGTERM, closing')); 321 | assert(app.stdout.includes('[master] exit with code:0')); 322 | assert(app.stdout.includes('[agent_worker] exit with code:0')); 323 | } 324 | 325 | assert(killer.stdout.includes('stopping egg application')); 326 | assert(killer.stdout.match(/got master pid \[\d+\]/i)); 327 | }); 328 | }); 329 | 330 | describe('stop with symlink', () => { 331 | const baseDir = path.join(__dirname, 'fixtures/tmp'); 332 | 333 | beforeEach(async function() { 334 | // if we can't create a symlink, skip the test 335 | try { 336 | await fs.symlink(fixturePath, baseDir, 'dir'); 337 | } catch (err) { 338 | // may get Error: EPERM: operation not permitted on windows 339 | console.log(`test skiped, can't create symlink: ${err}`); 340 | this.skip(); 341 | } 342 | 343 | // *unix get the real path of symlink, but windows wouldn't 344 | const appPathInRegexp = isWindows ? baseDir.replace(/\\/g, '\\\\') : fixturePath; 345 | 346 | await cleanup(fixturePath); 347 | await fs.rm(logDir, { force: true, recursive: true }); 348 | await coffee.fork(eggBin, [ 'start', '--daemon', '--workers=2' ], { cwd: baseDir }) 349 | .debug() 350 | .expect('stdout', new RegExp(`Starting custom-framework application at ${appPathInRegexp}`)) 351 | .expect('code', 0) 352 | .end(); 353 | 354 | await fs.rm(baseDir, { force: true, recursive: true }); 355 | const result = await request('http://127.0.0.1:7001'); 356 | assert(result.data.toString() === 'hi, egg'); 357 | }); 358 | afterEach(async () => { 359 | await cleanup(fixturePath); 360 | await fs.rm(baseDir, { force: true, recursive: true }); 361 | }); 362 | 363 | it('should stop', async () => { 364 | await fs.rm(baseDir, { force: true, recursive: true }); 365 | await fs.symlink(path.join(__dirname, 'fixtures/status'), baseDir); 366 | 367 | await coffee.fork(eggBin, [ 'stop', baseDir ]) 368 | .debug() 369 | .expect('stdout', /stopping egg application/) 370 | .expect('stdout', /got master pid \[\d+\]/i) 371 | .expect('code', 0) 372 | .end(); 373 | }); 374 | }); 375 | }); 376 | -------------------------------------------------------------------------------- /test/start.test.ts: -------------------------------------------------------------------------------- 1 | import path from 'node:path'; 2 | import { fileURLToPath } from 'node:url'; 3 | import { strict as assert } from 'node:assert'; 4 | import fs from 'node:fs/promises'; 5 | import { scheduler } from 'node:timers/promises'; 6 | import { createServer } from 'node:http'; 7 | import { once } from 'node:events'; 8 | import coffee from 'coffee'; 9 | import { request } from 'urllib'; 10 | import { mm, restore } from 'mm'; 11 | import { exists } from 'utility'; 12 | import { cleanup, replaceWeakRefMessage, Coffee } from './utils.js'; 13 | import { isWindows, getSourceFilename } from '../src/helper.js'; 14 | 15 | const version = parseInt(process.version.split('.')[0].substring(1)); 16 | const __filename = fileURLToPath(import.meta.url); 17 | const __dirname = path.dirname(__filename); 18 | 19 | describe('test/start.test.ts', () => { 20 | const eggBin = getSourceFilename('../bin/run.js'); 21 | const fixturePath = path.join(__dirname, 'fixtures/example'); 22 | const homePath = path.join(__dirname, 'fixtures/home'); 23 | const logDir = path.join(homePath, 'logs'); 24 | const waitTime = 10000; 25 | 26 | before(async () => { 27 | await fs.mkdir(homePath, { recursive: true }); 28 | }); 29 | after(async () => { 30 | await fs.rm(homePath, { force: true, recursive: true }); 31 | }); 32 | beforeEach(() => mm(process.env, 'MOCK_HOME_DIR', homePath)); 33 | afterEach(restore); 34 | 35 | describe('start without daemon', () => { 36 | describe('read pkgInfo on CommonJS', () => { 37 | let app: Coffee; 38 | let fixturePath: string; 39 | 40 | before(async () => { 41 | fixturePath = path.join(__dirname, 'fixtures/pkg-config'); 42 | await cleanup(fixturePath); 43 | }); 44 | 45 | after(async () => { 46 | app.proc.kill('SIGTERM'); 47 | await cleanup(fixturePath); 48 | }); 49 | 50 | it('should --require work', async () => { 51 | app = coffee.fork(eggBin, [ 'start', '--workers=1', '--require=./inject2.js' ], { 52 | cwd: fixturePath, 53 | }) as Coffee; 54 | // app.debug(); 55 | app.expect('code', 0); 56 | 57 | await scheduler.wait(waitTime); 58 | 59 | assert.equal(replaceWeakRefMessage(app.stderr), ''); 60 | assert.match(app.stdout, /@@@ inject script\!/); 61 | assert.match(app.stdout, /@@@ inject script1/); 62 | assert.match(app.stdout, /@@@ inject script2/); 63 | }); 64 | 65 | it('inject incorrect script', async () => { 66 | const script = './inject3.js'; 67 | app = coffee.fork(eggBin, [ 'start', '--workers=1', `--require=${script}` ], { 68 | cwd: fixturePath, 69 | }) as Coffee; 70 | // app.debug(); 71 | await scheduler.wait(waitTime); 72 | assert.match(app.stderr, /Cannot find module/); 73 | app.expect('code', 1); 74 | }); 75 | }); 76 | 77 | describe('read pkgInfo on ESM', () => { 78 | let app: Coffee; 79 | let fixturePath: string; 80 | 81 | before(async () => { 82 | fixturePath = path.join(__dirname, 'fixtures/pkg-config-esm'); 83 | await cleanup(fixturePath); 84 | }); 85 | 86 | after(async () => { 87 | app.proc.kill('SIGTERM'); 88 | await cleanup(fixturePath); 89 | }); 90 | 91 | it('should --require work', async () => { 92 | app = coffee.fork(eggBin, [ 'start', '--workers=1', '--require=./inject2.js' ], { 93 | cwd: fixturePath, 94 | }) as Coffee; 95 | // app.debug(); 96 | app.expect('code', 0); 97 | 98 | await scheduler.wait(waitTime); 99 | 100 | assert.equal(replaceWeakRefMessage(app.stderr), ''); 101 | assert.match(app.stdout, /@@@ inject script\!/); 102 | assert.match(app.stdout, /@@@ inject script1/); 103 | assert.match(app.stdout, /@@@ inject script2/); 104 | }); 105 | 106 | it('inject incorrect script', async () => { 107 | const script = './inject3.js'; 108 | app = coffee.fork(eggBin, [ 'start', '--workers=1', `--require=${script}` ], { cwd: fixturePath }) as Coffee; 109 | // app.debug(); 110 | await scheduler.wait(waitTime); 111 | assert.match(app.stderr, /Cannot find module/); 112 | app.expect('code', 1); 113 | }); 114 | }); 115 | 116 | describe('sourcemap default value should respect eggScriptConfig', () => { 117 | let app: Coffee; 118 | let fixturePath: string; 119 | 120 | before(async () => { 121 | fixturePath = path.join(__dirname, 'fixtures/pkg-config-sourcemap'); 122 | await cleanup(fixturePath); 123 | }); 124 | 125 | after(async () => { 126 | app.proc.kill('SIGTERM'); 127 | await cleanup(fixturePath); 128 | }); 129 | 130 | it('should not enable sourcemap-support', async () => { 131 | app = coffee.fork(eggBin, [ 'start', '--workers=1' ], { cwd: fixturePath }) as Coffee; 132 | // app.debug(); 133 | app.expect('code', 0); 134 | 135 | await scheduler.wait(waitTime); 136 | assert.doesNotMatch(app.stdout, /--require .*\/node_modules\/.*source-map-support/); 137 | }); 138 | }); 139 | 140 | describe('full path', () => { 141 | let app: Coffee; 142 | 143 | before(async () => { 144 | await cleanup(fixturePath); 145 | }); 146 | 147 | afterEach(async () => { 148 | app.proc.kill('SIGTERM'); 149 | await cleanup(fixturePath); 150 | }); 151 | 152 | it('should start', async () => { 153 | app = coffee.fork(eggBin, [ 'start', '--workers=2', fixturePath ]) as Coffee; 154 | app.debug(); 155 | app.expect('code', 0); 156 | 157 | await scheduler.wait(waitTime); 158 | 159 | assert.equal(replaceWeakRefMessage(app.stderr), ''); 160 | // assert(!app.stdout.includes('DeprecationWarning:')); 161 | assert(app.stdout.includes('--title=egg-server-example')); 162 | assert(app.stdout.includes('"title":"egg-server-example"')); 163 | assert.match(app.stdout, /custom-framework started on http:\/\/127\.0\.0\.1:7001/); 164 | assert.match(app.stdout, /app_worker#2:/); 165 | assert.doesNotMatch(app.stdout, /app_worker#3:/); 166 | const result = await request('http://127.0.0.1:7001'); 167 | assert.equal(result.data.toString(), 'hi, egg'); 168 | }); 169 | 170 | it('should start --trace-warnings work', async () => { 171 | app = coffee.fork(eggBin, [ 172 | 'start', '--workers=1', path.join(__dirname, 'fixtures/trace-warnings'), 173 | ]) as Coffee; 174 | // app.debug(); 175 | app.expect('code', 0); 176 | 177 | await scheduler.wait(waitTime); 178 | 179 | // assert.match(app.stderr, /MaxListenersExceededWarning:/); 180 | // assert.match(app.stderr, /app.js:10:9/); // should had trace 181 | assert.doesNotMatch(app.stdout, /DeprecationWarning:/); 182 | }); 183 | 184 | it.skip('should get ready', async () => { 185 | app = coffee.fork(path.join(__dirname, './fixtures/ipc-bin/start.js'), [], { 186 | env: { 187 | BASE_DIR: fixturePath, 188 | PATH: process.env.PATH, 189 | }, 190 | }) as Coffee; 191 | // app.debug(); 192 | app.expect('code', 0); 193 | 194 | await scheduler.wait(waitTime); 195 | 196 | assert.equal(replaceWeakRefMessage(app.stderr), ''); 197 | assert(app.stdout.includes('READY!!!')); 198 | assert(app.stdout.includes('--title=egg-server-example')); 199 | assert(app.stdout.includes('"title":"egg-server-example"')); 200 | assert(app.stdout.match(/custom-framework started on http:\/\/127\.0\.0\.1:7001/)); 201 | assert(app.stdout.includes('app_worker#2:')); 202 | assert(!app.stdout.includes('app_worker#3:')); 203 | }); 204 | }); 205 | 206 | describe('child exit with 1', () => { 207 | let app: Coffee; 208 | 209 | before(async () => { 210 | await cleanup(fixturePath); 211 | }); 212 | 213 | after(async () => { 214 | app.proc.kill('SIGTERM'); 215 | await cleanup(fixturePath); 216 | }); 217 | 218 | it('should emit spawn error', async () => { 219 | const server = createServer(() => {}); 220 | server.listen(7007); 221 | 222 | app = coffee.fork(eggBin, [ 'start', '--port=7007', '--workers=2', fixturePath ]) as Coffee; 223 | 224 | await scheduler.wait(waitTime); 225 | server.close(); 226 | assert.equal(app.code, 1); 227 | }); 228 | }); 229 | 230 | describe('relative path', () => { 231 | let app: Coffee; 232 | 233 | before(async () => { 234 | await cleanup(fixturePath); 235 | }); 236 | 237 | after(async () => { 238 | app.proc.kill('SIGTERM'); 239 | await cleanup(fixturePath); 240 | }); 241 | 242 | it('should start', async () => { 243 | app = coffee.fork(eggBin, [ 'start', '--workers=2', path.relative(process.cwd(), fixturePath) ]) as Coffee; 244 | // app.debug(); 245 | app.expect('code', 0); 246 | 247 | await scheduler.wait(waitTime); 248 | 249 | assert.equal(replaceWeakRefMessage(app.stderr), ''); 250 | assert(app.stdout.match(/custom-framework started on http:\/\/127\.0\.0\.1:7001/)); 251 | const result = await request('http://127.0.0.1:7001'); 252 | assert.equal(result.data.toString(), 'hi, egg'); 253 | }); 254 | }); 255 | 256 | describe('without baseDir', () => { 257 | let app: Coffee; 258 | 259 | before(async () => { 260 | await cleanup(fixturePath); 261 | }); 262 | 263 | after(async () => { 264 | app.proc.kill('SIGTERM'); 265 | await cleanup(fixturePath); 266 | }); 267 | 268 | it('should start', async () => { 269 | app = coffee.fork(eggBin, [ 'start', '--workers=2' ], { cwd: fixturePath }) as Coffee; 270 | // app.debug(); 271 | app.expect('code', 0); 272 | 273 | await scheduler.wait(waitTime); 274 | 275 | assert.equal(replaceWeakRefMessage(app.stderr), ''); 276 | assert(app.stdout.match(/custom-framework started on http:\/\/127\.0\.0\.1:7001/)); 277 | const result = await request('http://127.0.0.1:7001'); 278 | assert.equal(result.data.toString(), 'hi, egg'); 279 | }); 280 | }); 281 | 282 | describe('--framework', () => { 283 | let app: Coffee; 284 | 285 | before(async () => { 286 | await cleanup(fixturePath); 287 | }); 288 | 289 | after(async () => { 290 | app.proc.kill('SIGTERM'); 291 | await cleanup(fixturePath); 292 | }); 293 | 294 | it('should start', async () => { 295 | app = coffee.fork(eggBin, [ 'start', '--framework=yadan', '--workers=2', fixturePath ]) as Coffee; 296 | // app.debug(); 297 | app.expect('code', 0); 298 | 299 | await scheduler.wait(waitTime); 300 | 301 | assert.equal(replaceWeakRefMessage(app.stderr), ''); 302 | assert(app.stdout.match(/yadan started on http:\/\/127\.0\.0\.1:7001/)); 303 | const result = await request('http://127.0.0.1:7001'); 304 | assert.equal(result.data.toString(), 'hi, yadan'); 305 | }); 306 | }); 307 | 308 | describe('--title', () => { 309 | let app: Coffee; 310 | 311 | before(async () => { 312 | await cleanup(fixturePath); 313 | }); 314 | 315 | after(async () => { 316 | app.proc.kill('SIGTERM'); 317 | await cleanup(fixturePath); 318 | }); 319 | 320 | it('should start', async () => { 321 | app = coffee.fork(eggBin, [ 'start', '--workers=2', '--title=egg-test', fixturePath ]) as Coffee; 322 | // app.debug(); 323 | app.expect('code', 0); 324 | 325 | await scheduler.wait(waitTime); 326 | 327 | assert.equal(replaceWeakRefMessage(app.stderr), ''); 328 | assert(app.stdout.includes('--title=egg-test')); 329 | assert(app.stdout.includes('"title":"egg-test"')); 330 | assert(app.stdout.match(/custom-framework started on http:\/\/127\.0\.0\.1:7001/)); 331 | assert(app.stdout.includes('app_worker#2:')); 332 | assert(!app.stdout.includes('app_worker#3:')); 333 | const result = await request('http://127.0.0.1:7001'); 334 | assert.equal(result.data.toString(), 'hi, egg'); 335 | }); 336 | }); 337 | 338 | describe('--port', () => { 339 | let app: Coffee; 340 | 341 | before(async () => { 342 | await cleanup(fixturePath); 343 | }); 344 | 345 | after(async () => { 346 | app.proc.kill('SIGTERM'); 347 | await cleanup(fixturePath); 348 | }); 349 | 350 | it('should start', async () => { 351 | app = coffee.fork(eggBin, [ 'start', '--port=7002', '--workers=2', fixturePath ]) as Coffee; 352 | // app.debug(); 353 | app.expect('code', 0); 354 | 355 | await scheduler.wait(waitTime); 356 | 357 | assert.equal(replaceWeakRefMessage(app.stderr), ''); 358 | assert(app.stdout.match(/custom-framework started on http:\/\/127\.0\.0\.1:7002/)); 359 | const result = await request('http://127.0.0.1:7002'); 360 | assert.equal(result.data.toString(), 'hi, egg'); 361 | }); 362 | }); 363 | 364 | describe('process.env.PORT', () => { 365 | let app: Coffee; 366 | 367 | before(async () => { 368 | await cleanup(fixturePath); 369 | }); 370 | 371 | after(async () => { 372 | app.proc.kill('SIGTERM'); 373 | await cleanup(fixturePath); 374 | }); 375 | 376 | it('should start', async () => { 377 | app = coffee.fork(eggBin, [ 'start', '--workers=2', fixturePath ], { 378 | env: Object.assign({}, process.env, { PORT: 7002 }), 379 | }) as Coffee; 380 | // app.debug(); 381 | app.expect('code', 0); 382 | 383 | await scheduler.wait(waitTime); 384 | 385 | assert.equal(replaceWeakRefMessage(app.stderr), ''); 386 | assert.match(app.stdout, /custom-framework started on http:\/\/127\.0\.0\.1:7002/); 387 | const result = await request('http://127.0.0.1:7002'); 388 | assert.equal(result.data.toString(), 'hi, egg'); 389 | }); 390 | }); 391 | 392 | describe('--env', () => { 393 | let app: Coffee; 394 | 395 | before(async () => { 396 | await cleanup(fixturePath); 397 | }); 398 | 399 | after(async () => { 400 | app.proc.kill('SIGTERM'); 401 | await cleanup(fixturePath); 402 | }); 403 | 404 | it('should start', async () => { 405 | app = coffee.fork(eggBin, [ 'start', '--workers=2', '--env=pre', fixturePath ]) as Coffee; 406 | // app.debug(); 407 | app.expect('code', 0); 408 | 409 | await scheduler.wait(waitTime); 410 | 411 | assert.equal(replaceWeakRefMessage(app.stderr), ''); 412 | assert(app.stdout.match(/custom-framework started on http:\/\/127\.0\.0\.1:7001/)); 413 | const result = await request('http://127.0.0.1:7001/env'); 414 | assert.equal(result.data.toString(), 'pre, true'); 415 | }); 416 | }); 417 | 418 | describe('custom env', () => { 419 | let app: Coffee; 420 | 421 | before(async () => { 422 | await cleanup(fixturePath); 423 | }); 424 | 425 | after(async () => { 426 | app.proc.kill('SIGTERM'); 427 | await cleanup(fixturePath); 428 | }); 429 | 430 | it('should start', async () => { 431 | mm(process.env, 'CUSTOM_ENV', 'pre'); 432 | app = coffee.fork(eggBin, [ 'start', '--workers=2', fixturePath ]) as Coffee; 433 | // app.debug(); 434 | app.expect('code', 0); 435 | 436 | await scheduler.wait(waitTime); 437 | 438 | assert.equal(replaceWeakRefMessage(app.stderr), ''); 439 | assert(app.stdout.includes('## EGG_SERVER_ENV is not pass')); 440 | assert(app.stdout.includes('## CUSTOM_ENV: pre')); 441 | assert(app.stdout.match(/custom-framework started on http:\/\/127\.0\.0\.1:7001/)); 442 | let result = await request('http://127.0.0.1:7001/env'); 443 | assert.equal(result.data.toString(), 'pre, true'); 444 | result = await request('http://127.0.0.1:7001/path'); 445 | const appBinPath = path.join(fixturePath, 'node_modules/.bin'); 446 | assert(result.data.toString().startsWith(`${appBinPath}${path.delimiter}`)); 447 | }); 448 | }); 449 | 450 | describe('--stdout --stderr', () => { 451 | let app: Coffee; 452 | 453 | before(async () => { 454 | await cleanup(fixturePath); 455 | await fs.rm(logDir, { force: true, recursive: true }); 456 | await fs.rm(path.join(fixturePath, 'start-fail'), { force: true, recursive: true }); 457 | await fs.mkdir(logDir, { recursive: true }); 458 | }); 459 | 460 | after(async () => { 461 | app.proc.kill('SIGTERM'); 462 | await cleanup(fixturePath); 463 | await fs.rm(path.join(fixturePath, 'stdout.log'), { force: true }); 464 | await fs.rm(path.join(fixturePath, 'stderr.log'), { force: true }); 465 | await fs.rm(path.join(fixturePath, 'start-fail'), { force: true, recursive: true }); 466 | }); 467 | 468 | it('should start', async () => { 469 | const stdout = path.join(fixturePath, 'stdout.log'); 470 | const stderr = path.join(fixturePath, 'stderr.log'); 471 | app = coffee.fork(eggBin, [ 472 | 'start', '--workers=1', '--daemon', `--stdout=${stdout}`, `--stderr=${stderr}`, fixturePath, 473 | ]) as Coffee; 474 | // app.debug(); 475 | app.expect('code', 0); 476 | 477 | await scheduler.wait(waitTime); 478 | 479 | let content = await fs.readFile(stdout, 'utf-8'); 480 | assert.match(content, /custom-framework started on http:\/\/127\.0\.0\.1:7001/); 481 | 482 | content = await fs.readFile(stderr, 'utf-8'); 483 | assert.equal(content, ''); 484 | }); 485 | 486 | it('should start with insecurity --stderr argument', async () => { 487 | const cwd = path.join(__dirname, 'fixtures/status'); 488 | mm(process.env, 'ERROR', 'error message'); 489 | 490 | const stdout = path.join(fixturePath, 'start-fail/stdout.log'); 491 | const stderr = path.join(fixturePath, 'start-fail/stderr.log'); 492 | const malicious = path.join(fixturePath, 'start-fail/malicious'); 493 | app = coffee.fork(eggBin, [ 494 | 'start', '--workers=1', '--daemon', `--stdout=${stdout}`, 495 | `--stderr=${stderr}; touch ${malicious}`, 496 | cwd, 497 | ]) as Coffee; 498 | // app.debug(); 499 | 500 | await scheduler.wait(waitTime); 501 | 502 | const content = await fs.readFile(stdout, 'utf-8'); 503 | assert(!content.match(/custom-framework started on http:\/\/127\.0\.0\.1:7001/)); 504 | let stats = await exists(stderr); 505 | assert(!stats); 506 | stats = await exists(malicious); 507 | assert(!stats); 508 | }); 509 | }); 510 | 511 | describe('--node', () => { 512 | let app: Coffee; 513 | 514 | beforeEach(async () => { 515 | await cleanup(fixturePath); 516 | }); 517 | 518 | beforeEach(async () => { 519 | app && app.proc && app.proc.kill('SIGTERM'); 520 | await cleanup(fixturePath); 521 | }); 522 | 523 | describe('daemon', () => { 524 | it('should start', async () => { 525 | app = coffee.fork(eggBin, [ 526 | 'start', '--daemon', '--framework=yadan', '--workers=2', `--node=${process.execPath}`, fixturePath, 527 | ]) as Coffee; 528 | // app.debug(); 529 | app.expect('code', 0); 530 | 531 | await scheduler.wait(waitTime); 532 | 533 | assert.equal(replaceWeakRefMessage(app.stderr), ''); 534 | assert(app.stdout.match(/yadan started on http:\/\/127\.0\.0\.1:7001/)); 535 | const result = await request('http://127.0.0.1:7001'); 536 | assert.equal(result.data.toString(), 'hi, yadan'); 537 | }); 538 | 539 | it('should error if node path invalid', async () => { 540 | app = coffee.fork(eggBin, [ 541 | 'start', '--daemon', '--framework=yadan', '--workers=2', '--node=invalid', fixturePath, 542 | ]) as Coffee; 543 | // app.debug(); 544 | app.expect('code', 1); 545 | 546 | await scheduler.wait(3000); 547 | assert.match(app.stderr, /spawn invalid ENOENT/); 548 | }); 549 | }); 550 | 551 | describe('not daemon', () => { 552 | it('should start', async () => { 553 | app = coffee.fork(eggBin, [ 554 | 'start', '--framework=yadan', '--workers=2', `--node=${process.execPath}`, fixturePath, 555 | ]) as Coffee; 556 | // app.debug(); 557 | app.expect('code', 0); 558 | 559 | await scheduler.wait(waitTime); 560 | 561 | assert.equal(replaceWeakRefMessage(app.stderr), ''); 562 | assert(app.stdout.match(/yadan started on http:\/\/127\.0\.0\.1:7001/)); 563 | const result = await request('http://127.0.0.1:7001'); 564 | assert.equal(result.data.toString(), 'hi, yadan'); 565 | }); 566 | 567 | it('should error if node path invalid', async () => { 568 | app = coffee.fork(eggBin, [ 569 | 'start', '--framework=yadan', '--workers=2', '--node=invalid', fixturePath, 570 | ]) as Coffee; 571 | // app.debug(); 572 | app.expect('code', 1); 573 | 574 | await scheduler.wait(3000); 575 | assert.match(app.stderr, /spawn invalid ENOENT/); 576 | }); 577 | }); 578 | }); 579 | 580 | describe('read cluster config', () => { 581 | let app: Coffee; 582 | let fixturePath: string; 583 | 584 | before(async () => { 585 | fixturePath = path.join(__dirname, 'fixtures/cluster-config'); 586 | await cleanup(fixturePath); 587 | }); 588 | 589 | after(async () => { 590 | app.proc.kill('SIGTERM'); 591 | await cleanup(fixturePath); 592 | }); 593 | 594 | it('should start', async () => { 595 | app = coffee.fork(eggBin, [ 'start', '--workers=2', fixturePath ]) as Coffee; 596 | // app.debug(); 597 | app.expect('code', 0); 598 | 599 | await scheduler.wait(waitTime); 600 | 601 | assert.equal(replaceWeakRefMessage(app.stderr), ''); 602 | assert(app.stdout.match(/egg started on http:\/\/127\.0\.0\.1:8000/)); 603 | assert(!app.stdout.includes('app_worker#3:')); 604 | const result = await request('http://127.0.0.1:8000'); 605 | assert.equal(result.data.toString(), 'hi, egg'); 606 | }); 607 | }); 608 | 609 | describe('read eggScriptsConfig', () => { 610 | let app: Coffee; 611 | let fixturePath: string; 612 | 613 | before(async () => { 614 | fixturePath = path.join(__dirname, 'fixtures/egg-scripts-node-options'); 615 | await cleanup(fixturePath); 616 | }); 617 | 618 | after(async () => { 619 | app.proc.kill('SIGTERM'); 620 | await cleanup(fixturePath); 621 | }); 622 | 623 | it('should start', async () => { 624 | app = coffee.fork(eggBin, [ 'start', '--workers=1', fixturePath ]) as Coffee; 625 | app.debug(); 626 | app.expect('code', 0); 627 | 628 | await scheduler.wait(waitTime); 629 | 630 | assert.equal(replaceWeakRefMessage(app.stderr), ''); 631 | assert.match(app.stdout, /maxHeaderSize: 20000/); 632 | }); 633 | }); 634 | 635 | describe('read egg.revert', () => { 636 | if (version !== 20) return; 637 | if (isWindows) return; 638 | let app: Coffee; 639 | let fixturePath: string; 640 | 641 | before(async () => { 642 | fixturePath = path.join(__dirname, 'fixtures/egg-revert'); 643 | await cleanup(fixturePath); 644 | }); 645 | 646 | after(async () => { 647 | app.proc.kill('SIGTERM'); 648 | await cleanup(fixturePath); 649 | }); 650 | 651 | it('should start', async () => { 652 | app = coffee.fork(eggBin, [ 'start', '--workers=1', fixturePath ]) as Coffee; 653 | // app.debug(); 654 | app.expect('code', 0); 655 | 656 | await scheduler.wait(waitTime); 657 | 658 | assert.equal(replaceWeakRefMessage(app.stderr), ''); 659 | assert.match(app.stdout, /SECURITY WARNING: Reverting CVE-2023-46809: Marvin attack on PKCS#1 padding/); 660 | }); 661 | }); 662 | 663 | describe('subDir as baseDir', () => { 664 | let app: Coffee; 665 | const rootDir = path.join(__dirname, '..'); 666 | const subDir = path.join(__dirname, 'fixtures/subdir-as-basedir/base-dir'); 667 | 668 | before(async () => { 669 | await cleanup(rootDir); 670 | }); 671 | 672 | after(async () => { 673 | app.proc.kill('SIGTERM'); 674 | await cleanup(rootDir); 675 | }); 676 | 677 | it('should start', async () => { 678 | app = coffee.fork(eggBin, [ 'start', '--workers=2', subDir ], { cwd: rootDir }) as Coffee; 679 | // app.debug(); 680 | app.expect('code', 0); 681 | 682 | await scheduler.wait(waitTime); 683 | 684 | assert.equal(replaceWeakRefMessage(app.stderr), ''); 685 | assert(app.stdout.match(/egg started on http:\/\/127\.0\.0\.1:7001/)); 686 | const result = await request('http://127.0.0.1:7001'); 687 | assert.equal(result.data.toString(), 'hi, egg'); 688 | }); 689 | }); 690 | 691 | describe('auto set custom node dir to PATH', () => { 692 | let app: Coffee; 693 | let fixturePath: string; 694 | 695 | before(async () => { 696 | fixturePath = path.join(__dirname, 'fixtures/custom-node-dir'); 697 | await cleanup(fixturePath); 698 | }); 699 | 700 | after(async () => { 701 | app.proc.kill('SIGTERM'); 702 | await cleanup(fixturePath); 703 | }); 704 | 705 | it('should start', async () => { 706 | const expectPATH = [ 707 | path.join(fixturePath, 'node_modules/.bin'), 708 | path.join(fixturePath, '.node/bin'), 709 | ].join(path.delimiter) + path.delimiter; 710 | app = coffee.fork(eggBin, [ 'start', '--workers=2', '--port=7002', fixturePath ]) as Coffee; 711 | // app.debug(); 712 | app.expect('code', 0); 713 | 714 | await scheduler.wait(waitTime); 715 | 716 | assert.equal(replaceWeakRefMessage(app.stderr), ''); 717 | assert.match(app.stdout, /egg started on http:\/\/127\.0\.0\.1:7002/); 718 | assert(!app.stdout.includes('app_worker#3:')); 719 | const result = await request('http://127.0.0.1:7002'); 720 | assert(result.data.toString().startsWith(`hi, ${expectPATH}`)); 721 | }); 722 | }); 723 | 724 | describe('kill command', () => { 725 | let app: Coffee; 726 | 727 | before(async () => { 728 | await cleanup(fixturePath); 729 | }); 730 | 731 | after(async () => { 732 | await cleanup(fixturePath); 733 | }); 734 | 735 | it('should wait child process exit', async () => { 736 | app = coffee.fork(eggBin, [ 'start', '--port=7007', '--workers=2', fixturePath ]) as Coffee; 737 | await scheduler.wait(waitTime); 738 | const exitEvent = once(app.proc, 'exit'); 739 | app.proc.kill('SIGTERM'); 740 | const [ code ] = await exitEvent; 741 | if (isWindows) { 742 | assert(code === null); 743 | } else { 744 | assert.equal(code, 0); 745 | } 746 | }); 747 | }); 748 | }); 749 | 750 | describe('start with daemon', () => { 751 | let cwd: string; 752 | beforeEach(async () => { 753 | if (cwd) { 754 | await cleanup(cwd); 755 | } 756 | await fs.rm(logDir, { force: true, recursive: true }); 757 | await fs.mkdir(logDir, { recursive: true }); 758 | await fs.writeFile(path.join(logDir, 'master-stdout.log'), 'just for test'); 759 | await fs.writeFile(path.join(logDir, 'master-stderr.log'), 'just for test'); 760 | }); 761 | 762 | afterEach(async () => { 763 | await coffee.fork(eggBin, [ 'stop', cwd ]) 764 | // .debug() 765 | .end(); 766 | await cleanup(cwd); 767 | }); 768 | 769 | it('should start custom-framework', async () => { 770 | cwd = fixturePath; 771 | await coffee.fork(eggBin, [ 'start', '--daemon', '--workers=2', '--port=7002', cwd ]) 772 | // .debug() 773 | .expect('stdout', /Starting custom-framework application/) 774 | .expect('stdout', /custom-framework started on http:\/\/127\.0\.0\.1:7002/) 775 | .expect('code', 0) 776 | .end(); 777 | 778 | // master log 779 | const stdout = await fs.readFile(path.join(logDir, 'master-stdout.log'), 'utf-8'); 780 | const stderr = await fs.readFile(path.join(logDir, 'master-stderr.log'), 'utf-8'); 781 | assert(stderr === ''); 782 | assert.match(stdout, /custom-framework started on http:\/\/127\.0\.0\.1:7002/); 783 | 784 | // should rotate log 785 | const fileList = await fs.readdir(logDir); 786 | // console.log(fileList); 787 | assert(fileList.some(name => name.match(/master-stdout\.log\.\d+\.\d+/))); 788 | assert(fileList.some(name => name.match(/master-stderr\.log\.\d+\.\d+/))); 789 | 790 | const result = await request('http://127.0.0.1:7002'); 791 | assert.equal(result.data.toString(), 'hi, egg'); 792 | }); 793 | 794 | it('should start default egg', async () => { 795 | cwd = path.join(__dirname, 'fixtures/egg-app'); 796 | await coffee.fork(eggBin, [ 'start', '--daemon', '--workers=2', cwd ]) 797 | // .debug() 798 | .expect('stdout', /Starting egg application/) 799 | .expect('stdout', /egg started on http:\/\/127\.0\.0\.1:7001/) 800 | .expect('code', 0) 801 | .end(); 802 | }); 803 | }); 804 | 805 | describe('check status', () => { 806 | let cwd: string; 807 | beforeEach(() => { 808 | cwd = path.join(__dirname, 'fixtures/status'); 809 | }); 810 | 811 | after(async () => { 812 | await coffee.fork(eggBin, [ 'stop', cwd ]) 813 | // .debug() 814 | .end(); 815 | await cleanup(cwd); 816 | }); 817 | 818 | it('should status check success, exit with 0', async () => { 819 | mm(process.env, 'WAIT_TIME', 3000); 820 | await coffee.fork(eggBin, [ 'start', '--daemon', '--workers=1' ], { cwd }) 821 | // .debug() 822 | .expect('stdout', /Wait Start: 2.../) 823 | .expect('stdout', /custom-framework started/) 824 | .expect('code', 0) 825 | .end(); 826 | }); 827 | 828 | it('should status check fail `--ignore-stderr`, exit with 0', async () => { 829 | mm(process.env, 'WAIT_TIME', 3000); 830 | mm(process.env, 'ERROR', 'error message'); 831 | const app = coffee.fork(eggBin, [ 'start', '--daemon', '--workers=1', '--ignore-stderr' ], { cwd }); 832 | // app.debug(); 833 | // TODO: find a windows replacement for tail command 834 | if (!isWindows) { 835 | app.expect('stderr', /nodejs.Error: error message/); 836 | } 837 | await app.expect('stderr', /Start got error, see /) 838 | .expect('code', 0) 839 | .end(); 840 | }); 841 | 842 | it('should status check fail `--ignore-stderr` in package.json, exit with 0', async () => { 843 | cwd = path.join(__dirname, 'fixtures/egg-scripts-config'); 844 | mm(process.env, 'WAIT_TIME', 3000); 845 | mm(process.env, 'ERROR', 'error message'); 846 | 847 | const app = coffee.fork(eggBin, [ 'start' ], { cwd }); 848 | // app.debug(); 849 | // TODO: find a windows replacement for tail command 850 | if (!isWindows) { 851 | app.expect('stderr', /nodejs.Error: error message/); 852 | } 853 | await app.expect('stderr', /Start got error, see /) 854 | .expect('code', 0) 855 | .end(); 856 | }); 857 | 858 | it('should status check fail, exit with 1', async () => { 859 | mm(process.env, 'WAIT_TIME', 3000); 860 | mm(process.env, 'ERROR', 'error message'); 861 | 862 | const app = coffee.fork(eggBin, [ 'start', '--daemon', '--workers=1' ], { cwd }); 863 | // app.debug(); 864 | // TODO: find a windows replacement for tail command 865 | if (!isWindows) { 866 | app.expect('stderr', /nodejs.Error: error message/); 867 | } 868 | await app.expect('stderr', /Start got error, see /) 869 | .expect('stderr', /Got error when startup/) 870 | .expect('code', 1) 871 | .end(); 872 | }); 873 | 874 | it('should status check timeout and exit with code 1', async () => { 875 | mm(process.env, 'WAIT_TIME', 10000); 876 | 877 | await coffee.fork(eggBin, [ 'start', '--daemon', '--workers=1', '--timeout=5000' ], { cwd }) 878 | // .debug() 879 | .expect('stdout', /Wait Start: 1.../) 880 | .expect('stderr', /Start failed, 5s timeout/) 881 | .expect('code', 1) 882 | .end(); 883 | }); 884 | }); 885 | }); 886 | --------------------------------------------------------------------------------