Skip to content

Commit e163688

Browse files
authored
fix: adjust /** patterns to // for fs.globSync compatibility (#60)
1 parent 6dfb43c commit e163688

1 file changed

Lines changed: 18 additions & 10 deletions

File tree

src/index.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createReadStream, createWriteStream, existsSync, globSync, mkdirSync, statSync } from 'node:fs';
1+
import { createReadStream, createWriteStream, existsSync, globSync, mkdirSync, type PathLike, statSync } from 'node:fs';
22
import { basename, dirname, extname, join, normalize, posix, sep } from 'node:path';
33
import untildify from 'untildify';
44
import type { CopyFileOptions } from './interfaces.js';
@@ -115,6 +115,17 @@ export function filterDotFiles(paths: string[], dot: boolean): string[] {
115115
});
116116
}
117117

118+
function tryCreatingDir(path: PathLike, defaultReturn: any) {
119+
try {
120+
if (statSync(path).isDirectory()) {
121+
return `${path}/**`;
122+
}
123+
} catch {
124+
// fall through
125+
}
126+
return defaultReturn;
127+
}
128+
118129
/**
119130
* Copy the files per a glob pattern, the first item(s) can be a 1 or more files to copy
120131
* while the last item in the array is the output outDirectory directory
@@ -180,25 +191,22 @@ export function copyfiles(paths: string[], options: CopyFileOptions = {}, callba
180191
// Use a Set for deduplication from the start
181192
const allFilesSet = new Set<string>();
182193
for (const pattern of sources) {
183-
let files = globSync(pattern, { exclude: excludeGlobs });
194+
let adjustedPattern = tryCreatingDir(pattern, pattern);
195+
// fs.globSync treats /** differently, so adjust to /**/*
196+
adjustedPattern = adjustedPattern.replace(/\*\*$/, '**/*');
197+
let files = globSync(adjustedPattern, { exclude: excludeGlobs });
184198
// If options.all is set and pattern does not start with a dot, also search for dot-prefixed files
185199
if (options.all && pattern.includes('*') && !pattern.startsWith('.')) {
186200
// e.g. '*.txt' => '.*.txt', '**/*.txt' => '**/.*.txt'
187-
const dotPattern = pattern.replace(/(\*\.[^/]+$|\*$)/, '.$1');
201+
const dotPattern = pattern.replace(/(\*\.[^/]+$|\*$)/, '.$1').replace(/\*\*$/, '**/*');
188202
if (dotPattern !== pattern) {
189203
files = files.concat(globSync(dotPattern, { exclude: excludeGlobs }));
190204
}
191205
}
192206
// Normalize all file paths to POSIX style (forward slashes)
193207
files = files.map(f => f.replaceAll('\\', '/'));
194208
// Remove directories manually (since nodir is not supported)
195-
files = files.filter(f => {
196-
try {
197-
return !statSync(f).isDirectory();
198-
} /* v8 ignore next */ catch {
199-
return false;
200-
}
201-
});
209+
files = files.filter(f => !tryCreatingDir(f, false));
202210
// Add to Set for deduplication
203211
for (const f of files) {
204212
allFilesSet.add(f);

0 commit comments

Comments
 (0)