Skip to content

Commit

Permalink
Added file extensions option
Browse files Browse the repository at this point in the history
  • Loading branch information
kyungilpark committed Jun 29, 2019
1 parent e103408 commit 597c9a6
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 11 deletions.
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,29 @@ Usage : plato [options] -d <output_dir> <input files>
Time to use as the report date (seconds, > 9999999999 assumed to be ms)
-n, --noempty
Skips empty lines from line count
-X, --extensions : Array (default: .js)
Specify JavaScript file extensions
```

__Example__

```shell
plato -r -d report src
```console
$ plato -r -d report src
```

__Extended example__

```console
$ plato -r -d report -l .jshintrc -t "My Awesome App" -x .json routes/*.js
```
plato -r -d report -l .jshintrc -t "My Awesome App" -x .json routes/*.js

__Extended example (Specify JavaScript file extensions)__

```console
$ plato -r -d report -X .jsx src
$ plato -r -d report --extensions=.jsx src
$ plato -r -d report -X .js,.jsx src
$ plato -r -d report -X .js -X .jsx src
```

### From scripts
Expand Down
15 changes: 10 additions & 5 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ exports.exec = function(options, done) {
title : exports.args.t && exports.args.t.value,
exclude : exports.args.x && new RegExp(exports.args.x.value),
date : exports.args.D && exports.args.D.value,
eslint: exports.args.e && exports.args.e.value
eslint: exports.args.e && exports.args.e.value,
extensions: exports.args.X && exports.args.X.value
};
var json;
var jshintrc = {};
Expand Down Expand Up @@ -62,18 +63,22 @@ function parseArgs(options) {// \/\\*(?:(?!\\*\/)|.|\\n)*?\\*\/
Object.keys(options).forEach(function(option){
var def = options[option];
optionString += option;
if (def.type === 'String') optionString += ':';
if (def.type === 'String' || def.type === "Array") optionString += ':';
if (def.long) optionString += '(' + def.long + ')';
if (def.required) required.push(option);
});

var parser = new getopt.BasicParser(optionString, process.argv);
var args = {}, option;

while ((option = parser.getopt())) {
var arg = args[option.option] || { count : 0};
while ((option = parser.getopt()) !== undefined && !option.error) {
var arg = args[option.option] || { count : 0, value : [] };
arg.count++;
arg.value = option.optarg || true;
if (options[option.option].type === "Array") {
arg.value = arg.value.concat(option.optarg.split(","));
} else {
arg.value = option.optarg || true;
}

args[option.option] = arg;

Expand Down
6 changes: 6 additions & 0 deletions lib/cli/options.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,11 @@
"long": "noempty",
"desc": "Skips empty lines from line count",
"type": "Boolean"
},
"X": {
"long": "extensions",
"desc": "Specify JavaScript file extensions",
"type": "Array",
"default": ".js"
}
}
5 changes: 3 additions & 2 deletions lib/info.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ exports.help = function() {
Object.keys(options).forEach(function(shortOption){
var option = options[shortOption];
console.log(
' -%s%s%s%s',
' -%s%s%s%s%s',
shortOption,
option.long ? ', --' + option.long : '',
option.type !== 'Boolean' ? ' : ' + option.type : '',
option.required ? ' *required*' : ''
option.required ? ' *required*' : '',
option.default ? ' (default: ' + option.default + ')' : ''
);
console.log(' %s', option.desc);
});
Expand Down
4 changes: 3 additions & 1 deletion lib/plato.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ exports.inspect = function(files, outputDir, options, done) {
files = files instanceof Array ? files : [files];
files = _.flatten(files.map(unary(glob.sync)));

options.extensions = options.extensions || [".js"];

var flags = {
complexity : {
commonjs : true,
Expand Down Expand Up @@ -109,7 +111,7 @@ exports.inspect = function(files, outputDir, options, done) {
return path.join(file,innerFile);
});
runReports(files);
} else if (file.match(/\.js$/)) {
} else if (options.extensions.includes(path.extname(file))) {
log.info('Reading "%s"', file);

var fileShort = file.replace(commonBasePath, '');
Expand Down
21 changes: 21 additions & 0 deletions test/fixtures/c-es6
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

const a = 1;

class C
{
static classMethodA(arg) {
return arg;
}

static classMethodB(arg) {
return arg;
}
}

function moduleMethod(arg) {
return arg;
}

const b = moduleMethod(0) + C.classMethodB(1) + C.classMethodA(2);

export default C;
21 changes: 21 additions & 0 deletions test/fixtures/c-es6.es
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

const a = 1;

class C
{
static classMethodA(arg) {
return arg;
}

static classMethodB(arg) {
return arg;
}
}

function moduleMethod(arg) {
return arg;
}

const b = moduleMethod(0) + C.classMethodB(1) + C.classMethodA(2);

export default C;
72 changes: 72 additions & 0 deletions test/plato_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,5 +179,77 @@ exports['plato'] = {
test.ok(overview.summary.total.jshint === 8, 'Should contain total eslint issues');
test.done();
});
},

'test extensions option that default value is .js' : function(test) {
var options = {
eslint: 'test/fixtures/.eslintrc.json'
};
var files = [
'test/fixtures/c-es6.js'
];

test.expect(1);

plato.inspect(files, null, options, function(reports) {
var overview = plato.getOverviewReport(reports);
test.ok(overview.summary.total.jshint === 1, 'Should contain total eslint issues');
test.done();
});
},

'test extensions option with .es' : function(test) {
var options = {
extensions: [".es"],
eslint: 'test/fixtures/.eslintrc.json'
};
var files = [
'test/fixtures/c-es6.es'
];

test.expect(1);

plato.inspect(files, null, options, function(reports) {
var overview = plato.getOverviewReport(reports);
test.ok(overview.summary.total.jshint === 1, 'Should contain total eslint issues');
test.done();
});
},

'test extensions option with empty string' : function(test) {
var options = {
extensions: [""],
eslint: 'test/fixtures/.eslintrc.json'
};
var files = [
'test/fixtures/c-es6'
];

test.expect(1);

plato.inspect(files, null, options, function(reports) {
var overview = plato.getOverviewReport(reports);
test.ok(overview.summary.total.jshint === 1, 'Should contain total eslint issues');
test.done();
});
},

'test extensions option with multi values' : function(test) {
var options = {
extensions: [".es", ".js"],
eslint: 'test/fixtures/.eslintrc.json'
};
var files = [
'test/fixtures/c-es6.es',
'test/fixtures/c-es6.js'
];

test.expect(1);

plato.inspect(files, null, options, function(reports) {
var overview = plato.getOverviewReport(reports);
test.ok(overview.summary.total.jshint === 2, 'Should contain total eslint issues');
test.done();
});
}
};

0 comments on commit 597c9a6

Please sign in to comment.