#How to set up a Sails Generator
cd into the directory where you keep this kind of thing, and do:
sails generate generator GENERATOR-NAME
cd GENERATOR-NAME
npm linkThen, check and see if a ~/.sailsrc file exists in your home directory. If it doesn't, create one. Otherwise, just edit it. It should look like this:
{
"generators": {
"modules": {
"GENERATOR-NAME": "sails-generate-GENERATOR-NAME"
}
}
}Now, create a clean directory wherever you plan on keeping your project. (You don't have to do this in a clean directory, it just reduces the chances of anything getting messed up.) In that directory, create a package.json file with the following:
{
"name": "whatever-doesnt-matter",
"version": "0.0.0",
"description":"",
"main": "index.js",
"scripts": {
test: "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "MIT"
}After creating that, run:
npm link sails-generate-GENERATOR-NAMEThis creates a node_modules folder with a shortcut to sails-generate-GENERATOR-NAME inside.
Now to generate a project, you do:
sails generate GENERATOR-NAME...but that won't be very useful yet. First you'll want to do some setup.
##Generator.js
Open up the project for your generator and go to Generator.js. This is where you can configure your generator to do cool things.
In the before function, you can configure sccope variables. scope.args contains the raw command line arguments.
e.g. if you were to type
sails generate GENERATOR-NAME my arguments herescope.args would be: ['my','arguments','here'].
You can also set other command-line flags to be available on the scope. So you could do:
sails generate GENERATOR-NAME --appname=myCoolAppand scope.appname would be myCoolApp.
In my generator's before function, I did:
scope.appname = scope.args[0] || 'My New App';That way, you can just set the appname by doing sails generate GENERATOR-NAME nameOfMyApp.
###targets
The targets object is where you set up the files and folders you want your generator to create by default. The key to each object in targets is the path to the file/folder in the generated project. (For these examples, I'll use ./path/to_a/thing.) The value of ./path/to_a/thing is another object, which can be have one of three different keys:
####folder helper
To create an empty folder at a static path, add to targets:
./path/to_a/thing: {
folder: {}
}####template helper
When you want to include a file in your generated project that has a pre-made template, but also uses things from the scope, you use the template helper:
./path/to_a/thing.js: {
template: {
templatePath: 'folderWithMyTemplate/thing.html'
}
}You can then go to folderWithMyTemplate in your generator project and add thing.html, which might look like this:
<h1>This is my app, <%= appname %>.</h1>
Then, when you create a new project using the generator, appname from the scope will replace <%= appname %> in the template.
####copy helper
If you just want to completely copy a template without replacing anything with scope variables, you can use the copy helper in place of template, and the file in your final project will look exactly like the file in your generator.
./path/to_a/thing.js: {
copy: {
templatePath: 'folderWithMyTemplate/thing.js'
}
}