Skip to content

Commit

Permalink
feat: should run ets on pkg.egg.declarations = true (#248)
Browse files Browse the repository at this point in the history
use c8@9

closes #242
closes #246
  • Loading branch information
fengmk2 authored Jan 7, 2024
1 parent 1845b15 commit 018801a
Show file tree
Hide file tree
Showing 20 changed files with 148 additions and 25 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ name: CI

on:
push:
branches: [ master, 5.x ]
branches: [ master ]

pull_request:
branches: [ master, 5.x ]
branches: [ master ]

jobs:
Job:
name: Node.js
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'
4 changes: 1 addition & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,5 @@ cmd/*.js
!bin/postinstall.js
.eslintcache
dist
test/fixtures/example-declarations/typings/
!test/fixtures/example-declarations/node_modules
2 changes: 0 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
"--",
"--inspect-brk"
],
"protocol": "auto",
"port": 9229,
"autoAttachChildProcesses": true
}
]
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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": {
Expand All @@ -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"
Expand Down
7 changes: 5 additions & 2 deletions scripts/postinstall.js
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/cov.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
21 changes: 20 additions & 1 deletion src/middleware/global_options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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',
Expand Down Expand Up @@ -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');
Expand Down
5 changes: 4 additions & 1 deletion test/cmd/cov.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/)
Expand Down
12 changes: 12 additions & 0 deletions test/cmd/dev.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
10 changes: 10 additions & 0 deletions test/fixtures/example-declarations/app/controller/home.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const { Controller } = require('egg');

class HomeController extends Controller {
async index() {
const { ctx } = this;
ctx.body = 'hi, egg';
}
}

module.exports = HomeController;
7 changes: 7 additions & 0 deletions test/fixtures/example-declarations/app/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* @param {Egg.Application} app - egg application
*/
module.exports = app => {
const { router, controller } = app;
router.get('/', controller.home.index);
};
28 changes: 28 additions & 0 deletions test/fixtures/example-declarations/config/config.default.js
Original file line number Diff line number Diff line change
@@ -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,
};
};
7 changes: 7 additions & 0 deletions test/fixtures/example-declarations/config/plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/** @type Egg.EggPlugin */
module.exports = {
// had enabled by egg
// static: {
// enable: true,
// }
};
5 changes: 5 additions & 0 deletions test/fixtures/example-declarations/jsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"include": [
"**/*"
]
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions test/fixtures/example-declarations/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "example-declarations",
"version": "1.0.0",
"description": "",
"private": true,
"egg": {
"declarations": true,
"framework": "aliyun-egg"
}
}
2 changes: 1 addition & 1 deletion test/fixtures/example-ts-custom-compiler/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "example-ts-custom-compiler",
"dependencies": {
"ts-node": "10.9.0"
"ts-node": "10.9.2"
}
}
16 changes: 8 additions & 8 deletions test/ts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

Expand All @@ -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 [email protected].0 --no-save', { cwd });
await runscript('npx npminstall [email protected].2 --no-save', { cwd });
} else {
await runscript('npx npminstall -c [email protected].0 --no-save', { cwd });
await runscript('npx npminstall -c [email protected].2 --no-save', { cwd });
}

// copy egg to node_modules
Expand All @@ -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);
});

Expand All @@ -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 [email protected].0 --no-save', { cwd });
await runscript('npx npminstall [email protected].2 --no-save', { cwd });
} else {
await runscript('npx npminstall -c [email protected].0 --no-save', { cwd });
await runscript('npx npminstall -c [email protected].2 --no-save', { cwd });
}

// copy egg to node_modules
Expand All @@ -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);
});

Expand Down Expand Up @@ -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);
});

Expand Down

1 comment on commit 018801a

@vercel
Copy link

@vercel vercel bot commented on 018801a Jan 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

egg-bin – ./

egg-bin-git-master-no-veronica.vercel.app
egg-bin-no-veronica.vercel.app
egg-bin.vercel.app

Please sign in to comment.