carto/bin/lessc

99 lines
2.6 KiB
Plaintext
Raw Normal View History

2010-02-27 07:18:11 +08:00
#!/usr/bin/env node
var path = require('path'),
fs = require('fs'),
sys = require('sys');
2010-05-05 13:00:00 +08:00
require.paths.unshift(path.join(__dirname, '..', 'lib'));
2010-02-27 07:18:11 +08:00
2010-05-05 13:00:00 +08:00
var less = require('less');
2010-03-06 04:33:36 +08:00
var args = process.argv.slice(1);
var options = {
compress: false,
optimization: 1,
silent: false
};
2010-03-06 04:33:36 +08:00
args = args.filter(function (arg) {
2010-03-06 08:17:32 +08:00
var match;
2010-06-17 06:25:40 +08:00
if (match = arg.match(/^--?([a-z][0-9a-z-]*)$/i)) { arg = match[1] }
2010-03-06 04:33:36 +08:00
else { return arg }
switch (arg) {
case 'v':
2010-06-02 09:48:13 +08:00
case 'version':
sys.puts("lessc " + less.version.join('.') + " (LESS Compiler) [JavaScript]");
2010-03-06 04:33:36 +08:00
process.exit(0);
case 'verbose':
options.verbose = true;
break;
case 's':
case 'silent':
options.silent = true;
break;
2010-03-06 04:33:36 +08:00
case 'h':
2010-06-02 09:48:13 +08:00
case 'help':
2010-03-06 04:33:36 +08:00
sys.puts("usage: lessc source [destination]");
process.exit(0);
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;
2010-03-06 04:33:36 +08:00
}
});
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);
}
2010-02-27 07:18:11 +08:00
var css, fd, tree;
2010-03-06 04:33:36 +08:00
if (! input) {
2010-06-02 09:48:13 +08:00
sys.puts("lessc: no input files");
2010-03-06 04:33:36 +08:00
process.exit(1);
}
2010-02-27 07:18:11 +08:00
fs.stat(input, function (e, stats) {
2010-03-06 04:33:36 +08:00
if (e) {
sys.puts("lessc: " + e.message);
2010-03-06 04:33:36 +08:00
process.exit(1);
}
2010-02-27 07:18:11 +08:00
fs.open(input, process.O_RDONLY, stats.mode, function (e, fd) {
fs.read(fd, stats.size, 0, "utf8", function (e, data) {
new(less.Parser)({
paths: [path.dirname(input)],
optimization: options.optimization,
filename: input
}).parse(data, function (err, tree) {
if (err) {
less.writeError(err, options);
process.exit(1);
} else {
2010-06-12 09:46:24 +08:00
try {
css = tree.toCSS({ compress: options.compress });
if (output) {
fd = fs.openSync(output, "w");
fs.writeSync(fd, css, 0, "utf8");
} else {
sys.print(css);
}
} catch (e) {
less.writeError(e, options);
process.exit(2);
}
}
});
2010-02-27 07:18:11 +08:00
});
});
});