Forge: Template generators for Node.js

One of the Rails-isms I miss most when developing in Node.js is easy access to repetition-quashing generators. At new Rails model, for instance, is just a few keystrokes away:

$ rails g model contact

Press enter, and Rails’ built-in model generator will happily produce not only the model but also a database migration, unit test, and empty test fixture:

invoke  active_record
  create    db/migrate/20120728215235_create_contacts.rb
  create    app/models/contact.rb
  invoke    test_unit
    create      test/unit/contact_test.rb
    create      test/fixtures/contacts.yml

Besides introducing some modicum of uniformity to new models, it’s an approach that avoids repetition. Being lazy and averse to repetition in all its forms, I took a little time to write a minimally-featured analog in node. node-forge isn’t nearly ready for the npm registry just yet, but it’s available for testing and development via github:

$ cd ~/project_dir
$ git clone

Once Forge has been installed into the node-forge folder in a project’s working directory, new generators are easy to add. From the project directory, invoke forge.js to create a new one:

$ node ./node-forge/forge.js create generator model

At present, generators may overload two “actions”—Generator.create() and Generator.destroy()—to provide basic project templating. As the default implementation of destroy (attempt to revert actions taken when create is invoked) will be sufficient for most basic generators, create is the more interesting method of the two. It looks a little like this:

// in ./node-forge/generators/model/model.js
var Generator = require('./../../src/Generator');

module.exports = Generator.extend({

  key: 'model',

  create: function (name, options) {
    this.template('model.js',  './models/' + name + '.js', {});
    this.invoke = function ('create', 'view', name + 'View', {}); 

The version of create described for this generator will automate three tasks:

  1. Create a new directory to contain models in the project directory
  2. Add a new model based on a template ('model.js') from the generator’s templates/ directory
  3. Invoke another as-of-yet undefined generator ('view') to generate a view for the model

Forge remains under development, but in the meantime, please don’t hesitate to fork and contribute on Github!