-
-
Notifications
You must be signed in to change notification settings - Fork 208
/
Copy pathxml.ts
38 lines (32 loc) · 1.57 KB
/
xml.ts
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
import {writeFileSync} from 'fs';
import {ensureDirSync} from 'fs-extra';
import {IReporter} from '..';
import {getOption, IClone, IOptions} from '@jscpd/core';
import {escapeXml, getPath} from '../utils/reports';
import {green} from 'colors/safe';
import {join} from "path";
export class XmlReporter implements IReporter {
constructor(private options: IOptions) {
}
public report(clones: IClone[]): void {
let xmlDoc = '<?xml version="1.0" encoding="UTF-8" ?>';
xmlDoc += '<pmd-cpd>';
clones.forEach((clone: IClone) => {
xmlDoc = `${xmlDoc}
<duplication lines="${clone.duplicationA.end.line - clone.duplicationA.start.line}">
<file path="${escapeXml(getPath(clone.duplicationA.sourceId, this.options))}" line="${clone.duplicationA.start.line}">
<codefragment><![CDATA[${clone.duplicationA.fragment.replace(/]]>/g, 'CDATA_END')}]]></codefragment>
</file>
<file path="${escapeXml(getPath(clone.duplicationB.sourceId, this.options))}" line="${clone.duplicationB.start.line}">
<codefragment><![CDATA[${clone.duplicationB.fragment.replace(/]]>/g, 'CDATA_END')}]]></codefragment>
</file>
<codefragment><![CDATA[${clone.duplicationA.fragment.replace(/]]>/g, 'CDATA_END')}]]></codefragment>
</duplication>
`;
});
xmlDoc += '</pmd-cpd>';
ensureDirSync(getOption('output', this.options));
writeFileSync(getOption('output', this.options) + '/jscpd-report.xml', xmlDoc);
console.log(green(`XML report saved to ${join(this.options.output, 'jscpd-report.xml')}`));
}
}