Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More โ†’
Socket
Sign inDemoInstall
Socket

egg-template-helper

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

egg-template-helper

egg template helper

  • 1.0.3
  • latest
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

egg-template-helper

Setup an egg project

install egg-init globally

  npm i egg-init -g

setup a TypeScript based project

npx egg-init --type=ts projectName
cd projectName && npm i
npm run dev

setup a JavaScript based project

egg-init --type simple projectName
cd projectName && npm i
npm run dev

For further details, please refer to https://github.com/eggjs/egg-init

Install

  npm install egg-template-helper -g

or

  yarn global add egg-template-helper

Usage

$ eth -h

  Usage: eth [commands] [options]

  Options:
    -v, --version            output the version number
    -h, --help               output usage information

  Commands:

    new [name] [options]     ๆ–ฐๅปบeggๆ–‡ไปถ
    delete [name] [options]  ๅˆ ้™คeggๆ–‡ไปถ
    rename [name] [options]  ้‡ๅ‘ฝๅeggๆ–‡ไปถ

eth new

create files by the [name] and a specified type.

New Options
nametypedescription
categorystringnew file category
testbooleanwhether append test for controller / service / model

Available category

  • default: will create controller, service and model.
  • controller: will create a controller.
  • service: will create a service.
  • model: will create a model, the model depends on mongoose.
create controller / service / model
  eth new user --category default

or

  eth new user -c default

Actually the [name] parameter is a path relative to app/controller / app/service / app/model

In this example, it will create app/controller/user.ts, app/model/user.ts, app/service/user.ts.

create a sub directory controller / service / model
  eth new permission/role --category default

or

  eth new permission/role -c default

It will create app/controller/permission/role.ts, app/model/permission/role.ts, app/service/permission/role.ts.

eth delete

delete files associated with the [name]

  eth delete user

It will delete app/controller/user.ts, app/model/user.ts, app/service/user.ts.

eth rename

Rename Options
nametypedescription
newPathstringnew path

rename files associated with the [name]

  eth rename user -n masterUser

or

  eth rename user -newPath masterUser

It will rename app/controller/user.ts, app/model/user.ts, app/service/user.ts to app/controller/masterUser.ts, app/model/masterUser.ts, app/service/masterUser.ts

It will also change the file content!!!

Once user renamed to masterUser, the app/controller/masterUser.ts content is changed.

import { Controller } from 'egg'

/**
 * MasterUser Controller
 *
 * @export
 * @class MasterUserController
 * @extends {Controller}
 */
export default class MasterUserController extends Controller {
}

Customizations

You can setup your own templates.

eth.config.json

create an eth.config.json file under the project root.

nametypedefaultdescription
extstring".ts"file extenision, you can specify ext to ".js" if you use JavaScript to develop egg project
templateDirstring""custom template path, relative to the project root
execEtsbooleantrueexecute egg-ts-helper after command executed. It will take effect when ext is ".ts"
renameContentbooleantruerewrite file content when executing rename command
customDataobject{}pass custom data to custom template when executing new command

Example

Let's customize a template and inject an author name by setting customData.

eth.config.json

{
  "ext": ".ts",
  "templateDir": "template",
  "execEts": true,
  "renameContent": false,
  "customData": {
    "Author": "Dada"
  }
}

directory structure

egg-showcase
  โ”œโ”€โ”€ node_modules
  โ””โ”€โ”€ app
      โ”œโ”€โ”€ controller
      โ”œโ”€โ”€ model
      โ”œโ”€โ”€ service
      โ””โ”€โ”€ router.ts
  โ”œโ”€โ”€ template                          template directory
  |   โ”œโ”€โ”€ controller.test.tpl           controller test template
  |   โ”œโ”€โ”€ controller.tpl                controller template
  |   โ”œโ”€โ”€ model.tpl                     model template
  |   โ”œโ”€โ”€ service.test.tpl              service testtemplate
  โ””โ”€โ”€ โ””โ”€โ”€ service.tpl                   service template

template/comtroller.tpl

import { Controller } from 'egg'

/**
 * <%=pascalCaseName%> Controller
 * 
 * @author <%=Author%>
 * @export
 * @class <%=pascalCaseName%>Controller
 * @extends {Controller}
 */
export default class <%=pascalCaseName%>Controller extends Controller {
}

There are four built in variables: name, pascalCaseName, testName, modelTestRootRelative

execute new command

  eth new user -c default

The content in app/controller/user.ts will like this:

import { Controller } from 'egg'

/**
 * User Controller
 *
 * @author Dada
 * @export
 * @class UserController
 * @extends {Controller}
 */
export default class UserController extends Controller {
}

Keywords

FAQs

Package last updated on 23 Apr 2018

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with โšก๏ธ by Socket Inc