From 018801a241324626748c0d9ea90573349cd3d2fa Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Sun, 7 Jan 2024 20:42:29 +0800 Subject: [PATCH] feat: should run ets on pkg.egg.declarations = true (#248) use c8@9 closes https://github.com/eggjs/egg-bin/issues/242 closes https://github.com/eggjs/egg-bin/issues/246 --- .github/workflows/nodejs.yml | 6 ++-- .github/workflows/release.yml | 4 +-- .gitignore | 2 ++ .vscode/launch.json | 2 -- package.json | 6 ++-- scripts/postinstall.js | 7 +++-- src/cmd/cov.ts | 2 +- src/middleware/global_options.ts | 21 +++++++++++++- test/cmd/cov.test.ts | 5 +++- test/cmd/dev.test.ts | 12 ++++++++ .../app/controller/home.js | 10 +++++++ .../example-declarations/app/router.js | 7 +++++ .../config/config.default.js | 28 +++++++++++++++++++ .../example-declarations/config/plugin.js | 7 +++++ .../example-declarations/jsconfig.json | 5 ++++ .../node_modules/aliyun-egg/index.js | 15 ++++++++++ .../node_modules/aliyun-egg/package.json | 6 ++++ .../example-declarations/package.json | 10 +++++++ .../example-ts-custom-compiler/package.json | 2 +- test/ts.test.ts | 16 +++++------ 20 files changed, 148 insertions(+), 25 deletions(-) create mode 100644 test/fixtures/example-declarations/app/controller/home.js create mode 100644 test/fixtures/example-declarations/app/router.js create mode 100644 test/fixtures/example-declarations/config/config.default.js create mode 100644 test/fixtures/example-declarations/config/plugin.js create mode 100644 test/fixtures/example-declarations/jsconfig.json create mode 100644 test/fixtures/example-declarations/node_modules/aliyun-egg/index.js create mode 100644 test/fixtures/example-declarations/node_modules/aliyun-egg/package.json create mode 100644 test/fixtures/example-declarations/package.json diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index de34f4f7..ad35cba1 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -2,10 +2,10 @@ name: CI on: push: - branches: [ master, 5.x ] + branches: [ master ] pull_request: - branches: [ master, 5.x ] + branches: [ master ] jobs: Job: @@ -13,4 +13,4 @@ jobs: uses: node-modules/github-actions/.github/workflows/node-test.yml@master with: os: 'ubuntu-latest, macos-latest, windows-latest' - version: '14, 16, 18, 20' + version: '16, 18, 20' diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 996d63b6..970aedc7 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -6,9 +6,7 @@ on: jobs: release: name: Node.js - uses: node-modules/github-actions/.github/workflows/node-release.yml@master + uses: eggjs/github-actions/.github/workflows/node-release.yml@master secrets: NPM_TOKEN: ${{ secrets.NPM_TOKEN }} GIT_TOKEN: ${{ secrets.GIT_TOKEN }} - with: - checkTest: false diff --git a/.gitignore b/.gitignore index e57d269c..5243f439 100644 --- a/.gitignore +++ b/.gitignore @@ -33,3 +33,5 @@ cmd/*.js !bin/postinstall.js .eslintcache dist +test/fixtures/example-declarations/typings/ +!test/fixtures/example-declarations/node_modules diff --git a/.vscode/launch.json b/.vscode/launch.json index 47168960..3db70ec3 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -15,8 +15,6 @@ "--", "--inspect-brk" ], - "protocol": "auto", - "port": 9229, "autoAttachChildProcesses": true } ] diff --git a/package.json b/package.json index 51146213..9ef1381e 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,7 @@ "@artus-cli/artus-cli": "^0.2.9", "@artus-cli/plugin-autocomplete": "^0.1.1", "@artus-cli/plugin-version": "^1.0.1", - "c8": "^7.12.0", + "c8": "^9.0.0", "detect-port": "^1.3.0", "egg-ts-helper": "^1.30.3", "egg-utils": "^2.4.1", @@ -26,7 +26,7 @@ "mocha": "^10.2.0", "mochawesome-with-mocha": "^7.1.3", "runscript": "^1.5.3", - "ts-node": "^10.9.1", + "ts-node": "^10.9.2", "tsconfig-paths": "^4.1.2" }, "peerDependencies": { @@ -51,7 +51,7 @@ "esbuild": "^0.17.7", "esbuild-register": "^3.4.2", "eslint": "^8.16.0", - "eslint-config-egg": "^12.0.0", + "eslint-config-egg": "^13.1.0", "git-contributor": "2", "npminstall": "^7.5.0", "typescript": "^5.2.2" diff --git a/scripts/postinstall.js b/scripts/postinstall.js index d4517037..a653139a 100644 --- a/scripts/postinstall.js +++ b/scripts/postinstall.js @@ -20,9 +20,12 @@ debug('npmRunRoot: %o', npmRunRoot); if (npmRunRoot) { const pkgFile = path.join(npmRunRoot, 'package.json'); - if (fs.existsSync(pkgFile)) { + const pkgFileExists = fs.existsSync(pkgFile); + debug('pkgFile: %o exists: %o', pkgFile, pkgFileExists); + if (pkgFileExists) { const pkg = require(pkgFile); - if (!pkg.egg || !pkg.egg.typescript) return; + // should set pkg.egg.declarations = true or pkg.egg.typescript = true + if (!pkg.egg?.typescript && !pkg.egg?.declarations) return; // ignore eggModule and framework // framework package.json: // "egg": { diff --git a/src/cmd/cov.ts b/src/cmd/cov.ts index ccdf023d..b13802d7 100644 --- a/src/cmd/cov.ts +++ b/src/cmd/cov.ts @@ -75,7 +75,7 @@ export class CovCommand extends TestCommand { ]); for (const exclude of excludes) { c8Args.push('-x'); - c8Args.push(`'${exclude}'`); + c8Args.push(exclude); } const c8File = require.resolve('c8/bin/c8.js'); const outputDir = path.join(this.base, 'node_modules/.c8_output'); diff --git a/src/middleware/global_options.ts b/src/middleware/global_options.ts index 57014215..079a5125 100644 --- a/src/middleware/global_options.ts +++ b/src/middleware/global_options.ts @@ -5,12 +5,13 @@ import { Inject, ApplicationLifecycle, LifecycleHook, LifecycleHookUnit, Program, CommandContext, } from '@artus-cli/artus-cli'; +import runscript from 'runscript'; import { addNodeOptionsToEnv, readPackageJSON, hasTsConfig } from '../utils'; const debug = debuglog('egg-bin:midddleware:global_options'); @LifecycleHookUnit() -export default class implements ApplicationLifecycle { +export default class GlobalOptions implements ApplicationLifecycle { @Inject() private readonly program: Program; @@ -23,6 +24,11 @@ export default class implements ApplicationLifecycle { type: 'string', alias: 'baseDir', }, + declarations: { + description: 'whether create typings, will add `--require egg-ts-helper/register`', + type: 'boolean', + alias: 'dts', + }, typescript: { description: 'whether enable typescript support', type: 'boolean', @@ -117,6 +123,19 @@ export default class implements ApplicationLifecycle { addNodeOptionsToEnv(`--loader ${esmLoader}`, ctx.env); } + if (ctx.args.declarations === undefined) { + if (typeof ctx.args.pkgEgg.declarations === 'boolean') { + // read `egg.declarations` from package.json if not pass argv + ctx.args.declarations = ctx.args.pkgEgg.declarations; + debug('detect declarations from pkg.egg.declarations=%o', ctx.args.pkgEgg.declarations); + } + } + if (ctx.args.declarations) { + const etsBin = require.resolve('egg-ts-helper/dist/bin'); + debug('run ets first: %o', etsBin); + await runscript(`node ${etsBin}`); + } + debug('set NODE_OPTIONS: %o', ctx.env.NODE_OPTIONS); debug('ctx.args: %o', ctx.args); debug('enter next'); diff --git a/test/cmd/cov.test.ts b/test/cmd/cov.test.ts index b36ee428..722efcec 100644 --- a/test/cmd/cov.test.ts +++ b/test/cmd/cov.test.ts @@ -50,7 +50,10 @@ describe('test/cmd/cov.test.ts', () => { it('should success with COV_EXCLUDES', async () => { await coffee.fork(eggBin, [ 'cov', '--ts=false' ], { cwd, - env: { TESTS: 'test/**/*.test.js', COV_EXCLUDES: 'ignore/*' }, + env: { + TESTS: 'test/**/*.test.js', + COV_EXCLUDES: 'ignore/*', + }, }) // .debug() .expect('stdout', /should success/) diff --git a/test/cmd/dev.test.ts b/test/cmd/dev.test.ts index da2318eb..59b2a2bf 100644 --- a/test/cmd/dev.test.ts +++ b/test/cmd/dev.test.ts @@ -33,6 +33,18 @@ describe('test/cmd/dev.test.ts', () => { .end(); }); + it('should dev start work with declarations = true', () => { + const cwd = path.join(fixtures, 'example-declarations'); + return coffee.fork(eggBin, [ 'dev' ], { cwd }) + .debug() + .expect('stdout', /"workers":1/) + .expect('stdout', /"baseDir":".*?example-declarations"/) + .expect('stdout', /"framework":".*?egg"/) + .expect('stdout', /\[egg-ts-helper\] create typings/) + .expect('code', 0) + .end(); + }); + it('should startCluster with --port', () => { return coffee.fork(eggBin, [ 'dev', '--port', '6001' ], { cwd }) // .debug() diff --git a/test/fixtures/example-declarations/app/controller/home.js b/test/fixtures/example-declarations/app/controller/home.js new file mode 100644 index 00000000..3fb22d31 --- /dev/null +++ b/test/fixtures/example-declarations/app/controller/home.js @@ -0,0 +1,10 @@ +const { Controller } = require('egg'); + +class HomeController extends Controller { + async index() { + const { ctx } = this; + ctx.body = 'hi, egg'; + } +} + +module.exports = HomeController; diff --git a/test/fixtures/example-declarations/app/router.js b/test/fixtures/example-declarations/app/router.js new file mode 100644 index 00000000..59af36dc --- /dev/null +++ b/test/fixtures/example-declarations/app/router.js @@ -0,0 +1,7 @@ +/** + * @param {Egg.Application} app - egg application + */ +module.exports = app => { + const { router, controller } = app; + router.get('/', controller.home.index); +}; diff --git a/test/fixtures/example-declarations/config/config.default.js b/test/fixtures/example-declarations/config/config.default.js new file mode 100644 index 00000000..4f8f9a14 --- /dev/null +++ b/test/fixtures/example-declarations/config/config.default.js @@ -0,0 +1,28 @@ +/* eslint valid-jsdoc: "off" */ + +/** + * @param {Egg.EggAppInfo} appInfo app info + */ +module.exports = appInfo => { + /** + * built-in config + * @type {Egg.EggAppConfig} + **/ + const config = exports = {}; + + // use for cookie sign key, should change to your own and keep security + config.keys = appInfo.name + '_1704604037320_6202'; + + // add your middleware config here + config.middleware = []; + + // add your user config here + const userConfig = { + // myAppName: 'egg', + }; + + return { + ...config, + ...userConfig, + }; +}; diff --git a/test/fixtures/example-declarations/config/plugin.js b/test/fixtures/example-declarations/config/plugin.js new file mode 100644 index 00000000..a85464eb --- /dev/null +++ b/test/fixtures/example-declarations/config/plugin.js @@ -0,0 +1,7 @@ +/** @type Egg.EggPlugin */ +module.exports = { + // had enabled by egg + // static: { + // enable: true, + // } +}; diff --git a/test/fixtures/example-declarations/jsconfig.json b/test/fixtures/example-declarations/jsconfig.json new file mode 100644 index 00000000..1bbed3f2 --- /dev/null +++ b/test/fixtures/example-declarations/jsconfig.json @@ -0,0 +1,5 @@ +{ + "include": [ + "**/*" + ] +} \ No newline at end of file diff --git a/test/fixtures/example-declarations/node_modules/aliyun-egg/index.js b/test/fixtures/example-declarations/node_modules/aliyun-egg/index.js new file mode 100644 index 00000000..dc41c309 --- /dev/null +++ b/test/fixtures/example-declarations/node_modules/aliyun-egg/index.js @@ -0,0 +1,15 @@ +'use strict'; + +exports.startCluster = options => { + console.log('options: %j', options); + if (process.execArgv.length) { + console.log('process.execArgv:', process.execArgv); + } + console.log('NODE_ENV: %s', process.env.NODE_ENV); + + // make sure exit + setTimeout(function() { + console.log('exist by setTimeout'); + process.exit(0); + }, 3000); +}; diff --git a/test/fixtures/example-declarations/node_modules/aliyun-egg/package.json b/test/fixtures/example-declarations/node_modules/aliyun-egg/package.json new file mode 100644 index 00000000..4e36e661 --- /dev/null +++ b/test/fixtures/example-declarations/node_modules/aliyun-egg/package.json @@ -0,0 +1,6 @@ +{ + "name": "aliyun-egg", + "dependencies": { + "egg": "*" + } +} diff --git a/test/fixtures/example-declarations/package.json b/test/fixtures/example-declarations/package.json new file mode 100644 index 00000000..80f6e0ef --- /dev/null +++ b/test/fixtures/example-declarations/package.json @@ -0,0 +1,10 @@ +{ + "name": "example-declarations", + "version": "1.0.0", + "description": "", + "private": true, + "egg": { + "declarations": true, + "framework": "aliyun-egg" + } +} diff --git a/test/fixtures/example-ts-custom-compiler/package.json b/test/fixtures/example-ts-custom-compiler/package.json index a9c45d33..82de9e41 100644 --- a/test/fixtures/example-ts-custom-compiler/package.json +++ b/test/fixtures/example-ts-custom-compiler/package.json @@ -1,6 +1,6 @@ { "name": "example-ts-custom-compiler", "dependencies": { - "ts-node": "10.9.0" + "ts-node": "10.9.2" } } diff --git a/test/ts.test.ts b/test/ts.test.ts index 2cdfc6a5..c1a312fc 100644 --- a/test/ts.test.ts +++ b/test/ts.test.ts @@ -245,7 +245,7 @@ describe('test/ts.test.ts', () => { }) // .debug() .end(); - assert.match(stderr, /ts-node@10\.9\.0/); + assert.match(stderr, /ts-node@10\.\d+\.\d+/); assert.equal(code, 0); }); @@ -257,9 +257,9 @@ describe('test/ts.test.ts', () => { await fs.rm(path.join(cwd, 'node_modules'), { force: true, recursive: true }); if (process.env.CI) { // dont use npmmirror.com on CI - await runscript('npx npminstall ts-node@10.9.0 --no-save', { cwd }); + await runscript('npx npminstall ts-node@10.9.2 --no-save', { cwd }); } else { - await runscript('npx npminstall -c ts-node@10.9.0 --no-save', { cwd }); + await runscript('npx npminstall -c ts-node@10.9.2 --no-save', { cwd }); } // copy egg to node_modules @@ -278,7 +278,7 @@ describe('test/ts.test.ts', () => { }) // .debug() .end(); - assert.match(stderr, /ts-node@10\.9\.0/); + assert.match(stderr, /ts-node@10\.9\.2/); assert.equal(code, 0); }); @@ -289,9 +289,9 @@ describe('test/ts.test.ts', () => { await fs.rm(path.join(cwd, 'node_modules'), { force: true, recursive: true }); if (process.env.CI) { // dont use npmmirror.com on CI - await runscript('npx npminstall ts-node@10.9.0 --no-save', { cwd }); + await runscript('npx npminstall ts-node@10.9.2 --no-save', { cwd }); } else { - await runscript('npx npminstall -c ts-node@10.9.0 --no-save', { cwd }); + await runscript('npx npminstall -c ts-node@10.9.2 --no-save', { cwd }); } // copy egg to node_modules @@ -308,7 +308,7 @@ describe('test/ts.test.ts', () => { }) // .debug() .end(); - assert.doesNotMatch(stderr, /ts-node@10\.9\.0/); + assert.doesNotMatch(stderr, /ts-node@10\.9\.2/); assert.equal(code, 0); }); @@ -405,7 +405,7 @@ describe('test/ts.test.ts', () => { }) // .debug() .end(); - assert.match(stdout, /ts-node@10\.9\.0/); + assert.match(stdout, /ts-node@10\.\d+\.\d+/); assert.equal(code, 0); });