/app/node_modules/less/bin/lessc

https://bitbucket.org/jorritposthuma/inventory-counter-mobile · #! · 139 lines · 125 code · 14 blank · 0 comment · 0 complexity · 1f7b880ec9518f087bbe1013768e2bc3 MD5 · raw file

  1. #!/usr/bin/env node
  2. var path = require('path'),
  3. fs = require('fs'),
  4. sys = require('util'),
  5. os = require('os');
  6. var less = require('../lib/less');
  7. var args = process.argv.slice(1);
  8. var options = {
  9. compress: false,
  10. yuicompress: false,
  11. optimization: 1,
  12. silent: false,
  13. paths: [],
  14. color: true,
  15. strictImports: false
  16. };
  17. args = args.filter(function (arg) {
  18. var match;
  19. if (match = arg.match(/^-I(.+)$/)) {
  20. options.paths.push(match[1]);
  21. return false;
  22. }
  23. if (match = arg.match(/^--?([a-z][0-9a-z-]*)(?:=([^\s]+))?$/i)) { arg = match[1] }
  24. else { return arg }
  25. switch (arg) {
  26. case 'v':
  27. case 'version':
  28. sys.puts("lessc " + less.version.join('.') + " (LESS Compiler) [JavaScript]");
  29. process.exit(0);
  30. case 'verbose':
  31. options.verbose = true;
  32. break;
  33. case 's':
  34. case 'silent':
  35. options.silent = true;
  36. break;
  37. case 'strict-imports':
  38. options.strictImports = true;
  39. break;
  40. case 'h':
  41. case 'help':
  42. sys.puts("usage: lessc source [destination]");
  43. process.exit(0);
  44. case 'x':
  45. case 'compress':
  46. options.compress = true;
  47. break;
  48. case 'yui-compress':
  49. options.yuicompress = true;
  50. break;
  51. case 'no-color':
  52. options.color = false;
  53. break;
  54. case 'include-path':
  55. options.paths = match[2].split(os.type().match(/Windows/) ? ';' : ':')
  56. .map(function(p) {
  57. if (p) {
  58. return path.resolve(process.cwd(), p);
  59. }
  60. });
  61. break;
  62. case 'O0': options.optimization = 0; break;
  63. case 'O1': options.optimization = 1; break;
  64. case 'O2': options.optimization = 2; break;
  65. }
  66. });
  67. var input = args[1];
  68. if (input && input != '-') {
  69. input = path.resolve(process.cwd(), input);
  70. }
  71. var output = args[2];
  72. if (output) {
  73. output = path.resolve(process.cwd(), output);
  74. }
  75. var css, fd, tree;
  76. if (! input) {
  77. sys.puts("lessc: no input files");
  78. process.exit(1);
  79. }
  80. var parseLessFile = function (e, data) {
  81. if (e) {
  82. sys.puts("lessc: " + e.message);
  83. process.exit(1);
  84. }
  85. new(less.Parser)({
  86. paths: [path.dirname(input)].concat(options.paths),
  87. optimization: options.optimization,
  88. filename: input,
  89. strictImports: options.strictImports
  90. }).parse(data, function (err, tree) {
  91. if (err) {
  92. less.writeError(err, options);
  93. process.exit(1);
  94. } else {
  95. try {
  96. css = tree.toCSS({
  97. compress: options.compress,
  98. yuicompress: options.yuicompress
  99. });
  100. if (output) {
  101. fd = fs.openSync(output, "w");
  102. fs.writeSync(fd, css, 0, "utf8");
  103. } else {
  104. sys.print(css);
  105. }
  106. } catch (e) {
  107. less.writeError(e, options);
  108. process.exit(2);
  109. }
  110. }
  111. });
  112. };
  113. if (input != '-') {
  114. fs.readFile(input, 'utf-8', parseLessFile);
  115. } else {
  116. process.stdin.resume();
  117. process.stdin.setEncoding('utf8');
  118. var buffer = '';
  119. process.stdin.on('data', function(data) {
  120. buffer += data;
  121. });
  122. process.stdin.on('end', function() {
  123. parseLessFile(false, buffer);
  124. });
  125. }