Skip to content

Commit

Permalink
feat(reporters): Add silent reporter
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrii Kucherenko committed Aug 17, 2018
1 parent c61bc6a commit 517d2ee
Show file tree
Hide file tree
Showing 13 changed files with 82 additions and 32 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ node_modules
build
report
test
.idea
src/**.js

coverage
Expand Down
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ coverage
.nyc_output
*.log
.jscpd
.idea
2 changes: 1 addition & 1 deletion bin/jscpd
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env node

var path = require('path');
require(path.resolve(__dirname, "../build/main/cli"));
require(path.resolve(__dirname, "../build/cli"));
10 changes: 10 additions & 0 deletions logo.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 4 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@
"name": "jscpd",
"version": "1.0.0-alpha.1",
"description": "Copy/paste detector for programming code, support JavaScript, CoffeeScript, PHP, Ruby, Python, Less, Go, Java, Yaml, C#, C++, C, Puppet, Twig languages",
"main": "build/main/index.js",
"typings": "build/main/index.d.ts",
"module": "build/module/index.js",
"main": "build/index.js",
"typings": "build/index.d.ts",
"license": "MIT",
"scripts": {
"info": "npm-scripts-info",
"build": "run-s clean && run-p build:*",
"build:main": "tsc -p tsconfig.json",
"build:module": "tsc -p tsconfig.module.json",
"fix": "run-s fix:*",
"fix:prettier": "prettier \"src/**/*.ts\" --write",
"fix:tslint": "tslint --fix --project .",
Expand Down Expand Up @@ -83,10 +81,10 @@
"ava": {
"failFast": true,
"files": [
"build/main/**/*.spec.js"
"build/**/*.spec.js"
],
"sources": [
"build/main/**/*.js"
"build/**/*.js"
]
},
"config": {
Expand Down
6 changes: 3 additions & 3 deletions src/__tests__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { JSCPD } from '..';
import { IClone } from '../interfaces/clone.interface';
import { getDefaultOptions } from '../utils';

const path: string = normalize(__dirname + '/../../../tests/fixtures/');
const path: string = normalize(__dirname + '/../../tests/fixtures/');

let log: any;

Expand All @@ -24,15 +24,15 @@ test('should detect clones by source', async (t: ExecutionContext) => {

const clones: IClone[] = await cpd.detectBySource({
source: readFileSync(
__dirname + '/../../../tests/fixtures/markup.html'
__dirname + '/../../tests/fixtures/markup.html'
).toString(),
id: '123',
format: 'markup'
});
t.is(clones.length, 0);
const clonesNew: IClone[] = await cpd.detectBySource({
source: readFileSync(
__dirname + '/../../../tests/fixtures/markup.html'
__dirname + '/../../tests/fixtures/markup.html'
).toString(),
id: '1233',
format: 'markup'
Expand Down
22 changes: 15 additions & 7 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { bold, white } from 'colors/safe';
import { Command } from 'commander';
import { getSupportedFormats } from './formats';
import { IOptions } from './interfaces/options.interface';
import { JSCPD } from './jscpd';
import { prepareOptions } from './utils';
Expand Down Expand Up @@ -59,11 +61,17 @@ cli.parse(process.argv);

const options: IOptions = prepareOptions(cli);

const cpd: JSCPD = new JSCPD({
...options,
storeOptions: {
'*': { type: 'files' }
}
});
if (cli.list) {
console.log(bold(white("Supported formats: ")));
console.log(getSupportedFormats().join(', '));
} else {
const cpd: JSCPD = new JSCPD({
...options,
storeOptions: {
'*': { type: 'files' }
}
});

cpd.detectInFiles(options.path);
}

cpd.detectInFiles(options.path);
5 changes: 5 additions & 0 deletions src/reporters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ConsoleFullReporter } from './consoleFull';
import { JsonReporter } from './json';
import { StatisticReporter } from './statistic';
import { TimeReporter } from './time';
import { SilentReporter } from './silent';

const REPORTERS: { [key: string]: IReporter } = {};

Expand Down Expand Up @@ -37,4 +38,8 @@ export function registerReportersByName(options: IOptions) {
if (reporter.includes('stat')) {
registerReporter('stat', new StatisticReporter(options));
}

if (reporter.includes('silent')) {
registerReporter('silent', new SilentReporter(options));
}
}
30 changes: 30 additions & 0 deletions src/reporters/silent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { bold } from 'colors/safe';
import { END_EVENT, Events } from '../events';
import { IOptions } from '../interfaces/options.interface';
import { IReporter } from '../interfaces/reporter.interface';
import { STATISTIC_DB } from '../stores/models';
import { StoresManager } from '../stores/stores-manager';

export class SilentReporter implements IReporter {
constructor(private options: IOptions) {
}

public attach(): void {
Events.on(END_EVENT, this.finish.bind(this));
}


private finish() {
const statistic = StoresManager.getStore(STATISTIC_DB).get(
this.options.executionId
);
if (statistic) {
console.log(
`Duplications detection: Found ${bold(statistic.all.clones)} `+
`exact clones with ${bold(statistic.all.duplicatedLines)}(${statistic.all.percentage}%) `+
`duplicated lines in ${bold(statistic.all.sources)} ` +
`(${Object.keys(statistic.formats).length} formats) files.`
);
}
}
}
2 changes: 1 addition & 1 deletion src/tokenizer/__tests__/prism.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { IToken } from '../../interfaces/token/token.interface';
import { tokenize } from '../prism';

const file: string = normalize(
__dirname + '/../../../../tests/fixtures/markup.html'
__dirname + '/../../../tests/fixtures/markup.html'
);
let code: string;

Expand Down
12 changes: 10 additions & 2 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,19 @@ export function prepareOptions(cli: Command): IOptions {
}
}

return {
const result: IOptions = {
...{ config },
...getDefaultOptions(),
...storedConfig,
...argsConfig
};

if (result.silent) {
result.reporter = ['silent'];
}
result.reporter = ['stat', ...(result.reporter || []), 'time'];
result.reporter = [...new Set(result.reporter)];
return result;
}

export function getDefaultOptions(): IOptions {
Expand All @@ -81,9 +88,10 @@ export function getDefaultOptions(): IOptions {
minLines: 5,
minTokens: 50,
output: './report',
reporter: ['stat', 'time', 'console'],
reporter: ['console'],
ignore: [],
mode: 'mild',
threshold: 0,
format: [...getSupportedFormats()],
debug: false,
silent: false,
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"compilerOptions": {
"target": "es2017",
"outDir": "build/main",
"outDir": "build",
"rootDir": "src",
"moduleResolution": "node",
"module": "commonjs",
Expand Down
11 changes: 0 additions & 11 deletions tsconfig.module.json

This file was deleted.

0 comments on commit 517d2ee

Please sign in to comment.