carto/bin/messc
2011-02-04 12:25:27 -05:00

102 lines
2.4 KiB
JavaScript
Executable File

#!/usr/bin/env node
var path = require('path'),
fs = require('fs'),
sys = require('sys');
require.paths.unshift(path.join(__dirname, '../lib'), path.join(__dirname, '../lib/node'));
var mess = require('mess');
var args = process.argv.slice(1);
var options = {
silent: false,
json: false
};
args = args.filter(function (arg) {
var match;
if (match = arg.match(/^--?([a-z][0-9a-z-]*)$/i)) { arg = match[1] }
else { return arg }
switch (arg) {
case 'v':
case 'version':
sys.puts("messc " + mess.version.join('.') + " (MESS Compiler) [JavaScript]");
process.exit(0);
break;
case 'verbose':
options.verbose = true;
break;
case 'd':
case 'debug':
options.debug = true;
break;
case 's':
case 'silent':
options.silent = true;
break;
case 'b':
case 'benchmark':
options.benchmark = true;
break;
case 'h':
case 'help':
sys.puts("Usage: messc source");
sys.puts("Options:");
sys.puts(" -j\tParse JSON map manifest");
process.exit(0);
break;
}
});
var input = args[1];
if (input && input[0] != '/') {
input = path.join(process.cwd(), input);
}
var output = args[2];
if (output && output[0] != '/') {
output = path.join(process.cwd(), output);
}
if (!input) {
sys.puts("messc: no input files");
process.exit(1);
}
if (options.benchmark) {
var start = +new Date;
}
fs.readFile(input, 'utf-8', function (e, data) {
if (e) {
sys.puts("messc: " + e.message);
process.exit(1);
}
new mess.Renderer({
filename: input,
debug: options.debug,
local_data_dir: path.dirname(input),
}).render(data, function(err, output) {
if (err) {
if (Array.isArray(err)) {
err.forEach(function(e) {
mess.writeError(e, options);
});
} else {
throw err;
}
process.exit(1);
} else {
if (!options.benchmark) {
sys.puts(output);
} else {
var duration = (+new Date) - start;
console.log('Benchmark: ' + (duration) + 'ms');
}
}
});
});