Skip to content

Commit

Permalink
feat(cli-engine): cli-engine accept config object.
Browse files Browse the repository at this point in the history
  • Loading branch information
azu committed Jan 12, 2015
1 parent e6f1fac commit e9e914a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
8 changes: 6 additions & 2 deletions lib/cli-engine.js
Expand Up @@ -11,11 +11,15 @@ var debug = require("debug")("text:cli-engine");
* Process files are wanted to lint.
* CLIEngine is wrapper of textlint.js.
* Aim to be called from cli with cli options.
* @param {object} options - cli option object
* @param {object|Config} options the options is command line options or Config object.
* @constructor
*/
function CLIEngine(options) {
this.config = new Config(options);
if (options instanceof Config) {
this.config = options;
} else {
this.config = new Config(options);
}
}
/**
* filter files by config
Expand Down
5 changes: 4 additions & 1 deletion lib/config/config.js
Expand Up @@ -16,10 +16,13 @@ var defaultOptions = {
/**
* Create config object form command line options
* See options.js
* @param options the options is command line option object. @see options.js
* @param {object} options the options is command line option object. @see options.js
* @constructor
*/
function Config(options) {
if (typeof options !== "object") {
return this;
}
this.extensions = options.ext ? options.ext : defaultOptions.extensions;
this.rulePaths = options.rulesdir ? options.rulesdir : defaultOptions.rulePaths;
this.formatName = options.format ? options.format : defaultOptions.formatName;
Expand Down
20 changes: 20 additions & 0 deletions test/cli-engine-test.js
Expand Up @@ -6,6 +6,26 @@ var rulesDir = __dirname + "/fixtures/rules";
var path = require("path");
describe("cli-engine-test", function () {
var cliEngine;
describe("Constructor", function () {
context("when args is object", function () {
it("should convert the object and set config", function () {
cliEngine = new CLIEngine({
rulesdir: [rulesDir]
});
assert.deepEqual(cliEngine.config.rulePaths, [rulesDir]);
});
});
context("when args is Config object", function () {
it("should set directory to config", function () {
// Issue : when use Config as argus, have to export `../lib/config/config`
var Config = require("../lib/config/config");
var config = new Config();
config.rulePaths = [rulesDir];
cliEngine = new CLIEngine(config);
assert.deepEqual(cliEngine.config.rulePaths, [rulesDir]);
});
});
});
describe("executeOnFiles", function () {
beforeEach(function () {
cliEngine = new CLIEngine({
Expand Down

0 comments on commit e9e914a

Please sign in to comment.