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