Skip to content

Commit

Permalink
f
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Jan 7, 2024
1 parent 12f47cb commit 760db1b
Show file tree
Hide file tree
Showing 12 changed files with 123 additions and 2 deletions.
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: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
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"
}
}

0 comments on commit 760db1b

Please sign in to comment.