Skip to content

Commit 718f154

Browse files
committed
Merge branch 'main' into feat/js-api-dest
2 parents 8d74fb5 + e163688 commit 718f154

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
@@ -177,25 +188,22 @@ export function copyfiles(sources: string | string[], outPath: string, options:
177188
// Use a Set for deduplication from the start
178189
const allFilesSet = new Set<string>();
179190
for (const pattern of sources) {
180-
let files = globSync(pattern, { exclude: excludeGlobs });
191+
let adjustedPattern = tryCreatingDir(pattern, pattern);
192+
// fs.globSync treats /** differently, so adjust to /**/*
193+
adjustedPattern = adjustedPattern.replace(/\*\*$/, '**/*');
194+
let files = globSync(adjustedPattern, { exclude: excludeGlobs });
181195
// If options.all is set and pattern does not start with a dot, also search for dot-prefixed files
182196
if (options.all && pattern.includes('*') && !pattern.startsWith('.')) {
183197
// e.g. '*.txt' => '.*.txt', '**/*.txt' => '**/.*.txt'
184-
const dotPattern = pattern.replace(/(\*\.[^/]+$|\*$)/, '.$1');
198+
const dotPattern = pattern.replace(/(\*\.[^/]+$|\*$)/, '.$1').replace(/\*\*$/, '**/*');
185199
if (dotPattern !== pattern) {
186200
files = files.concat(globSync(dotPattern, { exclude: excludeGlobs }));
187201
}
188202
}
189203
// Normalize all file paths to POSIX style (forward slashes)
190204
files = files.map(f => f.replaceAll('\\', '/'));
191205
// Remove directories manually (since nodir is not supported)
192-
files = files.filter(f => {
193-
try {
194-
return !statSync(f).isDirectory();
195-
} /* v8 ignore next */ catch {
196-
return false;
197-
}
198-
});
206+
files = files.filter(f => !tryCreatingDir(f, false));
199207
// Add to Set for deduplication
200208
for (const f of files) {
201209
allFilesSet.add(f);

0 commit comments

Comments
 (0)