Skip to content

Commit

Permalink
feat: intergration with egg-ts-helper (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
whxaxes committed Feb 15, 2019
1 parent 5374c59 commit 263cfd1
Show file tree
Hide file tree
Showing 26 changed files with 276 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ test/fixtures/ts/node_modules/aliyun-egg/
!test/fixtures/example/node_modules/
!test/fixtures/example-ts-error-stack/node_modules/
!test/fixtures/egg-require/node_modules/
test/fixtures/example-ts-ets/typings/
!test/fixtures/example-ts-ets/node_modules/


**/run/*.json
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ $ egg-bin dev
- `--cluster` worker process number, skip this argvs will start only `1` worker, provide this without value will start `cpu` count worker.
- `--sticky` start a sticky cluster server, default to `false`.
- `--typescript` / `--ts` enable typescript support, default to `false`. Also support read from `package.json`'s `egg.typescript`.
- `--declarations` / `--dts` enable [egg-ts-helper](https://github.com/whxaxes/egg-ts-helper) support, default to `false`. Also support read from `package.json`'s `egg.declarations`.
- `--require` will add to `execArgv`, support multiple. Also support read from `package.json`'s `egg.require`

### debug
Expand Down
23 changes: 20 additions & 3 deletions lib/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ class Command extends BaseCommand {
alias: 'ts',
default: undefined,
},

declarations: {
description: 'whether create dts, will load `egg-ts-helper/register`',
type: 'boolean',
alias: 'dts',
default: undefined,
},
};
}

Expand All @@ -43,16 +50,21 @@ class Command extends BaseCommand {
execArgvObj.require = execArgvObj.require || [];

// read `egg.typescript` from package.json if not pass argv
if (argv.typescript === undefined && eggInfo && eggInfo.typescript === true) {
argv.typescript = true;
if (argv.typescript === undefined && eggInfo) {
argv.typescript = eggInfo.typescript === true;
}

// read `egg.declarations` from package.json if not pass argv
if (argv.declarations === undefined && eggInfo) {
argv.declarations = eggInfo.declarations === true;
}

// read `egg.require` from package.json
if (eggInfo && eggInfo.require && Array.isArray(eggInfo.require)) {
execArgvObj.require = execArgvObj.require.concat(eggInfo.require);
}

// execArgv
// load ts-node
if (argv.typescript) {
execArgvObj.require.push(require.resolve('ts-node/register'));

Expand All @@ -66,6 +78,11 @@ class Command extends BaseCommand {
env.TS_NODE_FILES = process.env.TS_NODE_FILES || 'true';
}

// load egg-ts-helper
if (argv.declarations) {
execArgvObj.require.push(require.resolve('egg-ts-helper/register'));
}

return context;
}
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"common-bin": "^2.7.3",
"debug": "^3.1.0",
"detect-port": "^1.2.3",
"egg-ts-helper": "^1.22.0",
"egg-utils": "^2.4.0",
"espower-typescript": "^9.0.1",
"globby": "^8.0.1",
Expand Down
17 changes: 17 additions & 0 deletions test/fixtures/example-ts-ets/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import * as path from 'path';

export default (app: Egg.Application) => {
let directory = path.resolve(app.baseDir, './app/model');
app.loader.loadToApp(directory, 'model', {
caseStyle: 'upper',
directory,
});

directory = path.resolve(app.baseDir, './app/custom');
app.loader.loadToApp(directory, 'custom', {
caseStyle: 'lower',
directory,
});

app.customLog();
};
17 changes: 17 additions & 0 deletions test/fixtures/example-ts-ets/app/controller/home.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Controller } from 'egg';

export default class HomeController extends Controller {
async index() {
const { ctx, app } = this;
ctx.customLog();
app.customLog();
ctx.request.customLog();
ctx.response.customLog();
ctx.helper.customLog();
console.info(app.config.otherBizConfig.type);
console.info(await ctx.service.db.fetch());
console.info(await app.model.User.get());
console.info('biz config', ctx.app.config.biz.type);
ctx.body = 'ok';
}
}
5 changes: 5 additions & 0 deletions test/fixtures/example-ts-ets/app/custom/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default () => {
return {
abc: '123',
};
};
5 changes: 5 additions & 0 deletions test/fixtures/example-ts-ets/app/extend/application.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
customLog() {
console.info('application log');
},
};
5 changes: 5 additions & 0 deletions test/fixtures/example-ts-ets/app/extend/context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
customLog() {
console.info('context log');
},
};
5 changes: 5 additions & 0 deletions test/fixtures/example-ts-ets/app/extend/helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
customLog() {
console.info('helper log');
},
};
5 changes: 5 additions & 0 deletions test/fixtures/example-ts-ets/app/extend/request.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
customLog() {
console.info('request log');
},
};
5 changes: 5 additions & 0 deletions test/fixtures/example-ts-ets/app/extend/response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
customLog() {
console.info('response log');
},
};
5 changes: 5 additions & 0 deletions test/fixtures/example-ts-ets/app/middleware/access.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default () => {
return async (_ctx, next) => {
await next();
};
};
7 changes: 7 additions & 0 deletions test/fixtures/example-ts-ets/app/model/User.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function() {
return {
get() {
return 'model get';
},
};
}
8 changes: 8 additions & 0 deletions test/fixtures/example-ts-ets/app/router.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Application } from 'egg';

export default function(app: Application) {
const { router, controller } = app;

console.info(app.custom.test.abc);
router.get('/', controller.home.index);
}
7 changes: 7 additions & 0 deletions test/fixtures/example-ts-ets/app/service/db.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Service } from 'egg';

export default class DbService extends Service {
async fetch() {
return 'service fetch';
}
}
18 changes: 18 additions & 0 deletions test/fixtures/example-ts-ets/config/config.default.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export default function() {
// built-in config
const config: Egg.PowerPartial<Egg.EggAppConfig> = {};

config.keys = '123123';

// biz config
const bizConfig = {
biz: {
type: 'biz',
},
};

return {
...config as {},
...bizConfig,
};
}
14 changes: 14 additions & 0 deletions test/fixtures/example-ts-ets/config/config.local.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { EggAppConfig, PowerPartial } from 'egg';

export default function() {
// built-in config
const config: PowerPartial<EggAppConfig> = {};

config.keys = '123123';

config.biz = {
type: 'local',
};

return config;
}
7 changes: 7 additions & 0 deletions test/fixtures/example-ts-ets/config/config.prod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function() {
return {
otherBizConfig: {
type: 'value',
},
};
}
6 changes: 6 additions & 0 deletions test/fixtures/example-ts-ets/config/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { EggPlugin } from 'egg';

const plugin: EggPlugin = {
};

export default plugin;
8 changes: 8 additions & 0 deletions test/fixtures/example-ts-ets/node_modules/egg/index.js

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

3 changes: 3 additions & 0 deletions test/fixtures/example-ts-ets/node_modules/egg/package.json

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

7 changes: 7 additions & 0 deletions test/fixtures/example-ts-ets/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "real-app",
"egg": {
"typescript": true,
"declarations": false
}
}
21 changes: 21 additions & 0 deletions test/fixtures/example-ts-ets/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"compilerOptions": {
"strict": true,
"target": "es2017",
"module": "commonjs",
"moduleResolution": "node",
"noImplicitAny": false,
"baseUrl": ".",
"paths": {
"egg": [
"../../../node_modules/egg"
],
"egg-mock": [
"../../../node_modules/egg-mock"
]
}
},
"include": [
"./**/*"
]
}
19 changes: 19 additions & 0 deletions test/fixtures/example-ts-ets/tshelper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';

