mirror of
https://github.com/less/less.js.git
synced 2026-04-09 03:00:20 -04:00
tidy code up, change to callback errors instead of system exit
This commit is contained in:
@@ -46,7 +46,7 @@ var less = {
|
||||
// only output a stack if it isn't a less error
|
||||
if (ctx.stack && !ctx.type) { return stylize(ctx.stack, 'red') }
|
||||
|
||||
if (!ctx.hasOwnProperty('index')) {
|
||||
if (!ctx.hasOwnProperty('index') || !extract) {
|
||||
return ctx.stack || ctx.message;
|
||||
}
|
||||
|
||||
@@ -100,8 +100,24 @@ var less = {
|
||||
var isUrlRe = /^(?:https?:)?\/\//i;
|
||||
|
||||
less.Parser.importer = function (file, paths, callback, env) {
|
||||
var pathname, data;
|
||||
var pathname, dirname, data;
|
||||
|
||||
function parseFile(e, data) {
|
||||
if (e) return callback(e);
|
||||
|
||||
env.contents[pathname] = data; // Updating top importing parser content cache.
|
||||
new(less.Parser)({
|
||||
paths: [dirname].concat(paths),
|
||||
filename: pathname,
|
||||
contents: env.contents,
|
||||
files: env.files,
|
||||
syncImport: env.syncImport,
|
||||
dumpLineNumbers: env.dumpLineNumbers
|
||||
}).parse(data, function (e, root) {
|
||||
callback(e, root, pathname);
|
||||
});
|
||||
};
|
||||
|
||||
var isUrl = isUrlRe.test( file );
|
||||
if (isUrl || isUrlRe.test(paths[0])) {
|
||||
|
||||
@@ -119,80 +135,61 @@ less.Parser.importer = function (file, paths, callback, env) {
|
||||
body += chunk.toString();
|
||||
});
|
||||
res.on('end', function () {
|
||||
if (!body) {
|
||||
sys.error( 'Note: Empty body (HTTP '+ res.statusCode + ') returned by "' + urlStr +'"' );
|
||||
if (res.statusCode === 404) {
|
||||
callback({ type: 'File', message: "resource '" + urlStr + "' was not found\n" });
|
||||
}
|
||||
new(less.Parser)({
|
||||
paths: [urlObj.protocol +'//'+ urlObj.host + urlObj.pathname.replace(/[^\/]*$/, '')],
|
||||
filename: urlStr
|
||||
}).parse(body, function (e, root) {
|
||||
callback(e, root, body);
|
||||
});
|
||||
if (!body) {
|
||||
sys.error( 'Warning: Empty body (HTTP '+ res.statusCode + ') returned by "' + urlStr +'"' );
|
||||
}
|
||||
pathname = urlStr;
|
||||
dirname = urlObj.protocol +'//'+ urlObj.host + urlObj.pathname.replace(/[^\/]*$/, '');
|
||||
parseFile(null, body);
|
||||
});
|
||||
}).on('error', function (err) {
|
||||
sys.error("resource '" + file + "' gave this Error:");
|
||||
sys.error(" "+ err +'\n' );
|
||||
process.exit(1);
|
||||
callback({ type: 'File', message: "resource '" + urlStr + "' gave this Error:\n "+ err +"\n" });
|
||||
});
|
||||
|
||||
} else {
|
||||
|
||||
// TODO: Undo this at some point,
|
||||
// or use different approach.
|
||||
var paths = [].concat(paths);
|
||||
paths.push('.');
|
||||
|
||||
// TODO: Undo this at some point,
|
||||
// or use different approach.
|
||||
var paths = [].concat(paths);
|
||||
paths.push('.');
|
||||
|
||||
for (var i = 0; i < paths.length; i++) {
|
||||
try {
|
||||
pathname = path.join(paths[i], file);
|
||||
fs.statSync(pathname);
|
||||
break;
|
||||
} catch (e) {
|
||||
pathname = null;
|
||||
for (var i = 0; i < paths.length; i++) {
|
||||
try {
|
||||
pathname = path.join(paths[i], file);
|
||||
fs.statSync(pathname);
|
||||
break;
|
||||
} catch (e) {
|
||||
pathname = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
paths = paths.slice(0, paths.length - 1);
|
||||
|
||||
paths = paths.slice(0, paths.length - 1);
|
||||
|
||||
if (!pathname) {
|
||||
if (typeof(env.errback) === "function") {
|
||||
env.errback(file, paths, callback);
|
||||
if (!pathname) {
|
||||
if (typeof(env.errback) === "function") {
|
||||
env.errback(file, paths, callback);
|
||||
} else {
|
||||
callback({ type: 'File', message: "'" + file + "' wasn't found.\n" });
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
dirname = path.dirname(pathname);
|
||||
|
||||
if (env.syncImport) {
|
||||
try {
|
||||
data = fs.readFileSync(pathname, 'utf-8');
|
||||
parseFile(null, data);
|
||||
} catch (e) {
|
||||
parseFile(e);
|
||||
}
|
||||
} else {
|
||||
callback({ type: 'File', message: "'" + file + "' wasn't found.\n" });
|
||||
fs.readFile(pathname, 'utf-8', parseFile);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
function parseFile(e, data) {
|
||||
if (e) return callback(e);
|
||||
|
||||
env.contents[pathname] = data; // Updating top importing parser content cache.
|
||||
new(less.Parser)({
|
||||
paths: [path.dirname(pathname)].concat(paths),
|
||||
filename: pathname,
|
||||
contents: env.contents,
|
||||
files: env.files,
|
||||
syncImport: env.syncImport,
|
||||
dumpLineNumbers: env.dumpLineNumbers
|
||||
}).parse(data, function (e, root) {
|
||||
callback(e, root, pathname);
|
||||
});
|
||||
};
|
||||
|
||||
if (env.syncImport) {
|
||||
try {
|
||||
data = fs.readFileSync(pathname, 'utf-8');
|
||||
parseFile(null, data);
|
||||
} catch (e) {
|
||||
parseFile(e);
|
||||
}
|
||||
} else {
|
||||
fs.readFile(pathname, 'utf-8', parseFile);
|
||||
}
|
||||
|
||||
} // END: if ( !isUrl && !isUrlRe.test(paths[0]) )
|
||||
|
||||
}
|
||||
|
||||
require('./functions');
|
||||
|
||||
Reference in New Issue
Block a user