Skip to content

Commit 3eaa596

Browse files
authored
Merge pull request #744 from alanprice-admiral/fix/ignore-patterns-issue-496
Fix ignore patterns not working in configuration files
2 parents 6d3c2e4 + b5894a1 commit 3eaa596

2 files changed

Lines changed: 35 additions & 4 deletions

File tree

apps/jscpd/src/options.ts

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,33 @@
11
// @ts-nocheck
2-
import {dirname, resolve} from "path";
2+
import {dirname, resolve, isAbsolute, relative} from "path";
33
import {existsSync} from "fs";
44
import {Command} from 'commander';
55
import {readJSONSync} from 'fs-extra';
66
import {getDefaultOptions, IOptions} from '@jscpd/core';
77
import {parseFormatsExtensions} from '@jscpd/finder';
88

9+
const resolveIgnorePattern = (configDir: string, pattern: string): string => {
10+
// Don't modify if pattern is already absolute
11+
if (isAbsolute(pattern)) {
12+
return pattern;
13+
}
14+
// Don't modify if pattern starts with ** (meant to match at any depth)
15+
if (pattern.startsWith('**/')) {
16+
return pattern;
17+
}
18+
// For relative patterns, we need to adjust them to be relative to cwd
19+
// instead of the config directory
20+
const absolutePattern = resolve(configDir, pattern);
21+
const cwd = process.cwd();
22+
// If the config is in cwd or a subdirectory of cwd, make pattern relative to cwd
23+
const relativePath = relative(cwd, absolutePattern);
24+
if (!relativePath.startsWith('..')) {
25+
return relativePath;
26+
}
27+
// Otherwise return as absolute
28+
return absolutePattern;
29+
};
30+
931
const convertCliToOptions = (cli: Command): Partial<IOptions> => {
1032
const result: Partial<IOptions> = {
1133
minTokens: cli.minTokens ? parseInt(cli.minTokens) : undefined,
@@ -70,8 +92,12 @@ const readConfigJson = (config: string | undefined): Partial<IOptions> => {
7092
const configExists = existsSync(configFile);
7193
if (configExists) {
7294
const result = {config: configFile, ...readJSONSync(configFile)};
95+
const configDir = dirname(configFile);
7396
if (result.path) {
74-
result.path = result.path.map((path: string) => resolve(dirname(configFile), path));
97+
result.path = result.path.map((path: string) => resolve(configDir, path));
98+
}
99+
if (result.ignore) {
100+
result.ignore = result.ignore.map((pattern: string) => resolveIgnorePattern(configDir, pattern));
75101
}
76102
return result;
77103
}
@@ -82,8 +108,12 @@ const readPackageJsonConfig = (): Partial<IOptions> => {
82108
const config = resolve(process.cwd() + '/package.json');
83109
if (existsSync(config)) {
84110
const json = readJSONSync(config);
111+
const configDir = dirname(config);
85112
if (json.jscpd && json.jscpd.path) {
86-
json.jscpd.path = json.jscpd.path.map((path: string) => resolve(dirname(config), path));
113+
json.jscpd.path = json.jscpd.path.map((path: string) => resolve(configDir, path));
114+
}
115+
if (json.jscpd && json.jscpd.ignore) {
116+
json.jscpd.ignore = json.jscpd.ignore.map((pattern: string) => resolveIgnorePattern(configDir, pattern));
87117
}
88118
return json.jscpd ? {config, ...json.jscpd} : {};
89119
}

packages/finder/src/files.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,9 @@ export function getFilesToDetect(options: IOptions): EntryWithContent[] {
102102
onlyFiles: true,
103103
dot: true,
104104
stats: true,
105-
absolute: options.absolute,
105+
absolute: options.absolute || false,
106106
followSymbolicLinks: !options.noSymlinks,
107+
cwd: process.cwd(),
107108
},
108109
)
109110
.filter(skipNotSupportedFormats(options))

0 commit comments

Comments
 (0)