macro

Macros for nodejs

npm install macro
2 downloads in the last week
3 downloads in the last month

Information

Packagemacrojs
Description Macros for nodejs
Node Version >= 0.4

Usage

Defining macros

You only need to define your macros and use register() once when your app starts

macros = require 'macros'
macro.add 'debug', (node) ->
  out = "console.log('Debug at line #{node.line}')\r\n"
  out += "console.trace()"
  return out

macro.add 'topload', (path, node) -> "require('../#{path}')"
macro.add 'lrequire', (path, node) -> "require('./#{path}')"
macro.add 'add', (numone, numtwo, node) -> String numone + numtwo

will replace

var appcfg = topload('config');
var dbcfg = lrequire('db_config');
var sum = add(1, 3);
debug();

with

var appcfg = require("../config");
var dbcfg = require("./db_config");
var sum = 4;
console.log("Debug at line 3");
console.trace();

Raw input

Pass in a string of JS, get out a string of transformed JS.

macros = require 'macros'
fs = require 'fs'
# Define your macros here

input = fs.readFileSync "coolinput.js"
output = macros.run input
fs.writefileSync output, "coolinput.out.js"

Registering files

All files loaded with require() will be passed through the macro transformer before being loaded.

macros = require 'macros'
macros.register()
# Define your macros here

Examples

You can view further examples in the example folder.

LICENSE

(MIT License)

Copyright (c) 2012 Fractal contact@wearefractal.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

npm loves you