From 511dd6de4db00192bccce841d70b73832d910dcc Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Sun, 29 Dec 2024 15:59:19 +0800 Subject: [PATCH 01/10] feat: hello world for egg v4 part of https://github.com/eggjs/egg/issues/3644 --- helloworld/.eslintrc | 6 +++++ helloworld/.gitignore | 2 ++ helloworld/app/controller/foo.js | 13 ----------- helloworld/app/controller/foo.ts | 11 ++++++++++ helloworld/app/controller/home.js | 13 ----------- helloworld/app/controller/home.ts | 8 +++++++ helloworld/app/{router.js => router.ts} | 4 ++-- helloworld/config/config.default.js | 3 --- helloworld/config/config.default.ts | 3 +++ helloworld/package.json | 23 +++++++++++++++----- helloworld/test/index.test.js | 29 ------------------------- helloworld/test/index.test.ts | 19 ++++++++++++++++ helloworld/tsconfig.json | 14 ++++++++++++ 13 files changed, 83 insertions(+), 65 deletions(-) create mode 100644 helloworld/.eslintrc create mode 100644 helloworld/.gitignore delete mode 100644 helloworld/app/controller/foo.js create mode 100644 helloworld/app/controller/foo.ts delete mode 100644 helloworld/app/controller/home.js create mode 100644 helloworld/app/controller/home.ts rename helloworld/app/{router.js => router.ts} (59%) delete mode 100644 helloworld/config/config.default.js create mode 100644 helloworld/config/config.default.ts delete mode 100644 helloworld/test/index.test.js create mode 100644 helloworld/test/index.test.ts create mode 100644 helloworld/tsconfig.json diff --git a/helloworld/.eslintrc b/helloworld/.eslintrc new file mode 100644 index 00000000..9bcdb468 --- /dev/null +++ b/helloworld/.eslintrc @@ -0,0 +1,6 @@ +{ + "extends": [ + "eslint-config-egg/typescript", + "eslint-config-egg/lib/rules/enforce-node-prefix" + ] +} diff --git a/helloworld/.gitignore b/helloworld/.gitignore new file mode 100644 index 00000000..23a608b6 --- /dev/null +++ b/helloworld/.gitignore @@ -0,0 +1,2 @@ +node_modules +*.js diff --git a/helloworld/app/controller/foo.js b/helloworld/app/controller/foo.js deleted file mode 100644 index 7ed1cb07..00000000 --- a/helloworld/app/controller/foo.js +++ /dev/null @@ -1,13 +0,0 @@ -'use strict'; - -const Controller = require('egg').Controller; - -class FooController extends Controller { - async render() { - const ctx = this.ctx; - - ctx.body = 'Hello foo'; - } -} - -module.exports = FooController; diff --git a/helloworld/app/controller/foo.ts b/helloworld/app/controller/foo.ts new file mode 100644 index 00000000..7ab1f970 --- /dev/null +++ b/helloworld/app/controller/foo.ts @@ -0,0 +1,11 @@ +import { Controller } from 'egg'; + +export default class FooController extends Controller { + async render() { + const ctx = this.ctx; + ctx.status = 400; + ctx.body = { + foo: 'bar', + }; + } +} diff --git a/helloworld/app/controller/home.js b/helloworld/app/controller/home.js deleted file mode 100644 index 45a39944..00000000 --- a/helloworld/app/controller/home.js +++ /dev/null @@ -1,13 +0,0 @@ -'use strict'; - -const Controller = require('egg').Controller; - -class HomeController extends Controller { - async render() { - const ctx = this.ctx; - - ctx.body = 'Hello World'; - } -} - -module.exports = HomeController; diff --git a/helloworld/app/controller/home.ts b/helloworld/app/controller/home.ts new file mode 100644 index 00000000..08a78dad --- /dev/null +++ b/helloworld/app/controller/home.ts @@ -0,0 +1,8 @@ +import { Controller } from 'egg'; + +export default class HomeController extends Controller { + async render() { + const ctx = this.ctx; + ctx.body = 'Hello World'; + } +} diff --git a/helloworld/app/router.js b/helloworld/app/router.ts similarity index 59% rename from helloworld/app/router.js rename to helloworld/app/router.ts index 4b1c79d2..f56b86d5 100644 --- a/helloworld/app/router.js +++ b/helloworld/app/router.ts @@ -1,6 +1,6 @@ -'use strict'; +import { Application } from 'egg'; -module.exports = app => { +export default (app: Application) => { app.router.get('/', app.controller.home.render); app.router.get('/foo', app.controller.foo.render); }; diff --git a/helloworld/config/config.default.js b/helloworld/config/config.default.js deleted file mode 100644 index af619a43..00000000 --- a/helloworld/config/config.default.js +++ /dev/null @@ -1,3 +0,0 @@ -'use strict'; - -exports.keys = 'my secret keys'; diff --git a/helloworld/config/config.default.ts b/helloworld/config/config.default.ts new file mode 100644 index 00000000..4e2424d4 --- /dev/null +++ b/helloworld/config/config.default.ts @@ -0,0 +1,3 @@ +export default { + keys: 'my secret keys', +}; diff --git a/helloworld/package.json b/helloworld/package.json index 800cf2f8..a34aa063 100644 --- a/helloworld/package.json +++ b/helloworld/package.json @@ -1,16 +1,29 @@ { "name": "helloworld", "dependencies": { - "egg": "^1.10.1" + "egg": "beta" }, "devDependencies": { - "egg-bin": "^4.3.5", - "egg-mock": "^3.13.1" + "@eggjs/bin": "7", + "@eggjs/mock": "6", + "@eggjs/tsconfig": "1", + "@types/mocha": "10", + "@types/node": "22", + "eslint": "8", + "eslint-config-egg": "14", + "typescript": "5" }, "scripts": { + "lint": "eslint . --ext .ts", "dev": "egg-bin dev", + "pretest": "npm run lint -- --fix", "test": "egg-bin test", - "cov": "egg-bin cov" + "preci": "npm run lint", + "ci": "egg-bin cov", + "postci": "npm run prepublishOnly && npm run clean", + "clean": "tsc -b --clean", + "prepublishOnly": "npm run clean && tsc" }, - "private": true + "private": true, + "repository": "git@github.com:eggjs/examples.git" } diff --git a/helloworld/test/index.test.js b/helloworld/test/index.test.js deleted file mode 100644 index 9c3bb8e8..00000000 --- a/helloworld/test/index.test.js +++ /dev/null @@ -1,29 +0,0 @@ -'use strict'; - - -const mm = require('egg-mock'); - -describe('example helloworld test', () => { - let app; - - before(() => { - app = mm.app(); - return app.ready(); - }); - - after(() => app.close()); - - it('should GET / 200', () => { - return app.httpRequest() - .get('/') - .expect(200) - .expect('Hello World'); - }); - - it('should GET /foo', () => { - return app.httpRequest() - .get('/foo') - .expect(200) - .expect('Hello foo'); - }); -}); diff --git a/helloworld/test/index.test.ts b/helloworld/test/index.test.ts new file mode 100644 index 00000000..5252564d --- /dev/null +++ b/helloworld/test/index.test.ts @@ -0,0 +1,19 @@ +import { app } from '@eggjs/mock/bootstrap'; + +describe('example helloworld test', () => { + it('should GET / 200', () => { + return app.httpRequest() + .get('/') + .expect(200) + .expect('Hello World'); + }); + + it('should GET /foo', async () => { + await app.httpRequest() + .get('/foo') + .expect(400) + .expect({ + foo: 'bar', + }); + }); +}); diff --git a/helloworld/tsconfig.json b/helloworld/tsconfig.json new file mode 100644 index 00000000..480e95d5 --- /dev/null +++ b/helloworld/tsconfig.json @@ -0,0 +1,14 @@ +{ + "extends": "@eggjs/tsconfig", + "compilerOptions": { + "strict": true, + "noImplicitAny": true, + "target": "ES2022", + "module": "NodeNext", + "moduleResolution": "NodeNext", + "declaration": false + }, + "exclude": [ + "test" + ] +} From 62be764ca4c719f83a140b46702311ddaf86445c Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Sun, 29 Dec 2024 16:28:51 +0800 Subject: [PATCH 02/10] feat: upgrade to egg v4 BREAKING CHANGE: drop Node.js < 18.19.0 support part of https://github.com/eggjs/egg/issues/3644 https://github.com/eggjs/egg/issues/5257 --- .github/PULL_REQUEST_TEMPLATE.md | 24 ------------------------ .github/workflows/ci.yml | 7 ++----- README.md | 18 ++++-------------- package.json | 4 ++-- 4 files changed, 8 insertions(+), 45 deletions(-) delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index 48f9944f..00000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,24 +0,0 @@ - - -##### Checklist - - -- [ ] `npm test` passes -- [ ] tests and/or benchmarks are included -- [ ] documentation is changed or added -- [ ] commit message follows commit guidelines - -##### Affected core subsystem(s) - - - -##### Description of change - diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 18a0657a..dc5563d8 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -3,16 +3,13 @@ name: CI on: push: branches: [ master ] - pull_request: branches: [ master ] - workflow_dispatch: {} - jobs: Job: name: Node.js - uses: artusjs/github-actions/.github/workflows/node-test.yml@master + uses: node-modules/github-actions/.github/workflows/node-test.yml@master with: os: 'ubuntu-latest' - version: '14, 16, 18' + version: '18, 20, 22' diff --git a/README.md b/README.md index 92bd9832..03f04ab3 100644 --- a/README.md +++ b/README.md @@ -1,16 +1,12 @@ # Examples for [egg](https://github.com/eggjs/egg) ---- - -[![build status][travis-image]][travis-url] +[![CI](https://github.com/eggjs/examples/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/eggjs/examples/actions/workflows/ci.yml) [![node version][node-image]][node-url] [![egg version][egg-image]][egg-url] -[travis-image]: https://img.shields.io/travis/eggjs/examples.svg?style=flat-square -[travis-url]: https://travis-ci.org/eggjs/examples -[node-image]: https://img.shields.io/badge/node.js-%3E=_8-green.svg?style=flat-square +[node-image]: https://img.shields.io/badge/node.js-%3E=_18-green.svg?style=flat-square [node-url]: http://nodejs.org/download/ -[egg-image]: https://img.shields.io/badge/egg-%3E=_1-green.svg?style=flat-square +[egg-image]: https://img.shields.io/badge/egg-%3E=_4-green.svg?style=flat-square [egg-url]: https://github.com/eggjs/egg ## Usage @@ -23,7 +19,7 @@ $ npm install $ npm run dev ``` -**Recommend to use Node >= 8** +**Recommend to use Node >= 18** ## List of examples @@ -64,12 +60,6 @@ You can use `--verbose` to show more infomation $ npm test -- --verbose ``` -### Generate dependencies - -```bash -$ npm run autod -``` - ### Show list of examples ```bash diff --git a/package.json b/package.json index aa3954a9..d0767184 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "chalk": "^1.1.3", "common-bin": "^2.7.1", "globby": "^6.1.0", - "npminstall": "^6.6.2", + "npminstall": "7", "runscript": "^1.5.3", "semver": "^5.3.0" }, @@ -30,7 +30,7 @@ "url": "https://github.com/eggjs/egg/issues" }, "engines": { - "node": ">= 14.17.0" + "node": ">= 18.19.0" }, "license": "MIT", "private": true From 8ceb9181054e5eb93882a4dbbbe0d895a3467bf2 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Sun, 29 Dec 2024 17:02:57 +0800 Subject: [PATCH 03/10] update eggbin --- bin/test.sh | 8 ++++++++ hello-tegg/package.json | 17 ++++++++--------- helloworld/config/config.default.ts | 4 +++- helloworld/package.json | 5 +++-- package.json | 4 ++-- 5 files changed, 24 insertions(+), 14 deletions(-) create mode 100755 bin/test.sh diff --git a/bin/test.sh b/bin/test.sh new file mode 100755 index 00000000..ec4c419f --- /dev/null +++ b/bin/test.sh @@ -0,0 +1,8 @@ +#!/usr/bin/env bash + +cd helloworld +rm -rf node_modules package-lock.json +npm install --registry=https://registry.npmmirror.com +npm run ci +cd helloworld + diff --git a/hello-tegg/package.json b/hello-tegg/package.json index 77890e7b..26180bde 100644 --- a/hello-tegg/package.json +++ b/hello-tegg/package.json @@ -6,7 +6,6 @@ "start": "egg-scripts start", "stop": "egg-scripts stop", "dev": "egg-bin dev", - "debug": "egg-bin debug", "test-local": "egg-bin test", "test": "npm run lint -- --fix && npm run test-local", "cov": "egg-bin cov", @@ -25,17 +24,17 @@ "@eggjs/tegg-plugin": "^3.2.1", "@eggjs/tsconfig": "^1.2.0", "@eggjs/tegg-config": "^3.1.0", - "egg": "^3.9.1", + "egg": "beta", "egg-scripts": "^2.17.0" }, "devDependencies": { - "@types/mocha": "^10.0.1", - "@types/node": "^16.18.10", - "egg-bin": "^6.0.0", - "egg-mock": "^5.4.0", - "eslint": "^8.30.0", - "eslint-config-egg": "^12.1.0", - "typescript": "^4.9.4" + "@types/mocha": "10", + "@types/node": "22", + "@egg/bin": "7", + "@eggjs/mock": "6", + "eslint": "8", + "eslint-config-egg": "14", + "typescript": "5" }, "repository": "git@github.com:eggjs/examples.git" } diff --git a/helloworld/config/config.default.ts b/helloworld/config/config.default.ts index 4e2424d4..b9d2d77a 100644 --- a/helloworld/config/config.default.ts +++ b/helloworld/config/config.default.ts @@ -1,3 +1,5 @@ +import { EggAppConfig } from 'egg'; + export default { keys: 'my secret keys', -}; +} as EggAppConfig; diff --git a/helloworld/package.json b/helloworld/package.json index a34aa063..2682fcf1 100644 --- a/helloworld/package.json +++ b/helloworld/package.json @@ -15,10 +15,11 @@ }, "scripts": { "lint": "eslint . --ext .ts", + "predev": "npm run clean && npm run lint -- --fix", "dev": "egg-bin dev", - "pretest": "npm run lint -- --fix", + "pretest": "npm run clean && npm run lint -- --fix", "test": "egg-bin test", - "preci": "npm run lint", + "preci": "npm run clean && npm run lint", "ci": "egg-bin cov", "postci": "npm run prepublishOnly && npm run clean", "clean": "tsc -b --clean", diff --git a/package.json b/package.json index d0767184..be08d9b8 100644 --- a/package.json +++ b/package.json @@ -16,9 +16,9 @@ }, "scripts": { "lint": "echo 'ignore'", - "test": "./example.js test", + "test": "./bin/test.sh", "test-cn": "./example.js test -c", - "ci": "npm run lint && npm test", + "ci": "npm test", "list": "./example.js list" }, "homepage": "https://github.com/eggjs/examples", From 67321cd46cf08ef015df997cacf2ab84fb6be626 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Sun, 29 Dec 2024 17:23:59 +0800 Subject: [PATCH 04/10] test on tegg v3 --- bin/test.sh | 16 +++++++++++----- hello-tegg/.gitignore | 1 + hello-tegg/app/middleware/trace_method.ts | 2 +- hello-tegg/config/config.default.ts | 5 +++-- hello-tegg/config/plugin.ts | 5 +++-- hello-tegg/package.json | 10 ++++++---- hello-tegg/test/biz/HelloService.test.ts | 9 +++++---- .../test/controller/HelloController.test.ts | 4 ++-- hello-tegg/tsconfig.json | 13 +++++++++++-- 9 files changed, 43 insertions(+), 22 deletions(-) diff --git a/bin/test.sh b/bin/test.sh index ec4c419f..d14ce22d 100755 --- a/bin/test.sh +++ b/bin/test.sh @@ -1,8 +1,14 @@ #!/usr/bin/env bash -cd helloworld -rm -rf node_modules package-lock.json -npm install --registry=https://registry.npmmirror.com -npm run ci -cd helloworld +test() { + echo "Test $1" + cd $1 + pwd + rm -rf node_modules package-lock.json + npm install --registry=https://registry.npmmirror.com + npm run ci + cd .. +} +test helloworld +test hello-tegg diff --git a/hello-tegg/.gitignore b/hello-tegg/.gitignore index 2ba77726..b2eeab3a 100644 --- a/hello-tegg/.gitignore +++ b/hello-tegg/.gitignore @@ -19,3 +19,4 @@ test/**/*.map config/**/*.map *.d.ts *.tsbuildinfo +.egg/ diff --git a/hello-tegg/app/middleware/trace_method.ts b/hello-tegg/app/middleware/trace_method.ts index 6e49a2c9..b5dd8009 100644 --- a/hello-tegg/app/middleware/trace_method.ts +++ b/hello-tegg/app/middleware/trace_method.ts @@ -2,5 +2,5 @@ import { EggContext, Next } from '@eggjs/tegg'; export async function traceMethod(ctx: EggContext, next: Next) { await next(); - ctx.body.data.message += ` (${ctx.method})`; + (ctx.body as any).data.message += ` (${ctx.method})`; } diff --git a/hello-tegg/config/config.default.ts b/hello-tegg/config/config.default.ts index 208980f9..c9f711d1 100644 --- a/hello-tegg/config/config.default.ts +++ b/hello-tegg/config/config.default.ts @@ -1,7 +1,8 @@ -import { EggAppConfig, PowerPartial } from 'egg'; +import { EggAppConfig } from 'egg'; +// import { EggAppConfig, PowerPartial } from 'egg'; export default (appInfo: EggAppConfig) => { - const config = {} as PowerPartial; + const config = {} as EggAppConfig; // override config from framework / plugin config.keys = appInfo.name + '123456'; diff --git a/hello-tegg/config/plugin.ts b/hello-tegg/config/plugin.ts index f7493ab2..b0aede11 100644 --- a/hello-tegg/config/plugin.ts +++ b/hello-tegg/config/plugin.ts @@ -1,6 +1,7 @@ -import { EggPlugin } from 'egg'; +// import { EggPlugin } from 'egg'; -const plugin: EggPlugin = { +// const plugin: EggPlugin = { +const plugin = { tegg: { enable: true, package: '@eggjs/tegg-plugin', diff --git a/hello-tegg/package.json b/hello-tegg/package.json index 26180bde..2d77b5a7 100644 --- a/hello-tegg/package.json +++ b/hello-tegg/package.json @@ -7,10 +7,12 @@ "stop": "egg-scripts stop", "dev": "egg-bin dev", "test-local": "egg-bin test", - "test": "npm run lint -- --fix && npm run test-local", - "cov": "egg-bin cov", + "pretest": "npm run clean && npm run lint -- --fix", + "test": "egg-bin test", "tsc": "tsc -p tsconfig.json", - "ci": "npm run lint && npm run cov && npm run tsc", + "preci": "npm run lint", + "ci": "egg-bin cov", + "postci": "npm run tsc && npm run clean", "lint": "eslint .", "clean": "tsc -b --clean" }, @@ -30,7 +32,7 @@ "devDependencies": { "@types/mocha": "10", "@types/node": "22", - "@egg/bin": "7", + "@eggjs/bin": "7", "@eggjs/mock": "6", "eslint": "8", "eslint-config-egg": "14", diff --git a/hello-tegg/test/biz/HelloService.test.ts b/hello-tegg/test/biz/HelloService.test.ts index 6d77a419..706bfcef 100644 --- a/hello-tegg/test/biz/HelloService.test.ts +++ b/hello-tegg/test/biz/HelloService.test.ts @@ -1,10 +1,11 @@ -import { Context } from 'egg'; -import assert from 'assert'; -import { app } from 'egg-mock/bootstrap'; +import { strict as assert } from 'node:assert'; +// import { EggContext } from 'egg'; +import { app } from '@eggjs/mock/bootstrap'; import { HelloService } from '../../app/biz/HelloService'; describe('test/biz/HelloService.test.ts', () => { - let ctx: Context; + let ctx: any; + // let ctx: EggContext; let helloService: HelloService; beforeEach(async () => { diff --git a/hello-tegg/test/controller/HelloController.test.ts b/hello-tegg/test/controller/HelloController.test.ts index d627e180..8ea27c79 100644 --- a/hello-tegg/test/controller/HelloController.test.ts +++ b/hello-tegg/test/controller/HelloController.test.ts @@ -1,5 +1,5 @@ -import { app } from 'egg-mock/bootstrap'; -import assert from 'assert'; +import { strict as assert } from 'node:assert'; +import { app } from '@eggjs/mock/bootstrap'; describe('test/controller/HelloController.test.ts', () => { it('should work', async () => { diff --git a/hello-tegg/tsconfig.json b/hello-tegg/tsconfig.json index 65fddc82..865b55a3 100644 --- a/hello-tegg/tsconfig.json +++ b/hello-tegg/tsconfig.json @@ -1,6 +1,15 @@ { "extends": "@eggjs/tsconfig", "compilerOptions": { - "baseUrl": "./" - } + "baseUrl": ".", + "strict": true, + "noImplicitAny": true, + "target": "ES2022", + "module": "NodeNext", + "moduleResolution": "NodeNext", + "declaration": false + }, + "exclude": [ + "test" + ] } From 07ac2b38dae277d248f7e6d5cbb7a5e2deae13a6 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Mon, 30 Dec 2024 09:15:51 +0800 Subject: [PATCH 05/10] use egg-scripts v3 --- hello-tegg/package.json | 7 ++++--- helloworld/package.json | 5 ++++- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/hello-tegg/package.json b/hello-tegg/package.json index 2d77b5a7..7aab1a81 100644 --- a/hello-tegg/package.json +++ b/hello-tegg/package.json @@ -14,7 +14,8 @@ "ci": "egg-bin cov", "postci": "npm run tsc && npm run clean", "lint": "eslint .", - "clean": "tsc -b --clean" + "clean": "tsc -b --clean", + "prepublishOnly": "npm run clean && tsc" }, "egg": { "typescript": true @@ -22,12 +23,12 @@ "license": "MIT", "dependencies": { "@eggjs/tegg": "^3.2.1", + "@eggjs/tegg-config": "^3.1.0", "@eggjs/tegg-controller-plugin": "^3.2.1", "@eggjs/tegg-plugin": "^3.2.1", "@eggjs/tsconfig": "^1.2.0", - "@eggjs/tegg-config": "^3.1.0", "egg": "beta", - "egg-scripts": "^2.17.0" + "egg-scripts": "^3.1.0" }, "devDependencies": { "@types/mocha": "10", diff --git a/helloworld/package.json b/helloworld/package.json index 2682fcf1..6beaa1e4 100644 --- a/helloworld/package.json +++ b/helloworld/package.json @@ -1,7 +1,8 @@ { "name": "helloworld", "dependencies": { - "egg": "beta" + "egg": "beta", + "egg-scripts": "^3.1.0" }, "devDependencies": { "@eggjs/bin": "7", @@ -14,6 +15,8 @@ "typescript": "5" }, "scripts": { + "start": "egg-scripts start", + "stop": "egg-scripts stop", "lint": "eslint . --ext .ts", "predev": "npm run clean && npm run lint -- --fix", "dev": "egg-bin dev", From 6dac52fdbc44a95c174e91091233d1ea256a6bcb Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Tue, 31 Dec 2024 21:35:50 +0800 Subject: [PATCH 06/10] use @eggjs/scripts v4 --- hello-tegg/package.json | 8 ++++---- helloworld/package.json | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/hello-tegg/package.json b/hello-tegg/package.json index 7aab1a81..2fff4e94 100644 --- a/hello-tegg/package.json +++ b/hello-tegg/package.json @@ -3,8 +3,8 @@ "private": true, "description": "tegg application example", "scripts": { - "start": "egg-scripts start", - "stop": "egg-scripts stop", + "start": "eggctl start --daemon", + "stop": "eggctl stop", "dev": "egg-bin dev", "test-local": "egg-bin test", "pretest": "npm run clean && npm run lint -- --fix", @@ -22,13 +22,13 @@ }, "license": "MIT", "dependencies": { + "@eggjs/scripts": "^4.0.0", "@eggjs/tegg": "^3.2.1", "@eggjs/tegg-config": "^3.1.0", "@eggjs/tegg-controller-plugin": "^3.2.1", "@eggjs/tegg-plugin": "^3.2.1", "@eggjs/tsconfig": "^1.2.0", - "egg": "beta", - "egg-scripts": "^3.1.0" + "egg": "beta" }, "devDependencies": { "@types/mocha": "10", diff --git a/helloworld/package.json b/helloworld/package.json index 6beaa1e4..3bb1d097 100644 --- a/helloworld/package.json +++ b/helloworld/package.json @@ -1,8 +1,8 @@ { "name": "helloworld", "dependencies": { - "egg": "beta", - "egg-scripts": "^3.1.0" + "@eggjs/scripts": "^4.0.0", + "egg": "beta" }, "devDependencies": { "@eggjs/bin": "7", @@ -15,8 +15,8 @@ "typescript": "5" }, "scripts": { - "start": "egg-scripts start", - "stop": "egg-scripts stop", + "start": "eggctl start --daemon", + "stop": "eggctl stop", "lint": "eslint . --ext .ts", "predev": "npm run clean && npm run lint -- --fix", "dev": "egg-bin dev", From be4466b597ab9a9fce4e1e718def3ce36254a652 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Wed, 12 Mar 2025 22:57:58 +0800 Subject: [PATCH 07/10] f --- .github/workflows/ci.yml | 2 +- hello-tegg/app/biz/package.json | 3 ++- hello-tegg/app/controller/HelloController.ts | 4 ++-- hello-tegg/app/middleware/trace_method.ts | 2 +- hello-tegg/config/config.default.ts | 7 +++--- hello-tegg/config/plugin.ts | 5 ++-- hello-tegg/package.json | 23 +++++++++++-------- hello-tegg/test/biz/HelloService.test.ts | 18 ++++----------- .../test/controller/HelloController.test.ts | 20 ++++++++-------- hello-tegg/tsconfig.json | 9 +------- hello-tegg/tsconfig.prod.json | 8 +++++++ 11 files changed, 47 insertions(+), 54 deletions(-) create mode 100644 hello-tegg/tsconfig.prod.json diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index dc5563d8..26c4fd39 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -12,4 +12,4 @@ jobs: uses: node-modules/github-actions/.github/workflows/node-test.yml@master with: os: 'ubuntu-latest' - version: '18, 20, 22' + version: '20, 22' diff --git a/hello-tegg/app/biz/package.json b/hello-tegg/app/biz/package.json index af5c74ae..5f7913ff 100644 --- a/hello-tegg/app/biz/package.json +++ b/hello-tegg/app/biz/package.json @@ -2,5 +2,6 @@ "name": "biz-module", "eggModule": { "name": "biz" - } + }, + "type": "module" } diff --git a/hello-tegg/app/controller/HelloController.ts b/hello-tegg/app/controller/HelloController.ts index 963088ff..1331cece 100644 --- a/hello-tegg/app/controller/HelloController.ts +++ b/hello-tegg/app/controller/HelloController.ts @@ -9,8 +9,8 @@ import { Inject, } from '@eggjs/tegg'; import { EggLogger } from 'egg'; -import { traceMethod } from 'app/middleware/trace_method'; -import { HelloService } from 'app/biz/HelloService'; +import { traceMethod } from '../middleware/trace_method.js'; +import { HelloService } from '../biz/HelloService.js'; @HTTPController() @Middleware(traceMethod) diff --git a/hello-tegg/app/middleware/trace_method.ts b/hello-tegg/app/middleware/trace_method.ts index b5dd8009..6e49a2c9 100644 --- a/hello-tegg/app/middleware/trace_method.ts +++ b/hello-tegg/app/middleware/trace_method.ts @@ -2,5 +2,5 @@ import { EggContext, Next } from '@eggjs/tegg'; export async function traceMethod(ctx: EggContext, next: Next) { await next(); - (ctx.body as any).data.message += ` (${ctx.method})`; + ctx.body.data.message += ` (${ctx.method})`; } diff --git a/hello-tegg/config/config.default.ts b/hello-tegg/config/config.default.ts index c9f711d1..351e6831 100644 --- a/hello-tegg/config/config.default.ts +++ b/hello-tegg/config/config.default.ts @@ -1,8 +1,7 @@ -import { EggAppConfig } from 'egg'; -// import { EggAppConfig, PowerPartial } from 'egg'; +import { EggAppInfo, EggAppConfig, PowerPartial } from 'egg'; -export default (appInfo: EggAppConfig) => { - const config = {} as EggAppConfig; +export default (appInfo: EggAppInfo) => { + const config = {} as PowerPartial; // override config from framework / plugin config.keys = appInfo.name + '123456'; diff --git a/hello-tegg/config/plugin.ts b/hello-tegg/config/plugin.ts index b0aede11..f7493ab2 100644 --- a/hello-tegg/config/plugin.ts +++ b/hello-tegg/config/plugin.ts @@ -1,7 +1,6 @@ -// import { EggPlugin } from 'egg'; +import { EggPlugin } from 'egg'; -// const plugin: EggPlugin = { -const plugin = { +const plugin: EggPlugin = { tegg: { enable: true, package: '@eggjs/tegg-plugin', diff --git a/hello-tegg/package.json b/hello-tegg/package.json index 2fff4e94..3fa38eed 100644 --- a/hello-tegg/package.json +++ b/hello-tegg/package.json @@ -1,16 +1,16 @@ { "name": "hello-tegg", "private": true, + "type": "module", "description": "tegg application example", "scripts": { "start": "eggctl start --daemon", "stop": "eggctl stop", "dev": "egg-bin dev", - "test-local": "egg-bin test", "pretest": "npm run clean && npm run lint -- --fix", "test": "egg-bin test", - "tsc": "tsc -p tsconfig.json", - "preci": "npm run lint", + "tsc": "tsc -p tsconfig.prod.json", + "preci": "npm run clean && npm run lint", "ci": "egg-bin cov", "postci": "npm run tsc && npm run clean", "lint": "eslint .", @@ -23,21 +23,24 @@ "license": "MIT", "dependencies": { "@eggjs/scripts": "^4.0.0", - "@eggjs/tegg": "^3.2.1", - "@eggjs/tegg-config": "^3.1.0", - "@eggjs/tegg-controller-plugin": "^3.2.1", - "@eggjs/tegg-plugin": "^3.2.1", - "@eggjs/tsconfig": "^1.2.0", - "egg": "beta" + "@eggjs/tegg": "^4.0.0-beta.2", + "@eggjs/tegg-config": "^4.0.0-beta.2", + "@eggjs/tegg-controller-plugin": "^4.0.0-beta.2", + "@eggjs/tegg-plugin": "^4.0.0-beta.2", + "egg": "^4.0.10" }, "devDependencies": { "@types/mocha": "10", "@types/node": "22", + "@eggjs/tsconfig": "2", "@eggjs/bin": "7", "@eggjs/mock": "6", "eslint": "8", "eslint-config-egg": "14", "typescript": "5" }, - "repository": "git@github.com:eggjs/examples.git" + "repository": "git@github.com:eggjs/examples.git", + "engines": { + "node": ">=20.0.0" + } } diff --git a/hello-tegg/test/biz/HelloService.test.ts b/hello-tegg/test/biz/HelloService.test.ts index 706bfcef..fb1c20b4 100644 --- a/hello-tegg/test/biz/HelloService.test.ts +++ b/hello-tegg/test/biz/HelloService.test.ts @@ -1,24 +1,16 @@ -import { strict as assert } from 'node:assert'; -// import { EggContext } from 'egg'; +import assert from 'node:assert/strict'; import { app } from '@eggjs/mock/bootstrap'; -import { HelloService } from '../../app/biz/HelloService'; +import { HelloService } from '../../app/biz/HelloService.js'; describe('test/biz/HelloService.test.ts', () => { - let ctx: any; - // let ctx: EggContext; let helloService: HelloService; - beforeEach(async () => { - ctx = await app.mockModuleContext(); - helloService = await ctx.getEggObject(HelloService); - }); - - afterEach(async () => { - await app.destroyModuleContext(ctx); + before(async () => { + helloService = await app.getEggObject(HelloService); }); it('should work', async () => { const msg = await helloService.hello('killa'); - assert(msg === 'hello, killa'); + assert.equal(msg, 'hello, killa'); }); }); diff --git a/hello-tegg/test/controller/HelloController.test.ts b/hello-tegg/test/controller/HelloController.test.ts index 8ea27c79..d0bc9d2d 100644 --- a/hello-tegg/test/controller/HelloController.test.ts +++ b/hello-tegg/test/controller/HelloController.test.ts @@ -1,18 +1,16 @@ -import { strict as assert } from 'node:assert'; +import assert from 'node:assert/strict'; import { app } from '@eggjs/mock/bootstrap'; describe('test/controller/HelloController.test.ts', () => { it('should work', async () => { - await app.httpRequest() + const res = await app.httpRequest() .get('/hello?name=killa') - .expect(200) - .expect(res => { - assert.deepStrictEqual(res.body, { - success: true, - data: { - message: 'hello, killa (GET)', - }, - }); - }); + .expect(200); + assert.deepEqual(res.body, { + success: true, + data: { + message: 'hello, killa (GET)', + }, + }); }); }); diff --git a/hello-tegg/tsconfig.json b/hello-tegg/tsconfig.json index 865b55a3..c4e60d98 100644 --- a/hello-tegg/tsconfig.json +++ b/hello-tegg/tsconfig.json @@ -2,14 +2,7 @@ "extends": "@eggjs/tsconfig", "compilerOptions": { "baseUrl": ".", - "strict": true, - "noImplicitAny": true, - "target": "ES2022", - "module": "NodeNext", - "moduleResolution": "NodeNext", "declaration": false }, - "exclude": [ - "test" - ] + "exclude": [] } diff --git a/hello-tegg/tsconfig.prod.json b/hello-tegg/tsconfig.prod.json new file mode 100644 index 00000000..c4e60d98 --- /dev/null +++ b/hello-tegg/tsconfig.prod.json @@ -0,0 +1,8 @@ +{ + "extends": "@eggjs/tsconfig", + "compilerOptions": { + "baseUrl": ".", + "declaration": false + }, + "exclude": [] +} From 93a6ea16a8b5968f2ca8094b51085cd2aacc0744 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Fri, 14 Mar 2025 09:15:56 +0800 Subject: [PATCH 08/10] Update bin/test.sh Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- bin/test.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bin/test.sh b/bin/test.sh index d14ce22d..11e54e8a 100755 --- a/bin/test.sh +++ b/bin/test.sh @@ -10,5 +10,5 @@ test() { cd .. } -test helloworld -test hello-tegg +test helloworld || exit 1 +test hello-tegg || exit 1 From 75dd0efda1f29222dbfa58d6dc39b37618633471 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Fri, 14 Mar 2025 09:16:38 +0800 Subject: [PATCH 09/10] Update bin/test.sh Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- bin/test.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/test.sh b/bin/test.sh index 11e54e8a..2103c7b1 100755 --- a/bin/test.sh +++ b/bin/test.sh @@ -2,7 +2,7 @@ test() { echo "Test $1" - cd $1 + cd "$1" || { echo "Error: Directory $1 not found"; return 1; } pwd rm -rf node_modules package-lock.json npm install --registry=https://registry.npmmirror.com From 97125cb635701f930b3fcf34d0b9df248df9218d Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Fri, 14 Mar 2025 09:28:41 +0800 Subject: [PATCH 10/10] add body-parser-example --- bin/test.sh | 9 ++++++--- {bodyParser => body-parser-example}/REAME.md | 0 .../app/controller/home.js | 4 +--- {bodyParser => body-parser-example}/app/router.js | 0 .../config/config.default.js | 2 -- {bodyParser => body-parser-example}/package.json | 10 +++++----- {bodyParser => body-parser-example}/test/home.test.js | 7 ++----- package.json | 2 +- 8 files changed, 15 insertions(+), 19 deletions(-) rename {bodyParser => body-parser-example}/REAME.md (100%) rename {bodyParser => body-parser-example}/app/controller/home.js (88%) rename {bodyParser => body-parser-example}/app/router.js (100%) rename {bodyParser => body-parser-example}/config/config.default.js (92%) rename {bodyParser => body-parser-example}/package.json (61%) rename {bodyParser => body-parser-example}/test/home.test.js (92%) diff --git a/bin/test.sh b/bin/test.sh index 2103c7b1..cd28e216 100755 --- a/bin/test.sh +++ b/bin/test.sh @@ -1,8 +1,10 @@ #!/usr/bin/env bash +set -eux -o pipefail + test() { echo "Test $1" - cd "$1" || { echo "Error: Directory $1 not found"; return 1; } + cd "$1" pwd rm -rf node_modules package-lock.json npm install --registry=https://registry.npmmirror.com @@ -10,5 +12,6 @@ test() { cd .. } -test helloworld || exit 1 -test hello-tegg || exit 1 +test body-parser-example +test helloworld +test hello-tegg diff --git a/bodyParser/REAME.md b/body-parser-example/REAME.md similarity index 100% rename from bodyParser/REAME.md rename to body-parser-example/REAME.md diff --git a/bodyParser/app/controller/home.js b/body-parser-example/app/controller/home.js similarity index 88% rename from bodyParser/app/controller/home.js rename to body-parser-example/app/controller/home.js index 0e5af4eb..d2f35779 100644 --- a/bodyParser/app/controller/home.js +++ b/body-parser-example/app/controller/home.js @@ -1,6 +1,4 @@ -'use strict'; - -const Controller = require('egg').Controller; +const { Controller } = require('egg'); const { xml2js } = require('xml-js'); class HomeController extends Controller { diff --git a/bodyParser/app/router.js b/body-parser-example/app/router.js similarity index 100% rename from bodyParser/app/router.js rename to body-parser-example/app/router.js diff --git a/bodyParser/config/config.default.js b/body-parser-example/config/config.default.js similarity index 92% rename from bodyParser/config/config.default.js rename to body-parser-example/config/config.default.js index c98e876b..c9d58d56 100644 --- a/bodyParser/config/config.default.js +++ b/body-parser-example/config/config.default.js @@ -1,5 +1,3 @@ -'use strict'; - exports.keys = '123456'; exports.bodyParser = { diff --git a/bodyParser/package.json b/body-parser-example/package.json similarity index 61% rename from bodyParser/package.json rename to body-parser-example/package.json index c394a9c0..de504606 100644 --- a/bodyParser/package.json +++ b/body-parser-example/package.json @@ -1,18 +1,18 @@ { - "name": "bodyParser", + "name": "body-parser-example", "version": "1.0.0", "dependencies": { - "egg": "^2", + "egg": "4", "xml-js": "^1.6.9" }, "devDependencies": { - "egg-bin": "^4.3.5", - "egg-mock": "^3.13.1" + "@eggjs/bin": "7", + "@eggjs/mock": "6" }, "scripts": { "dev": "egg-bin dev", "test": "egg-bin test", - "cov": "egg-bin cov" + "ci": "egg-bin cov" }, "private": true } diff --git a/bodyParser/test/home.test.js b/body-parser-example/test/home.test.js similarity index 92% rename from bodyParser/test/home.test.js rename to body-parser-example/test/home.test.js index 55957ba9..780f6199 100644 --- a/bodyParser/test/home.test.js +++ b/body-parser-example/test/home.test.js @@ -1,9 +1,6 @@ -'use strict'; - -const { app } = require('egg-mock/bootstrap'); - -describe('test/app/controller/home.test.js', () => { +const { app } = require('@eggjs/mock/bootstrap'); +describe('test/home.test.js', () => { beforeEach(() => app.mockCsrf()); it('should parse form', () => { diff --git a/package.json b/package.json index be08d9b8..a78affa8 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,7 @@ "url": "https://github.com/eggjs/egg/issues" }, "engines": { - "node": ">= 18.19.0" + "node": ">= 20" }, "license": "MIT", "private": true