Grunt task that generates files from user-defined templates.
Grunt plugin that generates files from user-defined templates.
It doesn't aim to replace grunt-init or Yo, but rather complement it. Yo generators are cool, but opinionated. This task allows you to set up your own templates and start using them right away, organized any which way you want.
When you run the task it basically looks for a specified template, processes it and uses it to generate a file right into your project structure. The real benefits are that the task is highly configurable and allows you to create files from templates of your own making (there are some pre-defined ones though) into a structure you have decided upon.
This grunt task is a derivative/enhancement of the backbone_generate grunt task.
This plugin requires Grunt
If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:
npm install grunt-generate --save-dev
grunt-generate now respects files with multiple extensions, e.g.
template.es6.js will generate a
v0.3 introduces an entirely new API and is most definitely NOT backwards compatible. Compared to the previous versions it should be more clear, intuitive and save you some typing.
Some pre-defined templates are shipped with grunt-generate, let's take a look how you would start using the pre-defined templates.
We're going to use the template
templates/backbone/Model.js and use it to generate
In your project's Gruntfile, add a section named
generate to the data object passed into
dest tells the task where to put the generated files by default.
Since most of our templates will be used in the application itself we allow them to be generated into
This will generate a file
app/scripts/models/UserModel by processing the template
Et voila, you just scaffolded your very first file using
grunt-generate is simple to use yet pretty powerful. You can tell the task where to generate your files depending on what template you use, either by configuration or through task arguments.
Let's take a look how this is done.
You can map template directories to destination directories, which means that any templates from a particular template directory will generate files into another specified directory.
Add the following to the above grunt configuration:
If you run
It will process the
nodeunit/test.js template and generate a file
test_foo.js into the
There's two things happening here:
destby preceding our mapping destination with "/" (forward slash). It tells the task to forget whatever value
You can also map specific template files to specific destinations.
When we run
It will generate a file
Any file-specific mappings override directory mappings. For instance if we have this configuration:
It will generate all backbone files (
Views will be generated into
More complex path structures can be created using the
:dir variable inside destinations, it will be replaced with the path-part of your arguments, i.e. everything which follows "@" (at-sign).
Allows you to organize your files by domain, like this:
This will generate
Grunt is automatically exposed, so you have access to the full Grunt API.
There are a few special variables you can use however:
filecontains all information on the generated file, e.g. for
name: 'LoginView'directory: 'user/views'basename: 'LoginView.js'template: '.tmp/:dir/:basename'relative: '.tmp/user/views/LoginView.js'absolute: '/Users/creynder/Dropbox/Work/Projects/grunt-generate/.tmp/user/views/LoginView.js'path: '.tmp/user/views'
//<%= file.basename %>, 2014 (c) Camille Reynders
metacontains information that can be useful for class declarations:
className: 'Loginview'type: 'backbone.View'package: 'user.views'fqn: 'user.views.Loginview'
app<%= metaclassName %> = BackboneView;
var jshintrc = '<%= jshint.options.jshintrc %>';
Path to the source templates directory. Do not end this with a
Relative to the Gruntfile.
Defines the default destination directory.
Maps template directories to destination directories.
true you'll be asked confirmation whether you want to generate the file or not.
We'd like to provide some more pre-defined templates, since people can use them as a starting point for their own templates. If you happen to have some lying around we'd be very happy to add them.
In lieu of a formal styleguide, take care to maintain the existing coding style.
Add unit tests for any new or changed functionality.
Lint and test your code:
.editorconfig file is provided; use it!