Product
Introducing License Enforcement in Socket
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
egg-ts-helper
Advanced tools
A simple tool for generates typescript definition files(d.ts) for egg application. Injecting controller
,proxy
,service
and extend
to egg by Declaration Merging
npm i egg-ts-helper -g
or
yarn global add egg-ts-helper
Open your egg application, executing the command
$ ets
-w
flag can auto recreated d.ts while file changed
$ ets -w
$ ets -h
Usage: ets [commands] [options]
Options:
-v, --version output the version number
-w, --watch Watching files, d.ts will recreate after file has changed
-c, --cwd [path] Egg application base dir (default: process.cwd)
-C, --config [path] Configuration file, The argument can be a file path to a valid JSON/JS configuration file.(default: {cwd}/tshelper.js
-f, --framework [name] Egg framework(default: egg)
-s, --silent Running without output
-i, --ignore [dirs] Ignore watchDirs, your can ignore multiple dirs with comma like: -i controller,service
-e, --enabled [dirs] Enable watchDirs, your can enable multiple dirs with comma like: -e proxy,other
-E, --extra [json] Extra config, the value should be json string
-h, --help output usage information
Commands:
clean Clean js file when it has the same name ts file
name | type | default | description |
---|---|---|---|
cwd | string | process.cwd | egg application base dir |
framework | string | egg | egg framework |
typings | string | {cwd}/typings | typings dir |
caseStyle | string | lower | egg case style(lower,upper,camel) |
watch | boolean | false | watch file change or not |
watchOptions | object | undefined | chokidar options |
execAtInit | boolean | false | execute d.ts generation while instance was created |
configFile | string | {cwd}/tshelper.js | configure file path |
watchDirs | object | generator configuration |
egg-ts-helper would watching app/extend
,app/controller
,app/service
, app/config
, app/middleware
, app/model
by default. The dts would recreated when the files under these folders was changed.
you can disabled some folders by -i
flag.
$ ets -i extend,controller
or configure in the config file
// {cwd}/tshelper.js
module.exports = {
watchDirs: {
extend: false,
controller: false,
}
}
or configure in package.json
// {cwd}/package.json
{
"egg": {
"framework": "egg",
"tsHelper": {
"watchDirs": {
"extend": false
}
}
}
}
egg-ts-helper
not only support the base loader( controller, middleware ... ), but also support to configure your own loader.
for example. If I want to auto create the d.ts for egg-mongodb
. configuring watchDirs in {cwd}/tshelper.js
and use class
generator
// ./tshelper.js
module.exports = {
watchDirs: {
model: {
path: 'app/model', // dir path
// pattern: '**/*.(ts|js)', // glob pattern, default is **/*.(ts|js). it doesn't need to configure normally.
generator: 'class', // generator name
framework: 'larva', // framework name
interface: 'IModel', // interface name
caseStyle: 'upper', // caseStyle for loader
interfaceHandle: val => `ReturnType<typeof ${val}>`, // interfaceHandle
trigger: ['add', 'unlink'], // recreate d.ts when receive these events, all events: ['add', 'unlink', 'change']
}
}
}
the configuration can create d.ts like below.
import Station from '../../../app/model/station';
declare module '{ framework }' {
interface { interface } {
Station: { interfaceHandle ? interfaceHandle('Station') : Station };
}
}
and don't forget to declare IModel to egg.
// typings/index.d.ts
import { PlainObject } from 'egg';
declare module 'egg' {
interface Application {
model: IModel
}
interface IModel extends PlainObject {
}
}
// ./tshelper.js
// custom generator
function myGenerator(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 {
dist: 'd.ts file url',
content: 'd.ts content'
}
}
module.exports = {
watchDirs: {
model: {
path: 'app/model',
generator: myGenerator,
trigger: ['add', 'unlink'],
}
}
}
You can require register to start egg-ts-helper before starting egg application with egg-bin.
$ egg-bin dev -r egg-ts-helper/register
debugging
$ egg-bin debug -r egg-ts-helper/register
see https://github.com/whxaxes/egg-ts-helper/tree/master/test/fixtures/real/typings
FAQs
egg typescript helper
The npm package egg-ts-helper receives a total of 15,911 weekly downloads. As such, egg-ts-helper popularity was classified as popular.
We found that egg-ts-helper demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.