{"name":"egg-ts-helper","version":"3.2.0","description":"egg typescript helper","bin":{"ets":"dist/bin.js"},"main":"dist/index.js","types":"dist/index.d.ts","repository":{"type":"git","url":"git+ssh://git@github.com/eggjs/egg-ts-helper.git"},"scripts":{"clean":"tsc -b --clean","tsc":"tsc -d","tsc:w":"tsc -d -w","lint":"eslint . --ext .ts","check":"npm run tsc && npm run lint","test":"npm run check && npm run test-local","test-local":"egg-bin test","prepublishOnly":"del dist && npm run tsc","cov":"egg-bin cov","ci":"npm run check && npm run cov && npm run tsc"},"keywords":["egg","typescript"],"author":{"name":"wanghx"},"license":"MIT","dependencies":{"@eggjs/utils":"^4.4.1","chalk":"^4.0.0","chokidar":"^3.0.0","commander":"^2.15.1","dot-prop":"^4.2.0","enquirer":"^2.3.0","globby":"^11.0.0","json5":"^2.2.3","ts-node":"^10.9.1","tslib":"^2.6.1","typescript":"^5.1.6","yn":"^3.0.0"},"devDependencies":{"@eggjs/bin":"^7.0.4","@eggjs/mock":"^6.0.5","@eggjs/tsconfig":"^1.0.0","@tsconfig/node14":"^14.1.2","@types/commander":"^2.12.2","@types/del":"^3.0.0","@types/globby":"^6.1.0","@types/mocha":"^10.0.0","@types/node":"^20.4.5","del":"^3.0.0","del-cli":"^1.1.0","egg":"3","egg-sequelize":"^4.3.1","eslint":"^8.28.0","eslint-config-egg":"14","extend2":"^1.0.0"},"engines":{"node":">= 18.19.0"},"gitHead":"101df076445565ac2606dcbbebbf9f469bccbe2d","_id":"egg-ts-helper@3.2.0","bugs":{"url":"https://github.com/eggjs/egg-ts-helper/issues"},"homepage":"https://github.com/eggjs/egg-ts-helper#readme","_nodeVersion":"22.20.0","_npmVersion":"11.6.1","dist":{"integrity":"sha512-rsWyzZfv7AaQEAjv5zJIra4V1K/3/6NTvbwUEzyZ9zPTxJCYML9vnJgFO9nj+BMVO6i+dziX03zbvqQQnXyMfQ==","shasum":"13875d47bdc8883b5b234556a2c2201c223713a5","tarball":"http://registry-npm.ywwl.com/egg-ts-helper/download/egg-ts-helper-3.2.0.tgz","fileCount":72,"unpackedSize":309760,"attestations":{"url":"https://registry.npmjs.org/-/npm/v1/attestations/egg-ts-helper@3.2.0","provenance":{"predicateType":"https://slsa.dev/provenance/v1"}},"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEUCIQD+Ku+mER4wzIXX2IyclOCz0iNKTwJo6RQUk/Qc3KY7nAIgDi0Asj+4USPtn1yUanW2fYVVv47ircdeOnWzMa2QvIk="}],"size":74657,"noattachment":false,"key":"/egg-ts-helper/-/egg-ts-helper-3.2.0.tgz"},"_npmUser":{"name":"GitHub Actions","email":"npm-oidc-no-reply@github.com","trustedPublisher":{"id":"github","oidcConfigId":"oidc:9d5d1fc5-5a45-4384-a4be-ea44cc2afaae"}},"directories":{},"maintainers":[{"name":"wanghx","email":"whxaxes@gmail.com"},{"name":"fengmk2","email":"fengmk2@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/egg-ts-helper_3.2.0_1759691851566_0.07150565608710213"},"_hasShrinkwrap":false,"readme":"# egg-ts-helper\n\n[![NPM version][npm-image]][npm-url]\n[![Node.js CI](https://github.com/eggjs/egg-ts-helper/actions/workflows/nodejs.yml/badge.svg)](https://github.com/eggjs/egg-ts-helper/actions/workflows/nodejs.yml)\n[![Package Quality](http://npm.packagequality.com/shield/egg-ts-helper.svg)](http://packagequality.com/#?package=egg-ts-helper)\n[![Test coverage][codecov-image]][codecov-url]\n[![NPM download][download-image]][download-url]\n\n[npm-image]: https://img.shields.io/npm/v/egg-ts-helper.svg?style=flat-square\n[npm-url]: https://npmjs.org/package/egg-ts-helper\n[codecov-image]: https://codecov.io/gh/whxaxes/egg-ts-helper/branch/master/graph/badge.svg\n[codecov-url]: https://codecov.io/gh/whxaxes/egg-ts-helper\n[download-image]: https://img.shields.io/npm/dm/egg-ts-helper.svg?style=flat-square\n[download-url]: https://npmjs.org/package/egg-ts-helper\n\n一个帮助 `egg` 生成 `d.ts` 的工具，通过 ts 提供的 [Declaration Merging](https://www.typescriptlang.org/docs/handbook/declaration-merging.html) 能力来将 `controller, proxy, service 等等` 这些 egg 中动态加载的模块注入到 egg 的声明中。从而获得类型校验以及代码提示。\n\n## 安装\n\n打开应用目录并且安装\n\n```bash\nnpm i egg-ts-helper --save-dev\n```\n\n## 快速开始\n\n打开你的 egg 应用，通过 [npx](https://github.com/zkat/npx) 来执行 ets 指令\n\n```bash\nnpx ets\n```\n\n可以通过 `-w` 来监听文件改动并且重新生成 `d.ts`\n\n```bash\nnpx ets -w\n```\n\n`egg-ts-helper` 已经内置在 `egg-bin` 中，可以通过以下命令方便使用\n\n```bash\negg-bin dev --dts\n```\n\n再或者在 `package.json` 中配置 `egg.declarations` 为 true 即可。\n\n## 命令行\n\n```bash\n$ ets -h\n\n  Usage: ets [commands] [options]\n\n  Options:\n\n    -v, --version           版本号\n    -w, --watch             是否监听文件改动\n    -c, --cwd [path]        Egg 的项目目录，(默认值: process.cwd)\n    -C, --config [path]     配置文件，入参应该是合法的 JSON/JS 文件 (默认值: {cwd}/tshelper)\n    -o, --oneForAll [path]  创建一个 import 了所有生成的 d.ts 的声明 (默认值: typings/ets.d.ts)\n    -s, --silent            静默执行，不输出日志\n    -i, --ignore [dirs]     忽略 generator 中的相关配置，可以通过逗号配置忽略多个，比如: -i controller,service\n    -e, --enabled [dirs]    开启 generator 中的相关配置， 可以通过逗号配置忽略多个，比如: -e proxy,other\n    -E, --extra [json]      额外配置，可以是 json 字符串\n    -h, --help              帮助\n\n  Commands:\n\n    clean                   清除所有包含同名 ts/tsx 文件的 js 文件\n    init <type>             在你的项目中初始化 egg-ts-helper\n```\n\n## 配置\n\n| name | type | default | description |\n| --- | --- | --- | --- |\n| cwd | `string` | process.cwd | Egg 的项目目录 |\n| typings | `string` | {cwd}/typings | 生成的声明放置目录 |\n| caseStyle | `string` `Function` | lower | egg 的模块命名方式 (lower (首字母小写), upper (首字母大写), camel (驼峰) ) ，也可以传方法 `(filename) => {return 'YOUR_CASE'}`|\n| silent | `boolean` | false | 静默执行，不输出日志 |\n| watch | `boolean` | false | 是否监听文件改动，使用 `register` 的话该值默认为 true |\n| watchOptions | `object` | undefined | chokidar 的[配置](https://github.com/paulmillr/chokidar#api) |\n| configFile | `string` | {cwd}/tshelper.(js|json) | 配置文件路径 |\n| generatorConfig | `object` | | 生成器配置 |\n\n可以在  `./tshelper.js`  `./tshelper.json` 或者 `package.json` 中配置上面的配置\n\n在 `tshelper.js`\n\n```js\n// {cwd}/tshelper.js\n\nmodule.exports = {\n  generatorConfig: {\n    model: {\n      enabled: true,\n      generator: \"function\",\n      interfaceHandle: \"InstanceType<{{ 0 }}>\"\n    },\n  }\n}\n```\n\n在 `tshelper.json`\n\n```json\n// {cwd}/tshelper.json\n\n{\n  \"generatorConfig\": {\n    \"model\": {\n      \"enabled\": true,\n      \"generator\": \"function\",\n      \"interfaceHandle\": \"InstanceType<{{ 0 }}>\"\n    },\n  }\n}\n```\n\n在 `package.json`\n\n```json\n// {cwd}/package.json\n\n{\n  \"egg\": {\n    \"framework\": \"egg\",\n    \"tsHelper\": {\n      \"generatorConfig\": {\n        \"model\": {\n          \"enabled\": true,\n          \"generator\": \"function\",\n          \"interfaceHandle\": \"InstanceType<{{ 0 }}>\"\n        },\n      }\n    }\n  }\n}\n```\n\n同时也可以通过环境变量来传入配置 ( 1.22.0 版本后开始支持 )\n\n- `ETS_CWD`: cwd\n- `ETS_FRAMEWORK`: framework\n- `ETS_TYPINGS`: typings\n- `ETS_CASE_STYLE`: caseStyle\n- `ETS_AUTO_REMOVE_JS`: autoRemoveJs\n- `ETS_THROTTLE`: throttle\n- `ETS_WATCH`: watch\n- `ETS_SILENT`: silent\n- `ETS_CONFIG_FILE`: configFile\n\n## Custom Loader\n\n> 在 1.24.0 之后版本支持\n\n`egg-ts-helper` 支持 egg 的 customLoader 配置，会自动去读取应用/插件/框架中的 customLoader 配置. 详情请看 <https://github.com/eggjs/egg/issues/3480>\n\n在 `config.default.ts` 中配置 customLoader\n\n```typescript\n'use strict';\n\nimport { EggAppConfig, PowerPartial } from 'egg';\n\nexport default function(appInfo: EggAppConfig) {\n  const config = {} as PowerPartial<EggAppConfig>;\n\n  config.keys = appInfo.name + '123123';\n\n  config.customLoader = {\n    model: {\n      directory: 'app/model',\n      inject: 'app',\n      caseStyle: 'upper',\n    },\n  };\n\n  return {\n    ...config as {},\n    ...bizConfig,\n  };\n}\n```\n\n`egg-ts-helper` 将会根据 `app/model` 目录下的文件，自动生成声明 （ 参考 <https://github.com/whxaxes/egg-boilerplate-d-ts> 这个项目 ）\n\n```typescript\n// This file is created by egg-ts-helper@1.24.1\n// Do not modify this file!!!!!!!!!\n\nimport 'egg';\ntype AutoInstanceType<T, U = T extends (...args: any[]) => any ? ReturnType<T> : T> = U extends { new (...args: any[]): any } ? InstanceType<U> : U;\nimport ExportCastle from '../../../app/model/Castle';\nimport ExportUser from '../../../app/model/User';\n\ndeclare module 'egg' {\n  interface Application {\n    model: T_custom_model;\n  }\n\n  interface T_custom_model {\n    Castle: AutoInstanceType<typeof ExportCastle>;\n    User: AutoInstanceType<typeof ExportUser>;\n  }\n}\n```\n\n然后你就可以愉快的在代码中使用了。\n\n![image](https://user-images.githubusercontent.com/5856440/54109111-b4848b80-4418-11e9-9da5-77b342f7f814.png)\n\n## Generator\n\n如果需要支持老的 customLoader 方式（ 即指在 app.ts 等中调用 `loader.loadToApp` 或者 `loader.loadToContext` 的方式实现的 customLoader ），则需要使用 `egg-ts-helper` 生成器配置。\n\n### 示例\n\n比如需要给 `app/model` 下的文件创建 `d.ts`，则需要在你的配置文件中配置一下 `generatorConfig.model`。\n\n```typescript\n// ./tshelper.js\n\nmodule.exports = {\n  generatorConfig: {\n    model: {\n      directory: 'app/model', // 监听目录\n      // pattern: '**/*.(ts|js)', // 遍历的文件表达式，一般都不需要改这个\n      // ignore: '', // 指定忽略某些文件的表达式，默认为空\n      generator: 'class', // 生成器名称，取值为 class、auto、function、object\n      interface: 'IModel',  // interface 名称，如果不填的话，将会随机生成个 interface\n      declareTo: 'Context.model', // 指定定义到 egg 的某个类型下\n      // watch: true, // 是否需要监听文件改动\n      // caseStyle: 'upper', // 模块命名格式\n      // interfaceHandle: val => `ReturnType<typeof ${val}>`, // interface 预处理方法\n      // trigger: ['add', 'unlink'], // 当接收到这些文件更改事件的时候，会触发 d.ts 的重新生成, 所有事件: ['add', 'unlink', 'change']\n    }\n  }\n}\n```\n\n使用上面的配置，会生成下面这个 `d.ts`\n\n> 注意，上面的 generator 是 class ，生成的声明即会对 import 进来的类型直接挂载，不会做任何处理，如果不了解这个，请配置 `auto` 类型。\n\n```typescript\nimport Station from '../../../app/model/station'; // <-- 遍历 app/model 目录并且 import\n\ndeclare module 'egg' {\n  interface Context { // <-- 这个 Context 是读配置中的 declareTo\n    model: IModel; // <-- 这个 IModel 是读配置中的 interface ，如果不传，会随机生成个 interface\n  }\n\n  interface IModel { // <-- 这个 IModel 同上\n    Station: Station; // <-- 将 app/model 中的文件 import 挂载到 IModel 上，从而将类型合并到 ctx.model 中\n  }\n}\n```\n\n### 不同配置的效果\n\n#### interface `string`\n\n`interface` 设置为 `IOther`.\n\n```typescript\ninterface IOther {\n  Station: Station;\n}\n```\n\n如果不设置 `interface` 将会使用随机的名称\n\n```typescript\ninterface T100 {\n  Station: Station;\n}\n```\n\n这种情况下请一定要配置 `declareTo`，不然就没意义了。\n\n#### generator `string`\n\n生成器名称，watcher 监听到文件改动的时候会执行该生成器用来重新生成 d.ts，可以使用的生成器名称为 `class` `function` `object` `auto` 。下面列举一下不同生成器生成的声明有什么不同。\n\n**`generator: 'class'`**\n\n生成的声明如下\n\n```typescript\ninterface IModel {\n  Station: Station; // 不做任何处理，直接挂载\n}\n```\n\n适合这样写的模块\n\n```typescript\nexport default class XXXController extends Controller { }\n```\n\n**`generator: 'function'`** ( `1.16.0` 开始支持 )\n\n生成的声明如下\n\n```typescript\ninterface IModel {\n  Station: ReturnType<typeof Station>; // 使用 ReturnType 获得方法的返回类型\n}\n```\n\n适合这样写的模块\n\n```typescript\nexport default () => {\n  return {};\n}\n```\n\n**`generator: 'object'`** ( `1.16.0` 开始支持 )\n\n生成的声明如下\n\n```typescript\ninterface IModel {\n  Station: typeof Station; // 使用 typeof 获得对象的原始类型。\n}\n```\n\n适合这样写的模块\n\n```typescript\nexport default {}\n```\n\n**`generator: 'auto'`** ( `1.19.0` 开始支持 )\n\n生成的声明如下，自动判断 import 的类型是方法还是对象还是类，即用了这个，你就不用管 export 的是方法还是对象还是类了，对类型了解不清楚的可以直接用这个。\n\n```typescript\ntype AutoInstanceType<T, U = T extends (...args: any[]) => any ? ReturnType<T> : T> = U extends { new (...args: any[]): any } ? InstanceType<U> : U;\n\ninterface IModel {\n  Station: AutoInstanceType<typeof Station>;\n}\n```\n\n适合上面描述的所有模块。\n\n#### interfaceHandle `function|string`\n\n如果在 generator 中找不到合适的生成器类型，可以通过该配置对类型进行预处理。\n\n```js\nmodule.exports = {\n  generatorConfig: {\n    model: {\n      ...\n\n      interfaceHandle: val => `${val} & { [key: string]: any }`,\n    }\n  }\n}\n```\n\n生成的声明如下\n\n```typescript\ninterface IModel {\n  Station: Station & { [key: string]: any };\n}\n```\n\n这个配置也可以是字符串 ( `1.18.0` 开始支持 )\n\n```js\nmodule.exports = {\n  generatorConfig: {\n    model: {\n      ...\n\n      interfaceHandle: '{{ 0 }} & { [key: string]: any }',\n    }\n  }\n}\n```\n\n生成的声明跟前面方法那个一样，`{{ 0 }}` 代表方法的第一个入参。\n\n#### caseStyle `function|string`\n\n`caseStyle` 可以设置为 `lower`、`upper`、`camel` 或者是方法。\n\n#### declareTo `string`\n\n可以将生成的声明定义到 egg 的类型中，比如 egg 的 ctx 的类型是 `Context` ，就可以配置到 `Context` 下，然后通过 ctx.xxx 就可以拿到提示了。 ( `1.15.0` 开始支持 )\n\n`declareTo` 设置为 `Context.model`，然后就可以通过 `ctx.model.xxx` 拿到代码提示了\n\n```typescript\nimport Station from '../../../app/model/station';\n\ndeclare module 'egg' {\n  interface Context {\n    model: IModel;\n  }\n\n  interface IModel {\n    Station: Station;\n  }\n}\n```\n\n`declareTo` 设置为 `Application.model.subModel`，然后就可以通过 `app.model.subModel.xxx` 拿到代码提示了\n\n```typescript\nimport Station from '../../../app/model/station';\n\ndeclare module 'egg' {\n  interface Application {\n    model: {\n      subModel: IModel;\n    }\n  }\n\n  interface IModel {\n    Station: Station;\n  }\n}\n```\n\n### 自定义生成器\n\n```javascript\n// ./tshelper.js\n\n// 自定义 generator\nfunction myGenerator(config, baseConfig) {\n  // config.dir       dir\n  // config.dtsDir    d.ts 目录\n  // config.file      发生更改的文件 file\n  // config.fileList  path 下的文件列表\n  console.info(config);\n  console.info(baseConfig);\n\n  // 返回值可以是对象或者数组 { dist: string; content: string } | Array<{ dist: string; content: string }>\n  // 如果返回的 content 是 undefined，egg-ts-helper 会删除 dist 指向的文件\n  return {\n    dist: 'd.ts file url',\n    content: 'd.ts content'\n  }\n}\nmodule.exports = {\n  generatorConfig: {\n    model: {\n      directory: 'app/model',\n      generator: myGenerator,\n      trigger: ['add', 'unlink'],\n    }\n  }\n}\n```\n\n或者将自定义生成器定义到其他 js 中\n\n```javascript\n// ./my-generator.js\n\nmodule.exports.defaultConfig = {\n  // 默认的 watchDir config\n}\n\n// 自定义 generator\nmodule.exports = (config, baseConfig) => {\n  // config.dir       dir\n  // config.dtsDir    d.ts 目录\n  // config.file      发生更改的文件 file\n  // config.fileList  path 下的文件列表\n  console.info(config);\n  console.info(baseConfig);\n\n  // 返回值可以是对象或者数组 { dist: string; content: string } | Array<{ dist: string; content: string }>\n  // 如果返回的 content 是 undefined，egg-ts-helper 会删除 dist 指向的文件\n  return {\n    dist: 'd.ts file url',\n    content: 'd.ts content'\n  }\n}\n```\n\n配置一下\n\n```js\n// ./tshelper.js\n\nmodule.exports = {\n  generatorConfig: {\n    model: {\n      directory: 'app/model',\n      generator: './my-generator',\n      trigger: ['add', 'unlink'],\n    }\n  }\n}\n```\n\n## 示例项目\n\n`egg-ts-helper` 可用于 egg 的 `ts` 和 `js` 项目.\n\nTS 项目: <https://github.com/whxaxes/egg-boilerplate-d-ts>\n\nJS 项目: <https://github.com/whxaxes/egg-boilerplate-d-js>\n\n## License\n\n[MIT](LICENSE)\n\n## Contributors\n\n[![Contributors](https://contrib.rocks/image?repo=eggjs/tracer)](https://github.com/eggjs/tracer/graphs/contributors)\n\nMade with [contributors-img](https://contrib.rocks).\n","publish_time":1759691851748,"_cnpm_publish_time":1759691851748,"dist-tags":{"latest":"3.2.0","latest-1":"1.35.1","release-1.x":"1.35.2"}}