A metalsmith based generator for scaffolding Feathers apps. Used by feathers-cli.
This module was experimental and is no longer under active development. Use the Feathers CLI at @feathersjs/cli to generate a new Feathers application and generator-feathers as a template to modify the generated application.
The available generators are:
- apps
- hooks
- services
- filters
- models
- middlewares
- plugins
Each generator has:
- a main
<type>.generator.js
file which has all the logic for the given generator. - a
templates/
directory that contains all the static and dynamic templates the generator uses. - a
meta.json
file.
Each main generator file must implement these methods:
loadAppConfigs
- This loads any existing Feathers application config files that reside infeathers-app/config/
so that they can be referenced and re-written with new values if necessary.loadPackageJSON
- This loads the existing Feathers applicationpackage.json
file so it can be referenced and re-written with new values if necessary.getQuestions
- This returns the questions from themeta.json
filegenerate
- This takes in theanswers
from the user and generates/copies the appropriate files based on their answers.
This file contains the meta data that each generator uses to tell the CLI which questions it needs the user to answer and when.
The CLI uses inquirer to ask the user questions and gather information. The prompts
section in the meta.json
file tells the CLI which questions to ask and when.
They are almost exactly the same as when defining prompts with vanilla inquirer. The only difference is that for every Inquirer question key that supports a function, we've already wrapped your value in function for you so that we can expose additional properties. Therefore your default
, when
, filter
, and validate
values can reference:
answers
- The ongoing answers to the questionnaire in the current Inquirer session.options
- The existing options prior to promptingpkg
- Anypackage.json
fields for the existing Feathers appconfig.default
- Anyconfig/default.json
fields for the existing appconfig.staging
- Anyconfig/staging.json
fields for the existing appconfig.production
- Anyconfig/production.json
fields for the existing app
See this meta.json for an example.
Generating an app or component goes something like this:
const inquirer = require('inquirer');
const Generator = require('feathers-generator');
const args = {
template: 'app', // one of the templates
name: 'my-app', // the name of your generated thing
root: '/path/to/project/root', // the root directory of your project
force: false, // whether it should override any existing values or files without confirmation
};
// Get the appropriate generator based on the `template` you supplied
// By this point the generator has already read in existing app configs
// and the package.json file.
const generator = Generator(args);
generator.getQuestions()
.then(questions => {
// Get user to answer questions
return inquirer.prompt(questions);
})
.then(answers => {
// Send answers back to generator
return generator.generate(answers);
})
.then(dependencies => {
// npm install dependencies
// { devDependencies: [], dependencies: [] }
})
.catch(error => {
if (error) {
console.log(error));
}
});