Skip to content

Commit

Permalink
Merge da15598 into 60b0ece
Browse files Browse the repository at this point in the history
  • Loading branch information
whxaxes committed Dec 12, 2018
2 parents 60b0ece + da15598 commit 21f781a
Show file tree
Hide file tree
Showing 19 changed files with 606 additions and 245 deletions.
91 changes: 86 additions & 5 deletions README.md
Expand Up @@ -135,7 +135,7 @@ In `package.json`
Generator is the core of `egg-ts-helper`. ( build-in generator: https://github.com/whxaxes/egg-ts-helper/tree/master/src/generators
)

In startup, `egg-ts-helper` executes all watcher's generator, the generator will traverse the directory and collect modules, then return fields `dist`( d.ts file path ) and `content`( import these modules and defined to interface of egg. ) to `egg-ts-helper`. `egg-ts-helper` will create `d.ts` by `dist` and `content` fields.
On `egg-ts-helper` startup, it will executes all watcher's generator, and the generator will traverse the directory and collect modules, then return fields `dist`( d.ts file path ) and `content`( import these modules and defined to interface of egg. ) to `egg-ts-helper`. `egg-ts-helper` will create `d.ts` by `dist` and `content` fields.

You can configure watcher in option `watchDirs` ( see `getDefaultWatchDirs` method in https://github.com/whxaxes/egg-ts-helper/blob/master/src/index.ts to know default config of watcher ). `egg-ts-helper` watch these directories `app/extend`,`app/controller`,`app/service`, `app/config`, `app/middleware`, `app/model` by default. When the files under these folders is changed, the `d.ts` will be created ( config.watch should set to true ) .

Expand Down Expand Up @@ -254,32 +254,72 @@ Should set `declareTo` if without `interface`.

#### generator `string`

The name of generator, ( the generator will be executed and recreate `d.ts` when the file is changed. ) but I recommend to use `class` `function` `object` only, because the other generator is not suitable for custom loader.
The name of generator, ( the generator will be executed and recreate `d.ts` when the file is changed. ) but I recommend to use `class` `function` `object` `auto` only, because the other generator is not suitable for custom loader.

`generator` set to `class`.
##### | `generator` set to `class`

the types created by `class` generator like below

```typescript
interface IModel {
Station: Station;
}
```

`generator` set to `function`. ( Support since `1.16.0` )
suitable for module like this

```typescript
export default class XXXController extends Controller { }
```

##### | `generator` set to `function` ( Support since `1.16.0` )

the types created by `function` generator like below

```typescript
interface IModel {
Station: ReturnType<typeof Station>;
}
```

`generator` set to `object`. ( Support since `1.16.0` )
suitable for module like this

```typescript
export default () => {
return {};
}
```

##### | `generator` set to `object` ( Support since `1.16.0` )

the types created by `object` generator like below.

```typescript
interface IModel {
Station: typeof Station;
}
```

suitable for module like this

```typescript
export default {}
```

##### | `generator` set to `auto` ( Support since `1.19.0` )

the types created by `auto` generator like below. It will check types automatically.

```typescript
type AutoInstanceType<T, U = T extends (...args: any[]) => any ? ReturnType<T> : T> = U extends { new (...args: any[]): any } ? InstanceType<U> : U;

interface IModel {
Station: AutoInstanceType<typeof Station>;
}
```

suitable for every module in above.

#### interfaceHandle `function|string`

```js
Expand Down Expand Up @@ -374,6 +414,8 @@ function myGenerator(config, baseConfig) {
console.info(config);
console.info(baseConfig);

// return type can be object or array { dist: string; content: string } | Array<{ dist: string; content: string }>
// egg-ts-helper will remove dist file when content is undefined.
return {
dist: 'd.ts file url',
content: 'd.ts content'
Expand All @@ -391,6 +433,45 @@ module.exports = {
}
```

or define generator to other js.

```javascript
// ./my-generator.js

// custom generator
module.exports = (config, baseConfig) => {
// config.dir dir
// config.dtsDir d.ts dir
// config.file changed file
// config.fileList file list
console.info(config);
console.info(baseConfig);

// return type can be object or array { dist: string; content: string } | Array<{ dist: string; content: string }>
// egg-ts-helper will remove dist file when content is undefined.
return {
dist: 'd.ts file url',
content: 'd.ts content'
}
}
```

configure in `tshelper.js` or `package.json`

```js
// ./tshelper.js

module.exports = {
watchDirs: {
model: {
path: 'app/model',
generator: './my-generator',
trigger: ['add', 'unlink'],
}
}
}
```

## Register

`egg-ts-helper` offers a `register.js` for easier to use with [egg-bin](https://github.com/eggjs/egg-bin).
Expand Down
90 changes: 85 additions & 5 deletions README.zh-CN.md
Expand Up @@ -253,32 +253,72 @@ interface T100 {

#### generator `string`

生成器名称,watcher 监听到文件改动的时候会执行该生成器用来重新生成 d.ts,建议只使用 `class` `function` `object` 这三个生成器,因为其他几个比较定制化,不太适用于 custom loader。
生成器名称,watcher 监听到文件改动的时候会执行该生成器用来重新生成 d.ts,建议只使用 `class` `function` `object` `auto` 这几个生成器,因为其他几个比较定制化,不太适用于 custom loader。

`generator` 设置为 `class`.
##### | `generator` 设置为 `class`.

生成的声明如下

```typescript
interface IModel {
Station: Station;
}
```

`generator` 设置为 `function`. ( `1.16.0` 开始支持 )
适合这样写的模块

```typescript
export default class XXXController extends Controller { }
```

##### | `generator` 设置为 `function`. ( `1.16.0` 开始支持 )

生成的声明如下

```typescript
interface IModel {
Station: ReturnType<typeof Station>;
}
```

`generator` 设置为 `object`. ( `1.16.0` 开始支持 )
适合这样写的模块

```typescript
export default () => {
return {};
}
```

##### | `generator` 设置为 `object`. ( `1.16.0` 开始支持 )

生成的声明如下

```typescript
interface IModel {
Station: typeof Station;
}
```

适合这样写的模块

```typescript
export default {}
```

##### | `generator` 设置为 `auto`. ( `1.19.0` 开始支持 )

生成的声明如下,会自动判断 import 的类型是方法还是对象还是类。

```typescript
type AutoInstanceType<T, U = T extends (...args: any[]) => any ? ReturnType<T> : T> = U extends { new (...args: any[]): any } ? InstanceType<U> : U;

interface IModel {
Station: AutoInstanceType<typeof Station>;
}
```

适合上面描述的所有模块。

#### interfaceHandle `function|string`

如果需要自定义类型,就可以用该配置
Expand Down Expand Up @@ -375,12 +415,13 @@ function myGenerator(config, baseConfig) {
console.info(config);
console.info(baseConfig);

// 返回值可以是对象或者数组 { dist: string; content: string } | Array<{ dist: string; content: string }>
// 如果返回的 content 是 undefined,egg-ts-helper 会删除 dist 指向的文件
return {
dist: 'd.ts file url',
content: 'd.ts content'
}
}

module.exports = {
watchDirs: {
model: {
Expand All @@ -392,6 +433,45 @@ module.exports = {
}
```

或者将自定义生成器定义到其他 js 中

```javascript
// ./my-generator.js

// custom generator
module.exports = (config, baseConfig) => {
// config.dir dir
// config.dtsDir d.ts dir
// config.file changed file
// config.fileList file list
console.info(config);
console.info(baseConfig);

// 返回值可以是对象或者数组 { dist: string; content: string } | Array<{ dist: string; content: string }>
// 如果返回的 content 是 undefined,egg-ts-helper 会删除 dist 指向的文件
return {
dist: 'd.ts file url',
content: 'd.ts content'
}
}
```

配置一下

```js
// ./tshelper.js

module.exports = {
watchDirs: {
model: {
path: 'app/model',
generator: './my-generator',
trigger: ['add', 'unlink'],
}
}
}
```

## 注册器

`egg-ts-helper` 提供了 `register.js` 来更方便的搭配 [egg-bin](https://github.com/eggjs/egg-bin) 使用.
Expand Down
20 changes: 20 additions & 0 deletions src/generators/auto.ts
@@ -0,0 +1,20 @@
import { TsGenConfig, TsHelperConfig } from '..';
import classGen from './class';
import * as utils from '../utils';

export default function(config: TsGenConfig, baseConfig: TsHelperConfig) {
config.interfaceHandle = utils.preWrapHandle(
val => `AutoInstanceType<typeof ${val}>`,
utils.strToFn(config.interfaceHandle),
);

const result = classGen(config, baseConfig);
if (result.content) {
result.content = [
'type AutoInstanceType<T, U = T extends (...args: any[]) => any ? ReturnType<T> : T> = U extends { new (...args: any[]): any } ? InstanceType<U> : U;',
result.content,
].join('\n');
}

return result;
}

0 comments on commit 21f781a

Please sign in to comment.