Skip to content

Commit

Permalink
feat: config improvement (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
whxaxes committed Mar 25, 2019
1 parent 891f7ef commit 958b117
Show file tree
Hide file tree
Showing 21 changed files with 345 additions and 151 deletions.
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,26 @@ In `package.json`
"enabled": true,
"generator": "function",
"interfaceHandle": "InstanceType<{{ 0 }}>"
},
}
}
}
}
}
```

or use `dot-prop`

```json
// {cwd}/package.json

{
"egg": {
"framework": "egg",
"tsHelper": {
"watchDirs.model": {
"enabled": true,
"generator": "function",
"interfaceHandle": "InstanceType<{{ 0 }}>"
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@
"license": "MIT",
"dependencies": {
"cache-require-paths": "^0.3.0",
"chalk": "^2.4.2",
"chokidar": "^2.0.1",
"commander": "^2.15.1",
"debug": "^3.1.0",
"dot-prop": "^4.2.0",
"enquirer": "^2.3.0",
"globby": "^8.0.1",
"mkdirp": "^0.5.1",
"ts-node": "^7.0.0",
"ts-node": "^8.0.0",
"tslib": "^1.9.3",
"typescript": "^3.0.0",
"yn": "^3.0.0"
Expand Down
5 changes: 4 additions & 1 deletion src/generators/auto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ export default function(config: TsGenConfig, baseConfig: TsHelperConfig) {
/* istanbul ignore else */
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;',
'type AnyClass = new (...args: any[]) => any;',
'type AnyFunc<T = any> = (...args: any[]) => T;',
'type CanExportFunc = AnyFunc<Promise<any>> | AnyFunc<IterableIterator<any>>;',
'type AutoInstanceType<T, U = T extends CanExportFunc ? T : T extends AnyFunc ? ReturnType<T> : T> = U extends AnyClass ? InstanceType<U> : U;',
result.content,
].join('\n');
}
Expand Down
40 changes: 20 additions & 20 deletions src/generators/custom.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// generator for custom loader
import { default as TsHelper, TsGenConfig, TsHelperConfig } from '..';
import { declMapping } from '../config';
import Watcher from '../watcher';
import * as utils from '../utils';
import path from 'path';

Expand All @@ -16,7 +15,8 @@ export const defaultConfig = {
],
};

const customWatcherPrefix = 'custom-';
const customWatcherName = 'custom';
const customSpecRef = `${customWatcherName}_spec_ref`;
const DeclareMapping = utils.pickFields<keyof typeof declMapping>(declMapping, [ 'ctx', 'app' ]);

export default function(config: TsGenConfig, baseConfig: TsHelperConfig, tsHelper: TsHelper) {
Expand All @@ -27,23 +27,26 @@ export default function(config: TsGenConfig, baseConfig: TsHelperConfig, tsHelpe
if (eggConfig.customLoader) {
Object.keys(eggConfig.customLoader).forEach(key => {
const loaderConfig = eggConfig.customLoader[key];
if (
!loaderConfig ||
!loaderConfig.directory ||
!DeclareMapping[loaderConfig.inject] ||
loaderConfig.tsd === false
) return;
if (!loaderConfig || !loaderConfig.directory) {
return;
}

loaderConfig.inject = loaderConfig.inject || 'app';
if (!DeclareMapping[loaderConfig.inject] || loaderConfig.tsd === false) return;

// custom d.ts name
const name = `${customWatcherPrefix}${key}`;
const name = `${customWatcherName}-${key}`;
newCustomWatcherList.push(name);

// create a custom watcher
tsHelper.registerWatcher(name, {
ref: customSpecRef,
distName: `${name}.d.ts`,
directory: loaderConfig.directory,
pattern: loaderConfig.match,
ignore: loaderConfig.ignore,
caseStyle: loaderConfig.caseStyle || 'lower',
interface: declMapping[key],
interface: loaderConfig.interface || declMapping[key],
declareTo: `${DeclareMapping[loaderConfig.inject]}.${key}`,
generator: 'auto',
execAtInit: true,
Expand All @@ -52,18 +55,15 @@ export default function(config: TsGenConfig, baseConfig: TsHelperConfig, tsHelpe
}

// collect watcher which is need to remove.
const removeList: Watcher[] = [];
tsHelper.watcherList.forEach(w => {
if (w.name.startsWith(customWatcherPrefix) && !newCustomWatcherList.includes(w.name)) {
removeList.push(w);
}
});
const removeList = tsHelper.watcherList.filter(w => (
w.ref === customSpecRef && !newCustomWatcherList.includes(w.name)
));

// remove watcher and old d.ts
return removeList.map(w => {
tsHelper.destroyWatcher(w.name);
return { dist: path.resolve(w.dtsDir, `${w.name}.d.ts`) };
});
tsHelper.destroyWatcher.apply(tsHelper, removeList.map(w => w.name));
return removeList.map(w => ({
dist: path.resolve(w.dtsDir, `${w.name}.d.ts`),
}));
};

return utils.getEggInfo(baseConfig.cwd, {
Expand Down

0 comments on commit 958b117

Please sign in to comment.