2011-01-05 02:07:27 +08:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
|
|
|
var path = require('path'),
|
|
|
|
fs = require('fs'),
|
|
|
|
sys = require('sys');
|
|
|
|
|
|
|
|
require.paths.unshift(path.join(__dirname, '..', 'lib'));
|
|
|
|
|
2011-01-06 03:23:28 +08:00
|
|
|
var mess = require('mess');
|
2011-01-05 02:07:27 +08:00
|
|
|
var args = process.argv.slice(1);
|
|
|
|
var options = {
|
|
|
|
compress: false,
|
|
|
|
optimization: 1,
|
2011-01-14 03:15:05 +08:00
|
|
|
silent: false,
|
2011-01-14 03:24:03 +08:00
|
|
|
json: false
|
2011-01-05 02:07:27 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
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':
|
2011-01-06 03:23:28 +08:00
|
|
|
sys.puts("messc " + mess.version.join('.') + " (MESS Compiler) [JavaScript]");
|
2011-01-05 02:07:27 +08:00
|
|
|
process.exit(0);
|
|
|
|
case 'verbose':
|
|
|
|
options.verbose = true;
|
|
|
|
break;
|
2011-01-14 03:15:05 +08:00
|
|
|
case 'j':
|
|
|
|
options.json = true;
|
2011-01-19 03:31:04 +08:00
|
|
|
case 'd':
|
|
|
|
options.debug = true;
|
2011-01-05 02:07:27 +08:00
|
|
|
case 's':
|
|
|
|
case 'silent':
|
|
|
|
options.silent = true;
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
case 'help':
|
2011-01-14 03:15:05 +08:00
|
|
|
sys.puts("Usage: messc source");
|
|
|
|
sys.puts("Options:");
|
|
|
|
sys.puts(" -j\tParse JSON map manifest");
|
2011-01-05 02:07:27 +08:00
|
|
|
process.exit(0);
|
2011-01-20 06:24:21 +08:00
|
|
|
case 'l':
|
|
|
|
case 'layers':
|
|
|
|
options.layers = true;
|
|
|
|
break;
|
2011-01-05 02:07:27 +08:00
|
|
|
case 'x':
|
|
|
|
case 'compress':
|
|
|
|
options.compress = true;
|
|
|
|
break;
|
|
|
|
case 'O0': options.optimization = 0; break;
|
|
|
|
case 'O1': options.optimization = 1; break;
|
|
|
|
case 'O2': options.optimization = 2; 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) {
|
2011-01-06 03:23:28 +08:00
|
|
|
sys.puts("messc: no input files");
|
2011-01-05 02:07:27 +08:00
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
fs.readFile(input, 'utf-8', function (e, data) {
|
|
|
|
if (e) {
|
2011-01-06 03:23:28 +08:00
|
|
|
sys.puts("messc: " + e.message);
|
2011-01-05 02:07:27 +08:00
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
2011-01-20 06:24:21 +08:00
|
|
|
if (options.layers) {
|
|
|
|
// Loads layer definitions from a .layers file in the same directory
|
|
|
|
// as the .mss file with the same basename.
|
|
|
|
var layerFile = input.replace(/\.\w+$/, '.layers');
|
|
|
|
|
|
|
|
fs.readFile(layerFile, 'utf-8', function(err, layers) {
|
|
|
|
if (err) {
|
|
|
|
sys.puts("messc: " + err.message);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
var m = '{ "Stylesheet": [{ "id": "' + input +'", "data": "' + data.replace(/\n/g, '\\n') + '" }], "Layer": ' + layers + ' }';
|
|
|
|
new mess.Renderer({
|
|
|
|
paths: [path.dirname(input)],
|
|
|
|
optimization: options.optimization,
|
|
|
|
filename: input
|
|
|
|
}).render(m, function(err, output) {
|
|
|
|
if (err) {
|
|
|
|
mess.writeError(err, options);
|
|
|
|
process.exit(1);
|
|
|
|
} else {
|
|
|
|
sys.puts(output);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} else if (options.json) {
|
2011-01-22 05:17:52 +08:00
|
|
|
new mess.Renderer({
|
2011-01-14 03:24:03 +08:00
|
|
|
paths: [path.dirname(input)],
|
|
|
|
optimization: options.optimization,
|
|
|
|
filename: input
|
|
|
|
}).render(data, function (err, output) {
|
|
|
|
if (err) {
|
|
|
|
mess.writeError(err, options);
|
|
|
|
process.exit(1);
|
|
|
|
} else {
|
|
|
|
sys.puts(output);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
2011-01-22 05:17:52 +08:00
|
|
|
new mess.Parser({
|
2011-01-14 03:24:03 +08:00
|
|
|
paths: [path.dirname(input)],
|
|
|
|
optimization: options.optimization,
|
|
|
|
filename: input
|
|
|
|
}).parse(data, function (err, tree) {
|
|
|
|
if (err) {
|
2011-01-18 03:03:00 +08:00
|
|
|
mess.writeError(err, options);
|
2011-01-20 04:06:42 +08:00
|
|
|
// process.exit(1);
|
2011-01-14 03:24:03 +08:00
|
|
|
} else {
|
|
|
|
try {
|
2011-01-20 04:06:42 +08:00
|
|
|
// if (options.debug) {
|
|
|
|
// try {
|
|
|
|
// require('eyes').inspect(tree.toAST());
|
|
|
|
// } catch (e) {
|
|
|
|
// console.log(e);
|
|
|
|
// console.log(tree.toAST());
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
var mss = tree.toMSS({ compress: options.compress });
|
|
|
|
sys.print(mss);
|
2011-01-14 03:24:03 +08:00
|
|
|
} catch (e) {
|
2011-01-18 03:03:00 +08:00
|
|
|
mess.writeError(e, options);
|
2011-01-20 04:06:42 +08:00
|
|
|
// process.exit(2);
|
2011-01-14 03:24:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2011-01-05 02:07:27 +08:00
|
|
|
});
|