[483] | 1 | #!/usr/bin/env node |
---|
| 2 | |
---|
| 3 | var path = require('path'), |
---|
| 4 | fs = require('fs'), |
---|
| 5 | sys = require('util'), |
---|
| 6 | os = require('os'); |
---|
| 7 | |
---|
| 8 | var less = require('../lib/less'); |
---|
| 9 | var args = process.argv.slice(1); |
---|
| 10 | var options = { |
---|
| 11 | compress: false, |
---|
| 12 | yuicompress: false, |
---|
| 13 | optimization: 1, |
---|
| 14 | silent: false, |
---|
| 15 | paths: [], |
---|
| 16 | color: true, |
---|
| 17 | strictImports: false, |
---|
| 18 | rootpath: '', |
---|
| 19 | relativeUrls: false |
---|
| 20 | }; |
---|
| 21 | var continueProcessing = true, |
---|
| 22 | currentErrorcode; |
---|
| 23 | |
---|
| 24 | // calling process.exit does not flush stdout always |
---|
| 25 | // so use this to set the exit code |
---|
| 26 | process.on('exit', function() { process.reallyExit(currentErrorcode) }); |
---|
| 27 | |
---|
| 28 | args = args.filter(function (arg) { |
---|
| 29 | var match; |
---|
| 30 | |
---|
| 31 | if (match = arg.match(/^-I(.+)$/)) { |
---|
| 32 | options.paths.push(match[1]); |
---|
| 33 | return false; |
---|
| 34 | } |
---|
| 35 | |
---|
| 36 | if (match = arg.match(/^--?([a-z][0-9a-z-]*)(?:=([^\s]+))?$/i)) { arg = match[1] } |
---|
| 37 | else { return arg } |
---|
| 38 | |
---|
| 39 | switch (arg) { |
---|
| 40 | case 'v': |
---|
| 41 | case 'version': |
---|
| 42 | sys.puts("lessc " + less.version.join('.') + " (LESS Compiler) [JavaScript]"); |
---|
| 43 | continueProcessing = false; |
---|
| 44 | case 'verbose': |
---|
| 45 | options.verbose = true; |
---|
| 46 | break; |
---|
| 47 | case 's': |
---|
| 48 | case 'silent': |
---|
| 49 | options.silent = true; |
---|
| 50 | break; |
---|
| 51 | case 'strict-imports': |
---|
| 52 | options.strictImports = true; |
---|
| 53 | break; |
---|
| 54 | case 'h': |
---|
| 55 | case 'help': |
---|
| 56 | require('../lib/less/lessc_helper').printUsage(); |
---|
| 57 | continueProcessing = false; |
---|
| 58 | case 'x': |
---|
| 59 | case 'compress': |
---|
| 60 | options.compress = true; |
---|
| 61 | break; |
---|
| 62 | case 'yui-compress': |
---|
| 63 | options.yuicompress = true; |
---|
| 64 | break; |
---|
| 65 | case 'no-color': |
---|
| 66 | options.color = false; |
---|
| 67 | break; |
---|
| 68 | case 'include-path': |
---|
| 69 | if (!match[2]) { |
---|
| 70 | sys.puts("include-path option requires a parameter"); |
---|
| 71 | continueProcessing = false; |
---|
| 72 | } else { |
---|
| 73 | options.paths = match[2].split(os.type().match(/Windows/) ? ';' : ':') |
---|
| 74 | .map(function(p) { |
---|
| 75 | if (p) { |
---|
| 76 | return path.resolve(process.cwd(), p); |
---|
| 77 | } |
---|
| 78 | }); |
---|
| 79 | } |
---|
| 80 | break; |
---|
| 81 | case 'O0': options.optimization = 0; break; |
---|
| 82 | case 'O1': options.optimization = 1; break; |
---|
| 83 | case 'O2': options.optimization = 2; break; |
---|
| 84 | case 'line-numbers': |
---|
| 85 | options.dumpLineNumbers = match[2]; |
---|
| 86 | break; |
---|
| 87 | case 'rp': |
---|
| 88 | case 'rootpath': |
---|
| 89 | if (!match[2]) { |
---|
| 90 | sys.puts("rootpath option requires a parameter"); |
---|
| 91 | continueProcessing = false; |
---|
| 92 | } else { |
---|
| 93 | options.rootpath = path.normalize(match[2] + '/').replace('\\', '/'); |
---|
| 94 | } |
---|
| 95 | break; |
---|
| 96 | case "ru": |
---|
| 97 | case "relative-urls": |
---|
| 98 | options.relativeUrls = true; |
---|
| 99 | break; |
---|
| 100 | } |
---|
| 101 | }); |
---|
| 102 | |
---|
| 103 | if (!continueProcessing) { |
---|
| 104 | return; |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | var input = args[1]; |
---|
| 108 | if (input && input != '-') { |
---|
| 109 | input = path.resolve(process.cwd(), input); |
---|
| 110 | } |
---|
| 111 | var output = args[2]; |
---|
| 112 | if (output) { |
---|
| 113 | output = path.resolve(process.cwd(), output); |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | if (! input) { |
---|
| 117 | sys.puts("lessc: no input files"); |
---|
| 118 | sys.puts(""); |
---|
| 119 | require('../lib/less/lessc_helper').printUsage(); |
---|
| 120 | currentErrorcode = 1; |
---|
| 121 | return; |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | var ensureDirectory = function (filepath) { |
---|
| 125 | var dir = path.dirname(filepath), |
---|
| 126 | existsSync = fs.existsSync || path.existsSync; |
---|
| 127 | if (!existsSync(dir)) { |
---|
| 128 | fs.mkdirSync(dir); |
---|
| 129 | } |
---|
| 130 | }; |
---|
| 131 | |
---|
| 132 | var parseLessFile = function (e, data) { |
---|
| 133 | if (e) { |
---|
| 134 | sys.puts("lessc: " + e.message); |
---|
| 135 | currentErrorcode = 1; |
---|
| 136 | return; |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | new(less.Parser)({ |
---|
| 140 | paths: [path.dirname(input)].concat(options.paths), |
---|
| 141 | optimization: options.optimization, |
---|
| 142 | filename: input, |
---|
| 143 | rootpath: options.rootpath, |
---|
| 144 | relativeUrls: options.relativeUrls, |
---|
| 145 | strictImports: options.strictImports, |
---|
| 146 | dumpLineNumbers: options.dumpLineNumbers |
---|
| 147 | }).parse(data, function (err, tree) { |
---|
| 148 | if (err) { |
---|
| 149 | less.writeError(err, options); |
---|
| 150 | currentErrorcode = 1; |
---|
| 151 | return; |
---|
| 152 | } else { |
---|
| 153 | try { |
---|
| 154 | var css = tree.toCSS({ |
---|
| 155 | compress: options.compress, |
---|
| 156 | yuicompress: options.yuicompress |
---|
| 157 | }); |
---|
| 158 | if (output) { |
---|
| 159 | ensureDirectory(output); |
---|
| 160 | fs.writeFileSync(output, css, 'utf8'); |
---|
| 161 | if (options.verbose) { |
---|
| 162 | console.log('lessc: wrote ' + output); |
---|
| 163 | } |
---|
| 164 | } else { |
---|
| 165 | sys.print(css); |
---|
| 166 | } |
---|
| 167 | } catch (e) { |
---|
| 168 | less.writeError(e, options); |
---|
| 169 | currentErrorcode = 2; |
---|
| 170 | return; |
---|
| 171 | } |
---|
| 172 | } |
---|
| 173 | }); |
---|
| 174 | }; |
---|
| 175 | |
---|
| 176 | if (input != '-') { |
---|
| 177 | fs.readFile(input, 'utf8', parseLessFile); |
---|
| 178 | } else { |
---|
| 179 | process.stdin.resume(); |
---|
| 180 | process.stdin.setEncoding('utf8'); |
---|
| 181 | |
---|
| 182 | var buffer = ''; |
---|
| 183 | process.stdin.on('data', function(data) { |
---|
| 184 | buffer += data; |
---|
| 185 | }); |
---|
| 186 | |
---|
| 187 | process.stdin.on('end', function() { |
---|
| 188 | parseLessFile(false, buffer); |
---|
| 189 | }); |
---|
| 190 | } |
---|