Skip to content

Instantly share code, notes, and snippets.

@nicoespeon
Last active March 23, 2016 23:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicoespeon/fcfff37708cec18d66a6 to your computer and use it in GitHub Desktop.
Save nicoespeon/fcfff37708cec18d66a6 to your computer and use it in GitHub Desktop.
Blog - Plop — a micro-generator to ease your daily life - concrete example
const modulePath = "app/modules/{{camelCase name}}.js";
module.exports = ( plop ) => {
plop.setGenerator( "model", {
// …
actions: [
// Add a new model + tests boilerplate.
{
type: "add",
path: "app/modules/{{camelCase name}}.model.js",
templateFile: "plop-templates/model.js"
},
{
type: "add",
path: "app/tests/{{camelCase name}}.model.tests.js",
templateFile: "plop-templates/model.tests.js"
},
// Modify the module file to inject created model.
// This is basically RegExp replacement.
{
type: "modify",
path: modulePath,
pattern: /(\/\/ IMPORT MODULE FILES)/g,
template: "$1\nimport Model from \"./{{camelCase name}}.model\";"
},
{
type: "modify",
path: modulePath,
pattern: /(const namespace = "\w+";)/g,
template: "$1\n\nModel = Model.extend( { namespace: namespace } );"
}
]
} );
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment