-
-
Notifications
You must be signed in to change notification settings - Fork 245
Expand file tree
/
Copy pathin-files-detector.ts
More file actions
115 lines (101 loc) · 3.09 KB
/
Copy pathin-files-detector.ts
File metadata and controls
115 lines (101 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import {
Detector,
DetectorEvents,
IClone,
ICloneValidator,
IHandler,
IMapFrame,
IOptions,
IStore,
ISubscriber,
ITokenizer,
Statistic,
} from '@jscpd/core';
import {getFormatByFile} from '@jscpd/tokenizer';
import {EntryWithContent, IHook, IReporter} from './interfaces';
import {SkipLocalValidator, SkipIsolatedValidator} from './validators';
export class InFilesDetector {
private readonly reporters: IReporter[] = [];
private readonly subscribes: ISubscriber[] = [];
private readonly postHooks: IHook[] = [];
constructor(
private readonly tokenizer: ITokenizer,
private readonly store: IStore<IMapFrame>,
private readonly statistic: Statistic,
public readonly options: IOptions) {
this.registerSubscriber(this.statistic);
}
registerReporter(reporter: IReporter): void {
this.reporters.push(reporter);
}
registerSubscriber(subscriber: ISubscriber): void {
this.subscribes.push(subscriber);
}
registerHook(hook: IHook): void {
this.postHooks.push(hook);
}
detect(fls: EntryWithContent[]): Promise<IClone[]> {
const files = fls.filter((f) => !!f);
if (files.length === 0) {
return Promise.resolve([]);
}
const options = this.options;
const hooks = [...this.postHooks];
const store = this.store;
const validators: ICloneValidator[] = [];
if (options.skipLocal) {
validators.push(new SkipLocalValidator());
}
if (options.skipIsolated) {
validators.push(new SkipIsolatedValidator());
}
const detector = new Detector(this.tokenizer, store, validators, options);
this.subscribes.forEach((listener: ISubscriber) => {
Object
.entries(listener.subscribe())
.map(([event, handler]: [DetectorEvents, IHandler]) => detector.on(event, handler));
});
const detect = (entry: EntryWithContent, clones: IClone[] = []): Promise<IClone[]> => {
const {path, content} = entry;
const format: string = getFormatByFile(path, options.formatsExts);
return detector
.detect(path, content, format)
.then((clns: IClone[]) => {
if (clns) {
clones.push(...clns);
}
const file = files.pop();
if (file) {
return detect(file, clones);
}
return clones;
});
};
const processHooks = (hook: IHook, detectedClones: IClone[]): Promise<IClone[]> => {
return hook
.process(detectedClones)
.then((clones: IClone[]) => {
const nextHook: IHook = hooks.pop();
if (nextHook) {
return processHooks(nextHook, clones);
}
return clones;
});
}
return detect(files.pop())
.then((clones: IClone[]) => {
const hook = hooks.pop();
if (hook) {
return processHooks(hook, clones)
}
return clones;
})
.then((clones: IClone[]) => {
const statistic = this.statistic.getStatistic();
this.reporters.forEach((reporter: IReporter) => {
reporter.report(clones, statistic);
});
return clones;
});
}
}