-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathsafeCopy.mjs
More file actions
35 lines (27 loc) · 1.04 KB
/
safeCopy.mjs
File metadata and controls
35 lines (27 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
'use strict';
import { statSync, constants } from 'node:fs';
import { copyFile, readdir } from 'node:fs/promises';
import { join } from 'node:path';
/**
* Copies files from source to target directory, skipping files that haven't changed.
* Uses synchronous stat checks for simplicity and copyFile for atomic operations.
*
* @param {string} srcDir - Source directory path
* @param {string} targetDir - Target directory path
*/
export async function safeCopy(srcDir, targetDir) {
const files = await readdir(srcDir);
const promises = files.map(file => {
const sourcePath = join(srcDir, file);
const targetPath = join(targetDir, file);
const tStat = statSync(targetPath, { throwIfNoEntry: false });
if (tStat === undefined) {
return copyFile(sourcePath, targetPath, constants.COPYFILE_FICLONE);
}
const sStat = statSync(sourcePath);
if (sStat.size !== tStat.size || sStat.mtimeMs > tStat.mtimeMs) {
return copyFile(sourcePath, targetPath, constants.COPYFILE_FICLONE);
}
});
await Promise.all(promises);
}