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..26c4fd39 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: '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/bin/test.sh b/bin/test.sh new file mode 100755 index 00000000..cd28e216 --- /dev/null +++ b/bin/test.sh @@ -0,0 +1,17 @@ +#!/usr/bin/env bash + +set -eux -o pipefail + +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 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/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/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/config/config.default.ts b/hello-tegg/config/config.default.ts index 208980f9..351e6831 100644 --- a/hello-tegg/config/config.default.ts +++ b/hello-tegg/config/config.default.ts @@ -1,6 +1,6 @@ -import { EggAppConfig, PowerPartial } from 'egg'; +import { EggAppInfo, EggAppConfig, PowerPartial } from 'egg'; -export default (appInfo: EggAppConfig) => { +export default (appInfo: EggAppInfo) => { const config = {} as PowerPartial; // override config from framework / plugin diff --git a/hello-tegg/package.json b/hello-tegg/package.json index 77890e7b..3fa38eed 100644 --- a/hello-tegg/package.json +++ b/hello-tegg/package.json @@ -1,41 +1,46 @@ { "name": "hello-tegg", "private": true, + "type": "module", "description": "tegg application example", "scripts": { - "start": "egg-scripts start", - "stop": "egg-scripts stop", + "start": "eggctl start --daemon", + "stop": "eggctl 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", - "tsc": "tsc -p tsconfig.json", - "ci": "npm run lint && npm run cov && npm run tsc", + "pretest": "npm run clean && npm run lint -- --fix", + "test": "egg-bin test", + "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 .", - "clean": "tsc -b --clean" + "clean": "tsc -b --clean", + "prepublishOnly": "npm run clean && tsc" }, "egg": { "typescript": true }, "license": "MIT", "dependencies": { - "@eggjs/tegg": "^3.2.1", - "@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": "^3.9.1", - "egg-scripts": "^2.17.0" + "@eggjs/scripts": "^4.0.0", + "@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.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", + "@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 6d77a419..fb1c20b4 100644 --- a/hello-tegg/test/biz/HelloService.test.ts +++ b/hello-tegg/test/biz/HelloService.test.ts @@ -1,23 +1,16 @@ -import { Context } from 'egg'; -import assert from 'assert'; -import { app } from 'egg-mock/bootstrap'; -import { HelloService } from '../../app/biz/HelloService'; +import assert from 'node:assert/strict'; +import { app } from '@eggjs/mock/bootstrap'; +import { HelloService } from '../../app/biz/HelloService.js'; describe('test/biz/HelloService.test.ts', () => { - let ctx: Context; 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 d627e180..d0bc9d2d 100644 --- a/hello-tegg/test/controller/HelloController.test.ts +++ b/hello-tegg/test/controller/HelloController.test.ts @@ -1,18 +1,16 @@ -import { app } from 'egg-mock/bootstrap'; -import assert from '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 65fddc82..c4e60d98 100644 --- a/hello-tegg/tsconfig.json +++ b/hello-tegg/tsconfig.json @@ -1,6 +1,8 @@ { "extends": "@eggjs/tsconfig", "compilerOptions": { - "baseUrl": "./" - } + "baseUrl": ".", + "declaration": false + }, + "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": [] +} 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..b9d2d77a --- /dev/null +++ b/helloworld/config/config.default.ts @@ -0,0 +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 800cf2f8..3bb1d097 100644 --- a/helloworld/package.json +++ b/helloworld/package.json @@ -1,16 +1,33 @@ { "name": "helloworld", "dependencies": { - "egg": "^1.10.1" + "@eggjs/scripts": "^4.0.0", + "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": { + "start": "eggctl start --daemon", + "stop": "eggctl stop", + "lint": "eslint . --ext .ts", + "predev": "npm run clean && npm run lint -- --fix", "dev": "egg-bin dev", + "pretest": "npm run clean && npm run lint -- --fix", "test": "egg-bin test", - "cov": "egg-bin cov" + "preci": "npm run clean && 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" + ] +} diff --git a/package.json b/package.json index aa3954a9..a78affa8 100644 --- a/package.json +++ b/package.json @@ -10,15 +10,15 @@ "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" }, "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", @@ -30,7 +30,7 @@ "url": "https://github.com/eggjs/egg/issues" }, "engines": { - "node": ">= 14.17.0" + "node": ">= 20" }, "license": "MIT", "private": true