Skip to content

Commit

Permalink
fix(blamer): Fix usage fix of blamer
Browse files Browse the repository at this point in the history
fix #238
  • Loading branch information
kucherenko committed Mar 10, 2020
1 parent 5605530 commit 5f9e125
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 17 deletions.
14 changes: 6 additions & 8 deletions src/hooks/post/blamer.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import Blamer from 'blamer';
import { yellow } from 'colors/safe';
import { IClone } from '../..';
import { IBlamedLines } from '../../interfaces/blame.interface';
import { IHook } from '../../interfaces/hook.interface';
import { JSCPD } from '../../jscpd';
import { IPostHook } from '../../interfaces/post-hook.interface';

const Blamer = require('blamer');

export class BlamerPostHook implements IHook {
public async use(jscpd: JSCPD): Promise<any> {
jscpd.clones = await Promise.all(
jscpd.clones.map((clone: IClone) => {
export class BlamerPostHook implements IPostHook {
public async use(clones: IClone[]): Promise<any> {
return await Promise.all(
clones.map((clone: IClone) => {
return this.matchClone(clone);
})
);
Expand Down
5 changes: 5 additions & 0 deletions src/interfaces/post-hook.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { IClone } from './clone.interface';

export interface IPostHook {
use(closes: IClone[]): Promise<any>;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { JSCPD } from '../jscpd';

export interface IHook {
export interface IPreHook {
use(jscpd: JSCPD): Promise<any>;
}
20 changes: 12 additions & 8 deletions src/jscpd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import { getSourceFragmentLength } from './clone';
import { Detector } from './detector';
import { END_EVENT, JscpdEventEmitter, MATCH_SOURCE_EVENT, SOURCE_SKIPPED_EVENT } from './events';
import { IClone } from './interfaces/clone.interface';
import { IHook } from './interfaces/hook.interface';
import { IListener } from './interfaces/listener.interface';
import { IOptions } from './interfaces/options.interface';
import { IPostHook } from './interfaces/post-hook.interface';
import { IPreHook } from './interfaces/pre-hook.interface';
import { IReporter } from './interfaces/reporter.interface';
import { ISourceOptions } from './interfaces/source-options.interface';
import { IStatistic } from './interfaces/statistic.interface';
Expand Down Expand Up @@ -72,8 +73,8 @@ export class JSCPD {
private _options: IOptions;
private _files: EntryItem[] = [];
private _clones: IClone[] = [];
private _preHooks: IHook[] = [];
private _postHooks: IHook[] = [];
private _preHooks: IPreHook[] = [];
private _postHooks: IPostHook[] = [];

constructor(options: IOptions = {} as IOptions, eventEmitter?: EventEmitter) {
this.eventEmitter = eventEmitter || new JscpdEventEmitter();
Expand All @@ -84,16 +85,16 @@ export class JSCPD {
StoresManager.initialize(this._options.storeOptions);
}

public attachPreHook(hook: IHook) {
public attachPreHook(hook: IPreHook) {
this._preHooks.push(hook);
}

public attachPostHook(hook: IHook) {
public attachPostHook(hook: IPostHook) {
this._postHooks.push(hook);
}

public async detect(code: string, options: ISourceOptions): Promise<IClone[]> {
await Promise.all(this._preHooks.map((hook: IHook) => hook.use(this)));
await Promise.all(this._preHooks.map((hook: IPreHook) => hook.use(this)));
this._clones = await this._detect(code, { ...options, source: code });
await this._detectionFinished(this._clones, true);
return this._clones;
Expand Down Expand Up @@ -138,7 +139,7 @@ export class JSCPD {
console.log(bold(`Found ${this._files.length} files to detect.`));
}

await Promise.all(this._preHooks.map((hook: IHook) => hook.use(this)));
await Promise.all(this._preHooks.map((hook: IPreHook) => hook.use(this)));

const sources: Array<{ source: string; sourceOptions: ISourceOptions }> = this._files
.filter((stats: any) => {
Expand Down Expand Up @@ -239,7 +240,10 @@ export class JSCPD {
}

private async _detectionFinished(clones: IClone[], pesists: boolean = false) {
await Promise.all(this._postHooks.map((hook: IHook) => hook.use(this)));
await Promise.all(this._postHooks.map(async (hook: IPostHook) => {
clones = await hook.use(clones);
return clones;
}));
await this.generateReports(clones);
this.eventEmitter.emit(END_EVENT, clones);
if (!pesists) {
Expand Down

0 comments on commit 5f9e125

Please sign in to comment.