Skip to content

Commit eed1d8f

Browse files
committed
chore: updated safeCopy
1 parent 8d4d289 commit eed1d8f

File tree

2 files changed

+19
-25
lines changed

2 files changed

+19
-25
lines changed

src/generators/legacy-html/utils/__tests__/safeCopy.test.mjs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,8 @@ describe('safeCopy', () => {
103103
// Don't create any files in source
104104
await safeCopy(srcDir, targetDir);
105105

106-
// Verify no error occurred and target is still empty
107-
const files = await readFile(targetDir).catch(() => []);
108-
assert.ok(Array.isArray(files) || files === undefined);
106+
// Verify no error occurred - if we get here, the function succeeded
107+
assert.ok(true);
109108
});
110109

111110
it('should copy files with same size but different content when mtime is newer', async () => {

src/generators/legacy-html/utils/safeCopy.mjs

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,35 @@
11
'use strict';
22

3-
import { copyFile, readdir, stat, constants } from 'node:fs/promises';
3+
import { copyFile, readdir, stat } from 'node:fs/promises';
44
import { join } from 'node:path';
55

66
/**
7-
* Attempts to copy a file forcibly (`COPYFILE_FICLONE_FORCE`. Otherwise, falls back to a time-based check approach)
7+
* Copies files from source to target directory, skipping files that haven't changed.
8+
* Uses synchronous stat checks for simplicity and copyFile for atomic operations.
89
*
910
* @param {string} srcDir - Source directory path
1011
* @param {string} targetDir - Target directory path
1112
*/
1213
export async function safeCopy(srcDir, targetDir) {
13-
try {
14-
await copyFile(srcDir, targetDir, constants.COPYFILE_FICLONE);
15-
} catch (err) {
16-
if (err?.syscall !== 'copyfile') {
17-
throw err;
18-
}
19-
20-
const files = await readdir(srcDir);
14+
const files = await readdir(srcDir);
2115

22-
for (const file of files) {
23-
const sourcePath = join(srcDir, file);
24-
const targetPath = join(targetDir, file);
16+
for (const file of files) {
17+
const sourcePath = join(srcDir, file);
18+
const targetPath = join(targetDir, file);
2519

26-
const [sStat, tStat] = await Promise.all([
27-
stat(sourcePath),
28-
stat(targetPath),
29-
]).catch(() => []);
20+
const tStat = await stat(targetPath).catch(() => undefined);
3021

31-
const shouldWrite =
32-
!tStat || sStat.size !== tStat.size || sStat.mtimeMs > tStat.mtimeMs;
22+
// If target doesn't exist, copy immediately
23+
if (!tStat) {
24+
await copyFile(sourcePath, targetPath);
25+
continue;
26+
}
3327

34-
if (!shouldWrite) {
35-
continue;
36-
}
28+
// Target exists, check if we need to update
29+
const sStat = await stat(sourcePath);
3730

31+
// Skip if target has same size and source is not newer
32+
if (sStat.size !== tStat.size || sStat.mtimeMs > tStat.mtimeMs) {
3833
await copyFile(sourcePath, targetPath);
3934
}
4035
}

0 commit comments

Comments
 (0)