Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 33 additions & 3 deletions apps/jscpd/src/options.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,33 @@
// @ts-nocheck
import {dirname, resolve} from "path";
import {dirname, resolve, isAbsolute, relative} from "path";
import {existsSync} from "fs";
import {Command} from 'commander';
import {readJSONSync} from 'fs-extra';
import {getDefaultOptions, IOptions} from '@jscpd/core';
import {parseFormatsExtensions} from '@jscpd/finder';

const resolveIgnorePattern = (configDir: string, pattern: string): string => {
// Don't modify if pattern is already absolute
if (isAbsolute(pattern)) {
return pattern;
}
// Don't modify if pattern starts with ** (meant to match at any depth)
if (pattern.startsWith('**/')) {
return pattern;
}
// For relative patterns, we need to adjust them to be relative to cwd
// instead of the config directory
const absolutePattern = resolve(configDir, pattern);
const cwd = process.cwd();
// If the config is in cwd or a subdirectory of cwd, make pattern relative to cwd
const relativePath = relative(cwd, absolutePattern);
if (!relativePath.startsWith('..')) {
return relativePath;
}
// Otherwise return as absolute
return absolutePattern;
};

const convertCliToOptions = (cli: Command): Partial<IOptions> => {
const result: Partial<IOptions> = {
minTokens: cli.minTokens ? parseInt(cli.minTokens) : undefined,
Expand Down Expand Up @@ -70,8 +92,12 @@ const readConfigJson = (config: string | undefined): Partial<IOptions> => {
const configExists = existsSync(configFile);
if (configExists) {
const result = {config: configFile, ...readJSONSync(configFile)};
const configDir = dirname(configFile);
if (result.path) {
result.path = result.path.map((path: string) => resolve(dirname(configFile), path));
result.path = result.path.map((path: string) => resolve(configDir, path));
}
if (result.ignore) {
result.ignore = result.ignore.map((pattern: string) => resolveIgnorePattern(configDir, pattern));
}
return result;
}
Expand All @@ -82,8 +108,12 @@ const readPackageJsonConfig = (): Partial<IOptions> => {
const config = resolve(process.cwd() + '/package.json');
if (existsSync(config)) {
const json = readJSONSync(config);
const configDir = dirname(config);
if (json.jscpd && json.jscpd.path) {
json.jscpd.path = json.jscpd.path.map((path: string) => resolve(dirname(config), path));
json.jscpd.path = json.jscpd.path.map((path: string) => resolve(configDir, path));
}
if (json.jscpd && json.jscpd.ignore) {
json.jscpd.ignore = json.jscpd.ignore.map((pattern: string) => resolveIgnorePattern(configDir, pattern));
}
return json.jscpd ? {config, ...json.jscpd} : {};
}
Expand Down
3 changes: 2 additions & 1 deletion packages/finder/src/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,9 @@ export function getFilesToDetect(options: IOptions): EntryWithContent[] {
onlyFiles: true,
dot: true,
stats: true,
absolute: options.absolute,
absolute: options.absolute || false,
followSymbolicLinks: !options.noSymlinks,
cwd: process.cwd(),
},
)
.filter(skipNotSupportedFormats(options))
Expand Down