@eggjs/bin
v8.1.0
Published
egg developer tool
Downloads
3,561
Keywords
Readme
@eggjs/bin
egg developer tool, base on oclif.
Install
npm i @eggjs/bin --save-devUsage
Add egg-bin to package.json scripts:
{
"scripts": {
"dev": "egg-bin dev",
"test-local": "egg-bin test",
"test": "npm run lint -- --fix && npm run test-local",
"cov": "egg-bin cov",
"bundle": "egg-bin bundle",
"lint": "eslint .",
"ci": "npm run lint && npm run cov"
}
}Command
All the commands support these specific options:
--inspect--inspect-brk--typescript/--tsenable typescript support. Auto detect frompackage.json'spkg.egg.typescript, orpkg.dependencies.typescript/pkg.devDependencies.typescript.--base/--baseDirapplication's root path, default toprocess.cwd().--requirewill add toexecArgv, support multiple. Also support read frompackage.json'spkg.egg.require--dry-run/-dwhether dry-run the test command, just show the command
egg-bin [command] --inspect
egg-bin [command] --inspect-brk
egg-bin [command] --typescript
egg-bin [command] --base /foo/bardev
Start dev cluster on local env, it will start a master, an agent and a worker.
egg-bin devdev options
--frameworkegg web framework root path.--portserver port. If not specified, the port is obtained in the following order: egg.js configurationconfig/config.*.js>process.env.EGG_BIN_DEFAULT_PORT> 7001 > other available ports.--workersworker process number, default to1worker at local mode.--stickystart a sticky cluster server, default tofalse.
debug/inspect on VSCode
Create .vscode/launch.json file:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Egg Debug",
"runtimeExecutable": "npm",
"runtimeArgs": ["run", "dev", "--", "--inspect-brk"],
"console": "integratedTerminal",
"restart": true,
"autoAttachChildProcesses": true
},
{
"type": "node",
"request": "launch",
"name": "Egg Test",
"runtimeExecutable": "npm",
"runtimeArgs": ["run", "test-local", "--", "--inspect-brk"],
"autoAttachChildProcesses": true
}
]
}test
Using vitest 5 to run tests. Vitest 4 is no longer supported.
egg-bin test [...files] [options]filesis optional, default totest/**/*.test.tstest/fixtures,test/node_modulesis always excluded.
auto load test/.setup.ts
If test/.setup.ts (or .setup.js) exists, it will be auto-added as the first vitest setupFile.
test
├── .setup.ts
└── foo.test.tsauto-inject @eggjs/mock/setup_vitest
For egg applications, @eggjs/mock/setup_vitest is automatically registered as a vitest setup file when @eggjs/mock is installed, handling app lifecycle (beforeAll / afterEach / afterAll).
auto use @eggjs/tegg-vitest/runner
If @eggjs/tegg-vitest is installed in the project, its runner is automatically detected and injected into the vitest config.
test options
--timeout/-tmilliseconds, default to60000--no-timeoutdisable timeout--grep/-gonly run tests matching pattern--bail/-bstop after first test failure--changed/-conly run tests for changed files (matchestest/**/*.test.(js|ts))--watch/-wrun in watch mode
test environment
You can set TESTS env to specify test files, supports comma-separated glob patterns.
TESTS=test/a.test.ts egg-bin testThe reporter can be set with TEST_REPORTER env (any vitest reporter), default is default.
TEST_REPORTER=json writes the report to .vitest/json/output.json in the application directory.
TEST_REPORTER=verbose egg-bin testThe test timeout can be set with TEST_TIMEOUT env, default is 60000 ms.
TEST_TIMEOUT=2000 egg-bin testcov
Using vitest with v8 coverage to run code coverage. Supports all test options above.
Coverage reports are written to coverage/ and include: text-summary, json-summary, json, lcov, cobertura.
cov options
-xadd a glob pattern to exclude from coverage, supports multiple- also supports all test options above.
cov environment
You can set COV_EXCLUDES env to add glob patterns to exclude from coverage (comma-separated).
Patterns match paths relative to the application directory. Absolute paths and directory patterns such as ignore/ are also supported.
COV_EXCLUDES="app/plugins/c*,app/autocreate/**" egg-bin covbundle
Bundle an Egg application into a deployable artifact with @eggjs/egg-bundler.
egg-bin bundle
egg-bin bundle --output ./dist-bundle
egg-bin bundle --mode development
egg-bin bundle --framework egg --output ./out
egg-bin bundle --pack-alias some-package=./node_modules/some-package/index.jsThe command writes the bundle to ./dist-bundle by default. The generated
artifact can be started with Node from the output directory:
cd dist-bundle
node worker.jsbundle options
--output/-ooutput directory, default to./dist-bundle--manifestpath tomanifest.json, default to<baseDir>/.egg/manifest.json--framework/-fframework package specifier, defaulting topackage.json#egg.frameworkoregg. Absolute framework paths are not supported by the bundled runtime.--modebuild mode,productionordevelopment, default toproduction--force-externalpackage name to always keep external, supports multiple--inline-externalpackage name to force inline, supports multiple--pack-alias@utoo/packresolve alias in<specifier>=<target>form, supports multiple. Dot-relative targets such as./target.jsand../target.jsare resolved from the application base directory
Bundle aliases can also be committed in the application module.yml:
bundle:
pack:
resolve:
alias:
some-package: ./node_modules/some-package/index.jsmodule.yml aliases use the same target resolution rules as --pack-alias.
If the same alias is configured in both places, the explicit CLI
--pack-alias value wins.
See @eggjs/egg-bundler for the programmatic API
and output structure.
Breaking Changes (v8)
Migrated from Mocha to Vitest
The test and cov commands now use vitest instead of Mocha. This brings native TypeScript support, faster execution, and built-in watch mode, but removes some Mocha-specific options:
Removed flags:
| Old flag | Reason |
| ------------------- | --------------------------------------------------------------- |
| --parallel / -p | Vitest handles parallelism natively via worker pools |
| --jobs / -j | Replaced by vitest's built-in pool configuration |
| --auto-agent | Mocha-specific, no equivalent needed in vitest |
| --prerequire | Use test/.setup.ts setupFile instead |
| --c8 | Coverage is now configured inside vitest, use -x for excludes |
Removed environment variables:
| Old env var | Reason |
| ------------ | ---------------------------------------------- |
| MOCHA_FILE | Mocha-specific, vitest runner is auto-detected |
Changed output format:
Test output now follows vitest's format. Assertions in test scripts that match mocha output (e.g. "N passing") should be updated to vitest output (e.g. "N passed").
Migration guide:
- Replace
before()/after()hooks withbeforeAll()/afterAll()(vitest naming) - Import vitest globals explicitly in
.tssetup files:import { beforeAll, afterEach } from 'vitest' - Plain
.jstest files can use globals directly (vitestglobals: trueis enabled by default) - Remove
--parallel/--jobsflags from your npm scripts
Custom egg-bin for your team
See https://oclif.io/docs/configuring_your_cli/
License
Contributors
Made with contributors-img.