module.exports = {
watchDirs: {
model: {
path: 'app/model', // dir path
generator: 'class', // generator name
interface: 'IModel', // interface name
declareTo: 'Application.model', // declare to this interface
interfaceHandle: val => `ReturnType<typeof ${val}>`, // interfaceHandle
},

custom: {
path: 'app/custom', // dir path
generator: 'auto', // generator name
declareTo: 'Application.custom', // declare to this interface
},
},
};
59 changes: 58 additions & 1 deletion test/ts.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
const path = require('path');
const coffee = require('coffee');
const mm = require('mm');
const fs = require('fs');
const rimraf = require('mz-modules/rimraf');

describe('test/ts.test.js', () => {
const eggBin = require.resolve('../bin/egg-bin');
Expand Down Expand Up @@ -152,12 +154,67 @@ describe('test/ts.test.js', () => {

it('should cov app', () => {
return coffee.fork(eggBin, [ 'cov' ], { cwd })
.debug()
// .debug()
.expect('stdout', /hi, egg, 123456/)
.expect('stdout', /ts env: true/)
.expect('stdout', process.env.NYC_ROOT_ID ? /Coverage summary/ : /Statements.*100%/)
.expect('code', 0)
.end();
});
});

describe('egg.declarations = true', () => {
if (process.env.EGG_VERSION && process.env.EGG_VERSION === '1') {
console.log('skip egg@1');
return;
}

let pkgJson;
before(() => {
cwd = path.join(__dirname, './fixtures/example-ts-ets');
pkgJson = JSON.parse(fs.readFileSync(path.resolve(cwd, './package.json')).toString());
});

beforeEach(() => rimraf(path.resolve(cwd, './typings')));

afterEach(() => {
pkgJson.egg.declarations = false;
fs.writeFileSync(path.resolve(cwd, './package.json'), JSON.stringify(pkgJson, null, 2));
});

it('should load egg-ts-helper with dts flag', () => {
return coffee.fork(eggBin, [ 'dev', '--dts' ], { cwd })
// .debug()
.expect('stdout', /application log/)
.expect('stdout', /"typescript":true/)
.expect('stdout', /started/)
.expect('code', 0)
.end();
});

it('should load egg-ts-helper with egg.declarations = true', () => {
pkgJson.egg.declarations = true;
fs.writeFileSync(path.resolve(cwd, './package.json'), JSON.stringify(pkgJson, null, 2));

return coffee.fork(eggBin, [ 'dev' ], { cwd })
// .debug()
.expect('stdout', /application log/)
.expect('stdout', /"typescript":true/)
.expect('stdout', /"declarations":true/)
.expect('stdout', /started/)
.expect('code', 0)
.end();
});

it('should not load egg-ts-helper without flag and egg.declarations', () => {
return coffee.fork(eggBin, [ 'dev' ], { cwd })
// .debug()
.expect('stdout', /"typescript":true/)
.notExpect('stdout', /application log/)
.notExpect('stdout', /"declarations":true/)
.notExpect('stdout', /started/)
.expect('code', 1)
.end();
});
});
});

0 comments on commit 263cfd1

Please sign in to comment.