diff --git a/README.md b/README.md index 988e21f4..ade24844 100644 --- a/README.md +++ b/README.md @@ -45,18 +45,29 @@ Usage : plato [options] -d 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 diff --git a/lib/cli.js b/lib/cli.js index 9986f5e6..cdc67ef7 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -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 = {}; @@ -62,7 +63,7 @@ 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); }); @@ -70,10 +71,14 @@ function parseArgs(options) {// \/\\*(?:(?!\\*\/)|.|\\n)*?\\*\/ 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; diff --git a/lib/cli/options.json b/lib/cli/options.json index 787650b2..eab70622 100644 --- a/lib/cli/options.json +++ b/lib/cli/options.json @@ -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" } } diff --git a/lib/info.js b/lib/info.js index edc1478c..790852bb 100644 --- a/lib/info.js +++ b/lib/info.js @@ -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); }); diff --git a/lib/plato.js b/lib/plato.js index ce6974a7..03988cc5 100644 --- a/lib/plato.js +++ b/lib/plato.js @@ -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, @@ -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, ''); diff --git a/test/fixtures/c-es6 b/test/fixtures/c-es6 new file mode 100644 index 00000000..f63d53ce --- /dev/null +++ b/test/fixtures/c-es6 @@ -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; diff --git a/test/fixtures/c-es6.es b/test/fixtures/c-es6.es new file mode 100644 index 00000000..f63d53ce --- /dev/null +++ b/test/fixtures/c-es6.es @@ -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; diff --git a/test/plato_test.js b/test/plato_test.js index 2a4769cb..f3764837 100644 --- a/test/plato_test.js +++ b/test/plato_test.js @@ -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(); + }); } };