Code Monkey home page Code Monkey logo

ftemp's Introduction

ftemp

Lightweight utility tool to create a template based file generator CLI

Install

npm install ftemp --save-dev

Usage

Given a file called generate.js, which runs ftemp

// generate.js

var ftemp = require('ftemp');
var path = require('path');

ftemp.paths.base = path.join(__dirname, 'src');
ftemp.paths.templates = path.join(__dirname, 'templates');

var testPlugin = {
  command: 'test <name>',
  description: 'Test ftemp',
  options: {
    shout: {
      alias: 's',
      describe: 'Uppercase the given name'
    }
  },
  onMatch: function(argv, paths) {
    var name = argv.shout ? argv.name.toUpperCase() : argv.name;
    return [
      {
        interpolator: { name: name },
        template: path.join(paths.templates, 'test.tpl'),
        path: path.join(paths.base, name, 'index.js')
      }
    ];
  }
};

ftemp.register(testPlugin);
ftemp.run();

and a template

// templates/test.tpl

Hello {{ name }}!

invoking

node generate.js test friend -s

will create a file at src/friend/index.js with the content Hello FRIEND!

Check out the examples folder for a more elaborate use case. The example script is exposed as npm run script in this repository, so you can try

npm run -s generate -- component

API Docs

ftemp.register(conf1, [conf2, conf3, ...])

register is a wrapper around yargs. Configuration objects have the following shape:

{
  command: String,
  description: String,
  options: Object,
  onMatch: Function(argv, paths) -> List<Instruction>
}

onMatch needs to return a list of Instructions - each Instruction will create a file.

{
  interpolator: Object, // Values used within the template
  template: String, // path to a template file
  path: String // path of the file to generate
}

ftemp.run()

Invoke at the end of your ftemp script

ftemp.paths

Helper paths, which are passed to the onMatch function.

Comes with two defaults:

{
  base: String // current working directory
  current: String // current working directory
}

ftemp's People

Contributors

lfdm avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.