Skip to content

Commit 6a1e8b1

Browse files
author
Andrii Kucherenko
committed
feat(options): add ability set formats extensions
1 parent 16d4710 commit 6a1e8b1

File tree

5 files changed

+26
-8
lines changed

5 files changed

+26
-8
lines changed

src/cli.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ cli.option(
1717
'min size of duplication in code lines (Default is 5)'
1818
);
1919
cli.option(
20-
'-t, --thresh' + 'old [number]',
20+
'-t, --threshold [number]',
2121
'threshold for duplication, in case duplications >= threshold jscpd will exit with error'
2222
);
2323
cli.option(
@@ -49,8 +49,12 @@ cli.option(
4949
'-s, --silent',
5050
'Do not write detection progress and result to a console'
5151
);
52+
cli.option(
53+
'--formats-exts [string]',
54+
'list of formats with file extensions (javascript:es,es6;dart:dt)'
55+
);
5256
cli.option('-n, --no-cache', 'Do not cache results');
53-
cli.option('--xsl-href', '(Deprecated) Path to xsl file');
57+
cli.option('--xsl-href [string]', '(Deprecated) Path to xsl file');
5458
cli.option('-p, --path', '(Deprecated) Path to repo');
5559
cli.option(
5660
'-d, --debug',

src/formats/index.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ export function getSupportedFormats(): string[] {
55
return Object.keys(FORMATS);
66
}
77

8-
export function getFormatByFile(path: string): string | undefined {
8+
export function getFormatByFile(path: string, formatsExts?: { [key: string]: string[] }): string | undefined {
99
const ext: string = extname(path).slice(1);
10-
return Object.keys(FORMATS).find(language =>
11-
FORMATS[language].exts.includes(ext)
12-
);
10+
if (formatsExts && Object.keys(formatsExts).length) {
11+
return Object.keys(formatsExts).find(format => formatsExts[format].includes(ext));
12+
}
13+
return Object.keys(FORMATS).find(language => FORMATS[language].exts.includes(ext));
1314
}

src/interfaces/options.interface.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export interface IOptions {
77
minTokens: number;
88
threshold?: number;
99
xslHref?: string;
10+
formatsExts?: {[key: string]: string[]}
1011
output: string;
1112
path: string;
1213
mode: string | ((token: IToken) => boolean);

src/jscpd.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export class JSCPD {
3939
});
4040

4141
glob.on('match', path => {
42-
const format: string = getFormatByFile(path) as string;
42+
const format: string = getFormatByFile(path, this.options.formatsExts) as string;
4343
if (
4444
format &&
4545
this.options.format &&

src/utils/index.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export function prepareOptions(cli: Command): IOptions {
3131
output: cli.output,
3232
xslHref: cli.xslHref,
3333
format: cli.format,
34+
formatsExts: parseFormatsExtensions(cli.formatsExts),
3435
list: cli.list,
3536
threshold: cli.threshold,
3637
mode: cli.mode
@@ -86,18 +87,29 @@ export function prepareOptions(cli: Command): IOptions {
8687
return result;
8788
}
8889

90+
function parseFormatsExtensions(extensions: string): {[key: string]: string[]} {
91+
const result: {[key: string]: string[]} = {};
92+
extensions.split(';').forEach((format: string) => {
93+
const pair = format.split(':');
94+
result[pair[0]] = pair[1].split(',');
95+
});
96+
return result
97+
}
98+
99+
89100
export function getDefaultOptions(): IOptions {
90101
return {
91102
executionId: new Date().toISOString(),
92103
path: process.cwd(),
93104
minLines: 5,
94105
minTokens: 50,
95106
output: './report',
96-
reporter: ['console'],
107+
reporter: ['stat', 'console', 'time'],
97108
ignore: [],
98109
mode: 'mild',
99110
threshold: 0,
100111
format: [...getSupportedFormats()],
112+
formatsExts: {},
101113
debug: false,
102114
silent: false,
103115
blame: false,

0 commit comments

Comments
 (0)