From ca0e5ebfd7d8385ffb583677b5b572827f3c7cf7 Mon Sep 17 00:00:00 2001 From: Karthik Kumar Viswanathan Date: Thu, 19 Mar 2015 19:05:53 +0530 Subject: [PATCH 1/2] Borrow CLI from html2jade. Fix NPE. --- cli.js | 123 +++++++++++++++++++++++++++++++++++++++++++++++++++ index.js | 4 +- package.json | 7 +++ 3 files changed, 132 insertions(+), 2 deletions(-) create mode 100644 cli.js diff --git a/cli.js b/cli.js new file mode 100644 index 0000000..3efaa71 --- /dev/null +++ b/cli.js @@ -0,0 +1,123 @@ +#!/usr/bin/env node + +var fs = require('fs'); +var path = require('path'); +var url = require('url'); + +var existsSync = fs.existsSync || path.existsSync; + +var css2less; +try { + css2less = require('css2less'); +} catch(err) { + css2less = require('./index'); +} + +var html2jade; +try { + html2jade = require('html2jade'); +} catch(err) { + html2jade = require('./lib/html2jade'); +} + +function parsePath(arg) { + if (typeof arg !== 'string') { + console.error('invalid input: ' + arg); + } else if (path.resolve('/',arg) === arg) { + // already absolute path + return arg; + } else if (arg.length >= 2 && arg.substring(0, 2) === '~/') { + // home path + return path.join(process.env['HOME'], arg.substring(2)); + } else { + // relative to current path + return path.join(process.cwd(), arg); + } +} + +function convert(input, output, options) { + if (input) { + try { + if(options.inputType === "file") + input = fs.readFileSync(arg, "utf8"); + var result = css2less(input, options); + if (!result) { + console.error('parser errors: ' + errors); + } + output.write(result); + } catch (err) { + console.error(err); + throw err; + } + } else { + console.error('invalid input: ' + input); + } +} + +var program = require('commander'); +var version = require('./package').version; + +program + .version(version) + +program.parse(process.argv); + +// if outdir is provided, check existance (sorry no mkdir support yet) +if (program.outdir && !existsSync(program.outdir)) { + console.error("output directory '" + program.outdir + "' doesn't exist"); + process.exit(1); +} + +// process each arguments +var args = program.args; +if (!args || args.length === 0) { + args = ['-']; + // console.error("input argument(s) missing"); + // process.exit(1); +} + +for (var i = 0; i < args.length; i++) { + var arg = args[i], inputUrl, inputPath; + + // handle stdin to stdout + if (arg === '-') { + var input = ''; + process.stdin.resume(); + process.stdin.on('data', function (chunk){ + input += chunk; + }); + process.stdin.on('end', function (){ + program.inputType = "css"; + convert(input, undefined, program); + }); + continue; + } + + if (typeof arg === 'string' && !existsSync(arg)) { + try { inputUrl = url.parse(arg); } catch (err) {} + } + if (inputUrl && inputUrl.protocol) { + // URL input, use stdout + program.inputType = "url"; + convert(arg, undefined, program); + } else { + // path or glob + inputPath = parsePath(arg); + if (existsSync(inputPath)) { + var inputStats = fs.statSync(inputPath); + if (inputStats.isFile()) { + var outdir = program.outdir || path.dirname(arg); + var outputPath = path.join(outdir, path.basename(inputPath, path.extname(inputPath)) + '.less'); + // console.log("converting '" + arg + "' to '" + outputPath + "'"); + var outputStream = fs.createWriteStream(outputPath, { + flags: 'w', + encoding: 'utf8', + }); + program.inputType = "file"; + convert(inputPath, new html2jade.StreamOutput(outputStream), program); + } + } else { + console.error("input file doesn't exist: " + arg); + } + } +} diff --git a/index.js b/index.js index 2a756e8..650d816 100644 --- a/index.js +++ b/index.js @@ -188,7 +188,7 @@ var css2less = function (css, options) { if ( isBase64(e) ) parts = [parts[0], parts[1] + parts[2]]; var key = parts[0].trim(); - var value = parts[1].trim(); + var value = (parts[1])? parts[1].trim() : false ; if (!value) { normal_rules[key] = ""; @@ -473,4 +473,4 @@ module.exports = function(cssString, options) { lessInst.processLess(); return lessInst.less; -}; \ No newline at end of file +}; diff --git a/package.json b/package.json index 888edba..673f4fa 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,13 @@ "url": "https://github.com/serheyShmyg/css2less/issues" }, "main": "index.js", + "dependencies": { + "html2jade": "*", + "commander": "*" + }, + "bin": { + "css2less": "./cli.js" + }, "scripts": { "start" : "gulp", "test": "echo \"Error: no test specified\" && exit 1" From dc11ba3684d352fbfe48475a8ef2848391ea08aa Mon Sep 17 00:00:00 2001 From: Karthik Kumar Viswanathan Date: Thu, 19 Mar 2015 19:20:58 +0530 Subject: [PATCH 2/2] Clean up Comments. --- index.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/index.js b/index.js index 650d816..1b75b2b 100644 --- a/index.js +++ b/index.js @@ -387,6 +387,9 @@ var css2less = function (css, options) { var index = 0; for (var i in tree) { + if (i == "/**") { + continue; + } if (i == "children") { continue; }