11// @ts -nocheck
2- import { dirname , resolve } from "path" ;
2+ import { dirname , resolve , isAbsolute , relative } from "path" ;
33import { existsSync } from "fs" ;
44import { Command } from 'commander' ;
55import { readJSONSync } from 'fs-extra' ;
66import { getDefaultOptions , IOptions } from '@jscpd/core' ;
77import { 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+
931const 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 }
0 commit comments