Edit Page

Custom generators

Overview

Custom generators are a type of plugin for the Sails command line. Through templates, they control which files get generated in your Sails projects when you run sails new or sails generate, and also what those files look like.

Creating a generator

To make this easier to play with, let's first make a Sails project. If you haven't already created one, go to your terminal and type:

sails new my-project

Then cd into my-project and ask Sails to spit out the template for a new generator:

sails generate generator awesome

Configuring a generator

To enable the generator you need to tell Sails about it via your test project's .sailsrc file.

If we were using an existing generator, we could just install it from NPM, then specify the name of the package in .sailsrc. But since we're developing this generator locally, we'll just connect it to the folder directly:

{
  "generators": {
    "modules": {
        "awesome": "./my-project/awesome"
    }
  }
}

Note: For now, we'll stick with "awesome", but you can mount the generator under any name you want. Whatever you choose for the name of the key in the .sailsrc file will be the name you'll use to run this generator from the terminal (e.g. sails generate awesome).

Running a custom generator

To run your generator, just tack its name on to sails generate, followed by any desired arguments or command-line options. For example:

sails generate awesome

Publishing to NPM

If your generator is useful across different projects, you might consider publishing it as an NPM package (note that this doesn't mean that your generator must be open-source: NPM also supports private packages.

First, pop open the package.json file and verify the package name (e.g. "@my-npm-name/sails-generate-awesome"), author ("My Name"), license, and other information are correct. If you're unsure, a good open source license to use is "MIT". If you're publishing a private generator and want it to remain proprietary to your organization, use "UNLICENSED".

Note: If you don't already have an NPM account, go to npmjs.com and create one. Then use npm login to get set up.

When you're ready to pull the trigger and publish your generator on NPM, cd into the generator's folder in the terminal and type:

npm publish

Installing a generator

To take your newly-published generator for a spin, cd back into your example Sails project (my-project), delete the inline generator, and run:

npm install @my-npm-name/sails-generate-awesome

then change the .sailsrc in your example Sails project (my-project/.sailsrc):

{
  "generators": {
    "modules": {
      "awesome": "@my-npm-name/sails-generate-awesome"
    }
  }
}

And, last but not least:

sails generate awesome

Is something missing?

If you notice something we've missed or could be improved on, please follow this link and submit a pull request to the sails repo. Once we merge it, the changes will be reflected on the website the next time it is deployed.

Sails logo
  • Home
  • Get started
  • Support
  • Documentation
  • Documentation

For a better experience on sailsjs.com, update your browser.

Check out the full Sailsconf 2022 playlist on Youtube

Tweet Follow @sailsjs

Documentation

Reference Concepts App structure | Upgrading Contribution guide | Tutorials More

Concepts

  • Actions and controllers
    • Generating actions and controllers
    • Routing to actions
  • Assets
    • Default tasks
    • Disabling Grunt
    • Task automation
  • Blueprints
    • Blueprint actions
    • Blueprint routes
  • Configuration
    • The local.js file
    • Using `.sailsrc` files
  • Deployment
    • FAQ
    • Hosting
    • Scaling
  • E-commerce
  • Extending Sails
    • Adapters
      • Available adapters
      • Custom adapters
    • Custom responses
      • Adding a custom response
    • Generators
      • Available generators
      • Custom generators
    • Hooks
      • Available hooks
      • Events
      • Hook specification
        • .configure
        • .defaults
        • .initialize()
        • .registerActions()
        • .routes
      • Installable hooks
      • Project hooks
      • Using hooks
  • File uploads
    • Uploading to GridFS
    • Uploading to S3
  • Globals
    • Disabling globals
  • Helpers
    • Example helper
  • Internationalization
    • Locales
    • Translating dynamic content
  • Logging
    • Custom log messages
  • Middleware
    • Conventional defaults
  • Models and ORM
    • Associations
      • Many-to-many
      • One way association
      • One-to-many
      • One-to-one
      • Reflexive associations
      • Through associations
    • Attributes
    • Errors
    • Lifecycle callbacks
    • Model settings
    • Models
    • Query language
    • Records
    • Standalone Waterline usage
    • Validations
  • Policies
    • Access Control and Permissions
  • Programmatic usage
    • Tips and tricks
  • Realtime
    • Multi-server environments
    • On the client
    • On the server
  • Routes
    • Custom routes
    • URL slugs
  • Security
    • Clickjacking
    • Content security policy
    • CORS
    • CSRF
    • DDOS
    • P3P
    • Socket hijacking
    • Strict Transport Security
    • XSS
  • Services
  • Sessions
  • Shell scripts
  • Testing
  • Views
    • Layouts
    • Locals
    • Partials
    • View engines

Built with Love

The Sails framework is built by a web & mobile shop in Austin, TX, with the help of our contributors. We created Sails in 2012 to assist us on Node.js projects. Naturally we open-sourced it. We hope it makes your life a little bit easier!

Sails:
  • What is Sails?
  • Community
  • News
  • For business
About:
  • Our company
  • Security
  • Legal
  • Logos/artwork
Help:
  • Get started
  • Documentation
  • Docs
  • Contribute
  • Take a class

© 2012-2023 The Sails Company. 
The Sails framework is free and open-source under the MIT License. 
Illustrations by Edamame.