Skip to content

Instantly share code, notes, and snippets.

@couto
Created August 6, 2012 15:21
Show Gist options
  • Save couto/3275454 to your computer and use it in GitHub Desktop.
Save couto/3275454 to your computer and use it in GitHub Desktop.
Javascript Preprocessor
#!/usr/bin/env node
/*!
* Javascript Preprocessor
* Mimicks the #include syntax in C
*
* @example
*
* //#include "relative/folder/file.js";
*
* Copyright (c) 2012 Couto
* Licensed under the MIT license.
*/
// Dependencies
var fs = require('fs'),
path = require('path'),
// args
args = process.argv,
input = args[2],
output = args[3],
// files and stuff
folder = path.dirname(input),
file = fs.readFileSync(input, 'utf8'),
merged = file.replace(/\/\/\#include \"(.+)\"\;/g, function (str, group) {
return fs.readFileSync(path.resolve(folder, group), 'utf8');
});
return (!output) ?
process.stdout.write(merged) :
(function () {
fs.writeFileSync(output, merged, 'utf8');
process.stdout.write(output);
process.exit();
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment