sort out usage and allow a plugin argument

This commit is contained in:
Luke Page
2014-09-17 21:14:40 +01:00
parent c7f8fe54cf
commit 771ab4e430
4 changed files with 46 additions and 17 deletions

View File

@@ -63,6 +63,12 @@ var parseVariableOption = function(option) {
var warningMessages = "";
var sourceMapFileInline = false;
function printUsage() {
require('../lib/less-node/lessc-helper').printUsage();
pluginManager.printUsage();
continueProcessing = false;
}
args = args.filter(function (arg) {
var match;
@@ -98,8 +104,7 @@ args = args.filter(function (arg) {
break;
case 'h':
case 'help':
require('../lib/less-node/lessc-helper').printUsage();
continueProcessing = false;
printUsage();
break;
case 'x':
case 'compress':
@@ -210,12 +215,23 @@ args = args.filter(function (arg) {
options.urlArgs = match[2];
}
break;
case 'plugin':
var splitupArg = match[2].match(/^([^=]+)(=(.*))?/),
name = splitupArg[1],
pluginOptions = splitupArg[3];
if (!pluginManager.tryLoadPlugin(name, pluginOptions)) {
console.log("Unable to load plugin " + name + " please make sure that it is installed under at the same level as less");
console.log();
printUsage();
currentErrorcode = 1;
}
break;
default:
if (!pluginManager.interpretCommandLineArgument(arg, match[2])) {
if (!pluginManager.tryLoadPlugin("less-plugin-" + arg, match[2])) {
console.log("Unable to interpret argument " + arg + " - if it is a plugin (less-plugin-" + arg + "), make sure that it is installed under at the same level as less");
console.log();
require('../lib/less-node/lessc-helper').printUsage();
continueProcessing = false;
printUsage();
currentErrorcode = 1;
}
break;
@@ -254,7 +270,7 @@ if (options.sourceMap === true) {
if (! input) {
console.log("lessc: no input files");
console.log("");
require('../lib/less/lessc_helper').printUsage();
printUsage();
currentErrorcode = 1;
return;
}

View File

@@ -38,11 +38,6 @@ var lessc_helper = {
console.log(" --insecure Allow imports from insecure https hosts.");
console.log(" -v, --version Print version number and exit.");
console.log(" -x, --compress Compress output by removing some whitespaces.");
console.log(" --clean-css Compress output using clean-css");
console.log(" --clean-option=opt:val Pass an option to clean css, using CLI arguments from ");
console.log(" https://github.com/GoalSmashers/clean-css e.g.");
console.log(" --clean-option=--selectors-merge-mode:ie8");
console.log(" and to switch on advanced use --clean-option=--advanced");
console.log(" --source-map[=FILENAME] Outputs a v3 sourcemap to the filename (or output filename.map)");
console.log(" --source-map-rootpath=X adds this path onto the sourcemap filename and less file paths");
console.log(" --source-map-basepath=X Sets sourcemap base path, defaults to current working directory.");
@@ -60,6 +55,12 @@ var lessc_helper = {
console.log(" --global-var='VAR=VALUE' Defines a variable that can be referenced by the file.");
console.log(" --modify-var='VAR=VALUE' Modifies a variable already declared in the file.");
console.log(" --url-args='QUERYSTRING' Adds params into url tokens (e.g. 42, cb=42 or 'a=1&b=2')");
console.log(" --plugin=PLUGIN=OPTIONS Loads a plugin. You can also omit the --plugin= if the plugin begins");
console.log(" less-plugin. E.g. the clean css plugin is called less-plugin-clean-css");
console.log(" once installed (npm install less-plugin-clean-css), use either with");
console.log(" --plugin=less-plugin-clean-css or just --clean-css");
console.log(" specify options afterwards e.g. --plugin=less-plugin-clean-css=\"advanced\"");
console.log(" or --clean-css=\"advanced\"");
console.log("");
console.log("-------------------------- Deprecated ----------------");
console.log(" --line-numbers=TYPE Outputs filename and line numbers.");

View File

@@ -6,7 +6,7 @@ var NodePluginManager = function(less) {
PluginManager.call(this, less);
};
NodePluginManager.prototype = new PluginManager();
NodePluginManager.prototype.interpretCommandLineArgument = function(name, argument) {
NodePluginManager.prototype.tryLoadPlugin = function(name, argument) {
var plugin = this.tryRequirePlugin(name);
if (plugin) {
this.addPlugin(plugin, argument);
@@ -15,15 +15,25 @@ NodePluginManager.prototype.interpretCommandLineArgument = function(name, argume
return false;
};
NodePluginManager.prototype.tryRequirePlugin = function(name) {
try {
return require("less-plugin-"+name);
}
catch(e) {
if (name[0] !== '.') {
try {
return require(name);
}
catch(e) {
}
}
try {
return require("../../../less-plugin-" + name);
return require("../../../" + name);
}
catch(e) {
}
};
NodePluginManager.prototype.printUsage = function() {
for(var i = 0; i < this.installedPlugins.length; i++) {
var plugin = this.installedPlugins[i];
if (plugin.printUsage) {
plugin.printUsage();
}
}
};
module.exports = NodePluginManager;

View File

@@ -5,8 +5,10 @@ var PluginManager = function(less) {
this.less = less;
this.visitors = [];
this.postProcessors = [];
this.installedPlugins = [];
};
PluginManager.prototype.addPlugin = function(plugin, options) {
this.installedPlugins.push(plugin);
plugin.install(this.less, this, options);
};
PluginManager.prototype.addVisitor = function(visitor) {