0.1.0 • Published 8 years ago

generate-file v0.1.0

Weekly downloads
3
License
MIT
Repository
github
Last release
8 years ago

generate-file NPM version NPM downloads Build Status

Generator for generating a single file from a template.

What is generate?

Generate is a new, open source developer framework for rapidly initializing and scaffolding out new code projects, offering an intuitive CLI, and a powerful and expressive API that makes it easy and enjoyable to use.

Visit the getting started guide or the generate project and documentation to learn more.

Quickstart

generate-file is a node.js application that is installed using npm. If you're unfamiliar with generate, it might help to visit the generate readme, or visit the getting started guide before continuing on.

Usage


CLI

Installing the CLI

To run the file generator from the command line, you'll need to install generate globally first. You can do that now with the following command:

$ npm i -g generate

This adds the gen command to your system path, allowing it to be run from any directory.

Help

Get general help and a menu of available commands:

$ gen help

Running the file generator

Once both generate and generate-file are installed globally, you can run the generator with the following command:

$ gen file

If completed successfully, you should see both starting and finished events in the terminal, like the following:

[00:44:21] starting ...
...
[00:44:22] finished ✔

If you do not see one or both of those events, please let us know about it.

Tasks

generate file

Creates a task for generating a single file for each view passed on options.views. If options.views is undefined, the templates collection is used.

Params

  • options {Object}

Example

var file = require('generate-file');

// use as a plugin
var generate = require('generate');
var app = generate();
app.template('license', {contents: fs.readFileSync('LICENSE')});
app.use(file());

app.generate('license', function(err) {
  if (err) return console.error(err);
});

// use as a plugin in your generator
module.exports = function(app) {
  app.use(file());
  // do other generator stuff
};

// use as a sub-generator
module.exports = function(app) {
  app.register('foo', file());
  // do other generator stuff

  app.task('default', function(cb) {
    // run task `bar` on sub-generator `foo`
    app.generate('foo:bar', cb);
  });
};

API

Use as a plugin in your generator

Use as a plugin if you want to extend your own generator with the features, settings and tasks of generate-file, as if they were created on your generator.

In your generator.js:

module.exports = function(app) {
  app.use(require('generate-file'));

  // specify any tasks from generate-file. Example:
  app.task('default', ['file']);
};

Use as a sub-generator

Use as a sub-generator if you want expose the features, settings and tasks from generate-file on a namespace in your generator.

In your generator.js:

module.exports = function(app) {
  // register the generate-file generator (as a sub-generator with an arbitrary name)
  app.register('foo', require('generate-file'));

  app.task('minify', function(cb) {
    // minify some stuff
    cb();
  });

  // run the "default" task on generate-file (aliased as `foo`), 
  // then run the `minify` task defined in our generator
  app.task('default', function(cb) {
    app.generate(['foo:default', 'minify'], cb);
  });
};

Tasks from generate-file will be available on the foo namespace from the API and the command line. Continuing with the previous code example, to run the default task on generate-file, you would run gen foo:default (or just gen foo if foo does not conflict with an existing task on your generator).

To learn more about namespaces and sub-generators, and how they work, visit the getting started guide.


Examples

This generator returns a function that needs to be called, so you can pass an options object if you need to customize anything.

var file = require('generate-file');
var generate = require('generate');
var app = generate();

// create an arbitrary template collection, or use
// the built-in generic collection `templates`
app.create('docs');

// add some views (a task will be created using the filename
// of each view, so you can run `foo` to generate the `foo` file)
app.doc('foo', {content: 'this is an example template'});
app.doc('bar', {content: 'another example template'});

// pass the collection on `options.views`, or if nothing
// is passed the `templates` view collection will be used
app.use(file({views: app.views.docs}));

Defaults

The following example shows how to use a custom collection. See the API docs below for an example of how to use the built-in generic templates collection.

var file = require('generate-file');
var generate = require('generate');
var app = generate();

// create an arbitrary template collection, or use
// the built-in generic collection `templates`
app.create('docs');

// add some views (a task will be created using the filename
// of each view, so you can run `foo` to generate the `foo` file)
app.doc('foo', {content: 'this is an example template'});
app.doc('bar', {content: 'another example template'});

// pass the collection on `options.views`, or if nothing
// is passed the `templates` view collection will be used
app.use(file({views: app.views.docs}));

Related projects

You might also be interested in these projects:

  • generate-dest: Generate generator that prompts the user for the destination directory to use. Can be used… more | homepage
  • generate-node: Generate a node.js project, with everything you need to begin writing code and easily publish… more | homepage
  • generate: Fast, composable, highly pluggable project generator with a user-friendly and expressive API. | homepage

Contributing

This document was generated by verb, please don't edit directly. Any changes to the readme must be made in .verb.md. See Building Docs.

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

Building docs

Generate readme and API documentation with verb:

$ npm install -g verb verb-readme-generator && verb

Running tests

Install dev dependencies:

$ npm install -d && npm test

Author

Jon Schlinkert

License

Copyright © 2016, Jon Schlinkert. Released under the MIT license.


This file was generated by verb, v0.9.0, on June 01, 2016.