Created
February 10, 2013 23:25
-
-
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).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You'll need commander:
$ npm install commander