carto/bin/lessc

64 lines
1.7 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');
require.paths.unshift(__dirname, path.join(__dirname, '..'));
var less = require('index');
2010-03-06 04:33:36 +08:00
var args = process.argv.slice(1);
args = args.filter(function (arg) {
2010-03-06 08:17:32 +08:00
var match;
if (match = arg.match(/^--?([a-z][a-z-]*)$/i)) { arg = match[1] }
2010-03-06 04:33:36 +08:00
else { return arg }
switch (arg) {
case 'v':
sys.puts("lessc " + less.version.join('.') + " (LESS Compiler)");
process.exit(0);
case 'h':
sys.puts("usage: lessc source [destination]");
process.exit(0);
}
});
var input = args[1] && path.join(process.cwd(), args[1]),
output = args[2] && path.join(process.cwd(), args[2]);
2010-02-27 07:18:11 +08:00
var css, fd, tree;
2010-03-06 04:33:36 +08:00
if (! input) {
process.stdio.writeError("lessc: no input files\n");
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) {
process.stdio.writeError("lessc: " + e.message + ": '" + input + "'\n");
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)]
}).parse(data, function (err, tree) {
if (err) {
process.stdio.writeError(err);
} else {
css = tree.toCSS();
if (output) {
fd = fs.openSync(output, "w");
fs.writeSync(fd, css, 0, "utf8");
} else {
sys.print(css);
}
}
});
2010-02-27 07:18:11 +08:00
});
});
});