From 043832684a7e9d672fb6a0ace7111965d92f359b Mon Sep 17 00:00:00 2001 From: Oleksii Okhrymenko Date: Mon, 12 Jun 2017 04:04:18 +0500 Subject: [PATCH] Ability to ignore files --- lib/cli.js | 9 +- package.json | 1 + src/cli.ts | 14 +- .../cli_output/test.igore.ts.color.txt | 137 ++++++++++++++++++ test/spec/system.coffee | 11 ++ 5 files changed, 168 insertions(+), 4 deletions(-) create mode 100644 test/fixtures/cli_output/test.igore.ts.color.txt diff --git a/lib/cli.js b/lib/cli.js index 8060068..6fbc09b 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -1,13 +1,16 @@ "use strict"; var program = require("commander"); var similar = require("./similar"); +var ignore = require("ignore"); var fs_collector = require("./cli/file_collector"); var pkg = require("./../package.json"); var compare = function (targets, opts) { var files = fs_collector.files(targets); var nocolors = !!opts.disablecolors; - fs_collector.print(files, nocolors); - var _a = similar.compare(files, opts), js = _a.js, ts = _a.ts; + var filter = ignore(); + var filtered_files = filter.add(opts.ignore).filter(files); + fs_collector.print(filtered_files, nocolors); + var _a = similar.compare(filtered_files, opts), js = _a.js, ts = _a.ts; similar.print(js, nocolors); similar.print(ts, nocolors); }; @@ -24,6 +27,8 @@ var configure = function () { ("[default=" + similar.DEFAULT_NGRAM_LENGTH + ",2,3...]")) .option("-d, --disablecolors", "Disable color output") .option("--estype [value]", "Set the JavaScript parser source type [default=module,script]") + .option("-i, --ignore ", "Set glob for files that will be ignored. WARNING: You should " + + "wrap glob in quote, otherwise you may get wrong result") .action(compare); program.on("--help", function () { console.log(" Command specific help:"); diff --git a/package.json b/package.json index dc8c1db..2eae772 100644 --- a/package.json +++ b/package.json @@ -67,6 +67,7 @@ "escodegen": "^1.8.1", "esprima": "^4.0.0", "estraverse": "^4.2.0", + "ignore": "^3.3.3", "lodash": "^4.17.4", "typescript": "^2.3.4", "walk-sync": "^0.3.2" diff --git a/src/cli.ts b/src/cli.ts index 0ad37e8..06ceac2 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -1,5 +1,6 @@ import program = require("commander") import similar = require("./similar") +import ignore = require("ignore"); import fs_collector = require("./cli/file_collector") const pkg = require("./../package.json") @@ -10,10 +11,14 @@ const compare = ( ) => { const files = fs_collector.files(targets) const nocolors : boolean = !!opts.disablecolors + const filter = ignore(); + // we need to cast result of ignore.filter to any + // because of typings of ignore package + const filtered_files = filter.add(opts.ignore).filter(files) as any; - fs_collector.print(files, nocolors) + fs_collector.print(filtered_files, nocolors) - const { js, ts } = similar.compare(files, opts) + const { js, ts } = similar.compare(filtered_files, opts) similar.print(js, nocolors) similar.print(ts, nocolors) @@ -40,6 +45,11 @@ const configure = () => { .option( "--estype [value]", "Set the JavaScript parser source type [default=module,script]") + .option( + "-i, --ignore ", + "Set glob for files that will be ignored. WARNING: You should " + + "wrap glob in quote, otherwise you may get wrong result" + ) .action(compare) program.on("--help", () => { diff --git a/test/fixtures/cli_output/test.igore.ts.color.txt b/test/fixtures/cli_output/test.igore.ts.color.txt new file mode 100644 index 0000000..87d2fe0 --- /dev/null +++ b/test/fixtures/cli_output/test.igore.ts.color.txt @@ -0,0 +1,137 @@ +found: test/fixtures/system/test-es.js +found: test/fixtures/system/test.copy.js +found: test/fixtures/system/test.js + +76% match + +in: test/fixtures/system/test-es.js + + 9: function diag(x, y) { +10: return sqrt(square(x) + square(y)); +11: } + +13: function dude(x, y) { +14: return sqrt(square(x ^ 2) + square(y)); +15: } + +83% match + +in: test/fixtures/system/test.js + +63: function (a, b) { +64: const dx = a.x - b.x; +65: const dy = a.y - b.y; +66: return Math.sqrt(dx * dx + dy * dy); +67: } + +70: function (an, ab) { +71: const dx = an.x - ab.x; +72: const dy = an.y - ab.y; +73: return Math.sqrt(dx * dx + dy * dy); +74: } + +89% match + +in: test/fixtures/system/test.js + +27: class FooBazzzz { +28: constructor() { +29: } +30: static get area() { +31: console.log('a'); +32: console.log('b'); +33: console.log('c'); +34: console.log('c'); +35: console.log('c'); +36: } +37: } + +40: class FooBarrr { +41: constructor() { +42: } +43: static get area() { +44: console.log('a'); +45: console.log('b'); +46: console.log('c'); +47: console.log('c'); +48: console.log('c'); +49: } +50: } + +93% match + +in: test/fixtures/system/test.copy.js + +1: (a, b) => { +2: console.log(); +3: if (!a) { +4: console.log(a); +5: } +6: } + +in: test/fixtures/system/test.js + +1: (a, b) => { +2: console.log(b); +3: if (a) { +4: console.log(a); +5: } +6: } + +93% match + +in: test/fixtures/system/test.js + +1: (a, b) => { +2: console.log(b); +3: if (a) { +4: console.log(a); +5: } +6: } + + 9: (a, b) => { +10: console.log(); +11: if (!a) { +12: console.log(a); +13: } +14: } + +100% match + +in: test/fixtures/system/test.copy.js + +1: (a, b) => { +2: console.log(); +3: if (!a) { +4: console.log(a); +5: } +6: } + +in: test/fixtures/system/test.js + + 9: (a, b) => { +10: console.log(); +11: if (!a) { +12: console.log(a); +13: } +14: } + +100% match + +in: test/fixtures/system/test.js + +31: function () { +32: console.log('a'); +33: console.log('b'); +34: console.log('c'); +35: console.log('c'); +36: console.log('c'); +37: } + +44: function () { +45: console.log('a'); +46: console.log('b'); +47: console.log('c'); +48: console.log('c'); +49: console.log('c'); +50: } diff --git a/test/spec/system.coffee b/test/spec/system.coffee index a55b2b2..d76323c 100644 --- a/test/spec/system.coffee +++ b/test/spec/system.coffee @@ -33,6 +33,8 @@ CLI_OUTPUT_TEST_JS_TS_DIR = path .join FIXTURES, "cli_output", "test.dir.txt" CLI_OUTPUT_TEST_JS_TS_DIR_COLOR = path .join FIXTURES, "cli_output", "test.dir.color.txt" +CLI_OUTPUT_TEST_IGNORE_TS_COLOR = path + .join FIXTURES, "cli_output", "test.igore.ts.color.txt" cli_output = (path) -> if on_win @@ -183,3 +185,12 @@ describe "system :: cli", -> expect(error.code).to.eql 0 expect(stdout).to.eql(cli_output CLI_OUTPUT_TEST_JS_TS_DIR) done() + + it "can ignore ts files", (done) -> + cmd = "analyze -i *.ts #{SYSTEM}" + + system.exec cmd, + (error, stdout, stderr) -> + expect(error.code).to.eql 0 + expect(stdout).to.eql(cli_output CLI_OUTPUT_TEST_IGNORE_TS_COLOR) + done()