Skip to content

Commit

Permalink
Revised the command line interface.
Browse files Browse the repository at this point in the history
- Revised the command line interface to now use the 'commander' module.
- Added validation for the selected mode.
- Bumped version to v0.2.7.
  • Loading branch information
lewie9021 committed Dec 21, 2014
1 parent dd378c7 commit d1e8400
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 104 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,20 @@ The configuration object above may seem daunting however, broken down, it's rath
* Emit an error event when an intergration throws when processing a file.
* Modify the info log when the monitor fires a chang event to render a path relative to the root directory.
* Make intergrations optional rather than installing them all.
* Make use of the 'commander' module for the command line interface.
* Save log files in the directory specified in the configuration object's directory property.
* Make a start on unit testing with Mocha and Chai.
* Make a start on unit testing with Mocha, Chai, and Sinon.
* Improve (standardise) error reporting.

### Changelog

<dl>
<dt>v0.2.7</dt>
<dd>
<ul>
<li>Revised the command line interface to now use the 'commander' module.</li>
<li>Added validation for the selected mode.</li>
</ul>
</dd>
<dt>v0.2.6</dt>
<dd>
<ul>
Expand Down
65 changes: 7 additions & 58 deletions src/bin/cli.js
Original file line number Diff line number Diff line change
@@ -1,67 +1,16 @@
#!/usr/bin/env node
var Commander = require("commander");
var Compiler = require("../lib/Compiler");

function parse(arguments, commands) {
var pattern = /^\-\-\w+/;
var command = null;
var params = {};

// Round up all the command pairs into an object.
arguments.forEach(function(argument) {
if (pattern.test(argument)) {
var name = argument.substr(2);
if (command = commands.filter(function(c) { return (c.name == name); })[0]) {
params[name] = ((command.type == "boolean") ? true : command.default);
}
} else {
if (command) {
params[command.name] = argument;
}
}
});

// Check to ensure required paramters have been specified and fill in any default values.
commands.forEach(function(command) {
if (command.required) {
var found = false;
for (var param in params) {
if (command.name == param) {
found = true;
break;
}
}

if (!found) {
throw new Error(command.name + " is a required parameter.");
}
}

params[command.name] = (params[command.name] || command.default);
});

return params;
}

var params = parse(process.argv.slice(2), [
{
name: "config",
required: true,
type: "string"
},
{
name: "mode",
required: true,
type: "string"
},
{
name: "debug",
default: false,
type: "boolean"
}
]);
var params = Commander
.option('-c, --config <path>', 'Configuration path')
.option('-m, --mode <id>', 'Compile mode')
.option('-d, --debug', 'Debug mode')
.parse(process.argv);

try {
var compiler = new Compiler(params.config, params.mode, params.debug);
compiler.on("compiled", function() { console.log("compiled"); });
compiler.compile();
} catch(e) {
console.log(e);
Expand Down
24 changes: 21 additions & 3 deletions src/lib/Compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function Compiler(config, mode, debug) {
config = JSON.parse(FS.readFileSync(config, "utf-8"));
}

var invalid = this.validate(config);
var invalid = this.validateCompiler(config);
if (invalid) { throw new Error(invalid); }

Events.EventEmitter.call(this);
Expand All @@ -73,6 +73,9 @@ Compiler.prototype.init = function _init(config, mode) {
this.mode = config.modes.filter(function(m) { return m.id == mode; })[0];
if (!this.mode) { throw new Error("Failed to find configuration mode."); }

var invalid = this.validateMode(this.mode);
if (invalid) { throw new Error(invalid); }

Logger.debug("Initialising " + this.mode.name + " mode...");

this.name = config.name;
Expand All @@ -87,7 +90,7 @@ Compiler.prototype.init = function _init(config, mode) {
this.profiles = [];
this.mode.profiles.forEach(function(profileID) {
var found = config.profiles.filter(function(p) { return p.id == profileID; })[0];
if (!found) { return Logger.warn("Invalid profile ID specified '" + profileID + "'."); }
if (!found) { return Logger.warn("Failed to find profile with ID '" + profileID + "'."); }

var profile = new Profile(this, found);
this.profiles.push(profile);
Expand All @@ -102,7 +105,7 @@ Compiler.prototype.compile = function _compile() {
this.emit("compiled");
};

Compiler.prototype.validate = new Legitimize({
Compiler.prototype.validateCompiler = new Legitimize({
name: {
type: "string",
error: "'name' property in configuration object is required and must be a string."
Expand All @@ -121,4 +124,19 @@ Compiler.prototype.validate = new Legitimize({
}
}, {required: true});

Compiler.prototype.validateMode = new Legitimize({
id: {
type: "string",
error: "'directory' property in mode is required and must be a valid path."
},
name: {
type: "string",
error: "'name' property in mode is required and must be a string."
},
profiles: {
type: "array",
error: "'profiles' property in mode is required and must be an array."
}
}, {required: true});

module.exports = Compiler;
83 changes: 42 additions & 41 deletions src/package.json
Original file line number Diff line number Diff line change
@@ -1,42 +1,43 @@
{
"name": "node-compiler",
"description": "Single page application compiler that supports Dust, JS, Sass, CoffeeScript, and Sync.",
"keywords": [
"node",
"compiler",
"dustjs",
"sass",
"coffee-script",
"sync",
"web",
"minify",
"concatenate",
"build",
"watch",
"cli"
],
"author": {
"name": "Lewis Barnes"
},
"version": "0.2.6",
"license": "MIT",
"main": "./lib/Compiler.js",
"repository": {
"type" : "git",
"url": "https://github.com/lewie9021/node-compiler"
},
"bin": "./bin/cli.js",
"dependencies": {
"coffee-script": "~1.8.0",
"dustjs-linkedin": "~2.5.1",
"fs-extra": "~0.13.0",
"node-sass": "~1.2.3",
"uglify-js": "~2.4.16",
"chalk": "~0.5.1",
"chokidar": "~0.12.4",
"legitimize": "~0.0.3"
},
"engines": {
"node": "0.10.x"
}
}
"name": "node-compiler",
"description": "Single page application compiler that supports Dust, JS, Sass, CoffeeScript, and Sync.",
"keywords": [
"node",
"compiler",
"dustjs",
"sass",
"coffee-script",
"sync",
"web",
"minify",
"concatenate",
"build",
"watch",
"cli"
],
"author": {
"name": "Lewis Barnes"
},
"version": "0.2.7",
"license": "MIT",
"main": "./lib/Compiler.js",
"repository": {
"type": "git",
"url": "https://github.com/lewie9021/node-compiler"
},
"bin": "./bin/cli.js",
"dependencies": {
"chalk": "~0.5.1",
"chokidar": "~0.12.4",
"coffee-script": "~1.8.0",
"commander": "~2.5.1",
"dustjs-linkedin": "~2.5.1",
"fs-extra": "~0.13.0",
"legitimize": "~0.0.3",
"node-sass": "~1.2.3",
"uglify-js": "~2.4.16"
},
"engines": {
"node": "0.10.x"
}
}

0 comments on commit d1e8400

Please sign in to comment.