mirror of
https://github.com/less/less.js.git
synced 2026-04-09 03:00:20 -04:00
lessc: Defend against missing output directories
If an output specifies a destination inside a directory that doesn't exist, it blows up unceremoniously. Let's avoid that. Also, uses modern fs.writeFileSync instead of manual open/write/closeSync.
This commit is contained in:
committed by
Luke Page
parent
be2c938944
commit
f4105114f0
16
bin/lessc
16
bin/lessc
@@ -101,7 +101,12 @@ if (! input) {
|
||||
return;
|
||||
}
|
||||
|
||||
var css, fd, tree;
|
||||
var ensureDirectory = function (filepath) {
|
||||
var dir = path.dirname(filepath);
|
||||
if (!fs.existsSync(dir)) {
|
||||
fs.mkdirSync(dir);
|
||||
}
|
||||
};
|
||||
|
||||
var parseLessFile = function (e, data) {
|
||||
if (e) {
|
||||
@@ -123,14 +128,13 @@ var parseLessFile = function (e, data) {
|
||||
return;
|
||||
} else {
|
||||
try {
|
||||
css = tree.toCSS({
|
||||
var css = tree.toCSS({
|
||||
compress: options.compress,
|
||||
yuicompress: options.yuicompress
|
||||
});
|
||||
if (output) {
|
||||
fd = fs.openSync(output, "w");
|
||||
fs.writeSync(fd, css, 0, "utf8");
|
||||
fs.closeSync(fd);
|
||||
ensureDirectory(output);
|
||||
fs.writeFileSync(output, css, 'utf8');
|
||||
} else {
|
||||
sys.print(css);
|
||||
}
|
||||
@@ -144,7 +148,7 @@ var parseLessFile = function (e, data) {
|
||||
};
|
||||
|
||||
if (input != '-') {
|
||||
fs.readFile(input, 'utf-8', parseLessFile);
|
||||
fs.readFile(input, 'utf8', parseLessFile);
|
||||
} else {
|
||||
process.stdin.resume();
|
||||
process.stdin.setEncoding('utf8');
|
||||
|
||||
Reference in New Issue
Block a user