Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@tripleshotsw
Created February 10, 2013 23:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tripleshotsw/4751496 to your computer and use it in GitHub Desktop.
Save tripleshotsw/4751496 to your computer and use it in GitHub Desktop.
Using Node.js to create simple build tools. In this case, 'concat.js' which concatenates JS files (or, really any text files).
var fs = require('fs');
var os = require('os');
var program = require('commander');
var importList = [];
//Options -- also sets up automatic --help message/usage info
program
.version('0.0.1')
.option('-f, --filelist [txtfile]', 'use an input file with a comma separated list of files to concat')
.option('-s, --files [files]', 'specify the files to concat in the option')
.option('-d, --dest [dest]', 'the destination file')
.parse(process.argv);
//does the actual concatenating work
function concat(list, destPath) {
var out = list.map(function(filePath){
var current = filePath.trim();
return fs.readFileSync(current);
});
fs.writeFileSync(destPath, out.join(os.EOL));
console.log(' '+ destPath +' built.');
}
// do we need to display the help?
if (!program.filelist && !program.files || !program.dest) {
program.help(); // exits automatically
}
// the js files are listed in a text file
if (program.filelist) {
var string = fs.readFileSync(program.filelist).toString();
//TODO replace EOL's with ',' so that either could be used.
importList = string.split(',');
}
// there are js files on the command line.
if (program.files) {
var list = program.files.split(',');
// supports both a list file and command line, but list file goes first.
importList = importList.concat(list);
}
// go.
concat(importList, program.dest.trim());
@tripleshotsw
Copy link
Author

You'll need commander:
$ npm install commander

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment