Generate a code file (or multiple files) in a Sails app.
sails generate <generator>
Sails ships with several generators to help you scaffold new projects, spit out boilerplate code for common files, and automate your development process.
The following core generators are bundled with Sails:
Command | Details |
---|---|
sails generate page | Generate four pages: .ejs, .less, page script, and view action. You must add your .less file to the importer and you must set your route for your new page to work. Note: sails generate page is intended for use with projects generated with the "Web app" template. You can still use this command if you're not using the web app template, but you'll need to delete the assets/js/pages/page-name.page.js file that's been generated, as it relies on dependencies that don't come bundled with an "Empty" Sails app. |
sails generate model | Generate api/models/Foo.js, including attributes with the specified types if provided. For example, sails generate model User username isAdmin:boolean will generate a User model with a username string attribute and an isAdmin boolean attribute. |
sails generate action | Generate a standalone action. |
sails generate helper | Generate a helper at api/helpers/foo.js. |
sails generate controller | Generate api/controllers/FooController.js, including actions with the specified names if provided. |
sails generate hook | Generate a project hook in api/hooks/foo/. |
sails generate generator | Generate a foo folder containing the files necessary for building a new generator. |
sails generate response | Generate a custom response at api/responses/foo.js |
sails generate adapter | Generate a api/adapters/foo/ folder containing the files necessary for building a new adapter. |
sails generate sails.io.js | Generate a sails.io.js file at the specified location, overwriting the default sails.io.js if applicable. |
sails generate api | Generate api/models/Foo.js and api/controllers/FooController.js. |
sails generate new | Alias for sails new . |
sails generate etc | Experimental. Adds the following files to your app: • .gitignore • .jshintrc • .editorconfig • .npmignore • .travis.yml • .appveyor.yml |
Custom / third party generators allow you to extend or override the default functionality of sails generate
(for example, by creating a generator that outputs view files for your favorite view engine).
You can also use custom generators to automate frequent tasks or generate app-specific files. For example, if you are using React, you might wire up a quick custom generator to allow you to generate React components in the appropriate folder in your project (sails generate react component
).