index.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. var environment = require("./environment"),
  2. FileManager = require("./file-manager"),
  3. UrlFileManager = require("./url-file-manager"),
  4. createFromEnvironment = require("../less"),
  5. less = createFromEnvironment(environment, [new FileManager(), new UrlFileManager()]),
  6. lesscHelper = require('./lessc-helper');
  7. // allow people to create less with their own environment
  8. less.createFromEnvironment = createFromEnvironment;
  9. less.lesscHelper = lesscHelper;
  10. less.PluginLoader = require("./plugin-loader");
  11. less.fs = require("./fs");
  12. less.FileManager = FileManager;
  13. less.UrlFileManager = UrlFileManager;
  14. less.formatError = function(ctx, options) {
  15. options = options || {};
  16. var message = "";
  17. var extract = ctx.extract;
  18. var error = [];
  19. var stylize = options.color ? lesscHelper.stylize : function (str) { return str; };
  20. // only output a stack if it isn't a less error
  21. if (ctx.stack && !ctx.type) { return stylize(ctx.stack, 'red'); }
  22. if (!ctx.hasOwnProperty('index') || !extract) {
  23. return ctx.stack || ctx.message;
  24. }
  25. if (typeof extract[0] === 'string') {
  26. error.push(stylize((ctx.line - 1) + ' ' + extract[0], 'grey'));
  27. }
  28. if (typeof extract[1] === 'string') {
  29. var errorTxt = ctx.line + ' ';
  30. if (extract[1]) {
  31. errorTxt += extract[1].slice(0, ctx.column) +
  32. stylize(stylize(stylize(extract[1].substr(ctx.column, 1), 'bold') +
  33. extract[1].slice(ctx.column + 1), 'red'), 'inverse');
  34. }
  35. error.push(errorTxt);
  36. }
  37. if (typeof extract[2] === 'string') {
  38. error.push(stylize((ctx.line + 1) + ' ' + extract[2], 'grey'));
  39. }
  40. error = error.join('\n') + stylize('', 'reset') + '\n';
  41. message += stylize(ctx.type + 'Error: ' + ctx.message, 'red');
  42. if (ctx.filename) {
  43. message += stylize(' in ', 'red') + ctx.filename +
  44. stylize(' on line ' + ctx.line + ', column ' + (ctx.column + 1) + ':', 'grey');
  45. }
  46. message += '\n' + error;
  47. if (ctx.callLine) {
  48. message += stylize('from ', 'red') + (ctx.filename || '') + '/n';
  49. message += stylize(ctx.callLine, 'grey') + ' ' + ctx.callExtract + '/n';
  50. }
  51. return message;
  52. };
  53. less.writeError = function (ctx, options) {
  54. options = options || {};
  55. if (options.silent) { return; }
  56. console.error(less.formatError(ctx, options));
  57. };
  58. // provide image-size functionality
  59. require('./image-size')(less.environment);
  60. module.exports = less;