Skip to content

Commit 70f1d4d

Browse files
authored
feat(copyFiles): improve with size filter (#93)
* feat(copyFiles): improve with size filter * feat(copyFiles): enhance file copy logic with mtime checks and logging * Change files * feat(copyFiles): add configurable skipIfPossible flag * fix(copyFiles): passed all tests in test:ci
1 parent 4666268 commit 70f1d4d

File tree

2 files changed

+48
-4
lines changed

2 files changed

+48
-4
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "minor",
3+
"comment": "added sizeCheck as a flag",
4+
"packageName": "@minecraft/core-build-tasks",
5+
"email": "[email protected]",
6+
"dependentChangeType": "patch"
7+
}

tools/core-build-tasks/src/tasks/helpers/copyFiles.ts

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,59 @@
44
import { FileSystem } from '@rushstack/node-core-library';
55
import path from 'path';
66

7-
export function copyFiles(originPaths: string[], outputPath: string) {
8-
let destinationPath = path.resolve(outputPath);
7+
export function copyFiles(originPaths: string[], outputPath: string, skipIfPossible: boolean = true) {
8+
const destinationPath = path.resolve(outputPath);
9+
const MTIME_TOLERANCE_MS = 1000; // 1 second tolerance, avoid the case when file copying across system get delayed
910
for (const originPath of originPaths) {
1011
const inputPath = path.resolve(originPath);
1112
const pathStats = FileSystem.getLinkStatistics(inputPath);
1213
if (pathStats.isDirectory()) {
1314
console.log(`Copying folder ${inputPath} to ${destinationPath}`);
1415
} else {
1516
const filename = path.parse(inputPath).base;
16-
destinationPath = path.resolve(destinationPath, filename);
17-
console.log(`Copying file ${inputPath} to ${destinationPath}`);
17+
const fileDestinationPath = path.resolve(destinationPath, filename);
18+
19+
let shouldCopy = true;
20+
if (skipIfPossible) {
21+
try {
22+
const destFileStats = FileSystem.getStatistics(fileDestinationPath);
23+
// If sizes differ => must copy
24+
if (destFileStats.size !== pathStats.size) {
25+
shouldCopy = true;
26+
} else {
27+
// sizes equal -> check mtimes within tolerance
28+
const srcMtime = pathStats.mtimeMs ?? pathStats.mtime.getTime();
29+
const destMtime = destFileStats.mtimeMs ?? destFileStats.mtime.getTime();
30+
if (Math.abs(srcMtime - destMtime) > MTIME_TOLERANCE_MS) {
31+
shouldCopy = true;
32+
} else {
33+
// sizes equal and mtimes within tolerance -> skip copy
34+
shouldCopy = false;
35+
}
36+
}
37+
} catch {
38+
shouldCopy = true;
39+
}
40+
}
41+
42+
if (!shouldCopy) {
43+
console.log(`Skipping copy for ${inputPath}; no change detected`);
44+
continue;
45+
}
46+
47+
console.log(`Copying file ${inputPath} to ${fileDestinationPath}`);
48+
FileSystem.copyFiles({
49+
sourcePath: inputPath,
50+
destinationPath: fileDestinationPath,
51+
preserveTimestamps: true,
52+
});
53+
continue;
1854
}
1955

2056
FileSystem.copyFiles({
2157
sourcePath: inputPath,
2258
destinationPath: destinationPath,
59+
preserveTimestamps: true,
2360
});
2461
}
2562
}

0 commit comments

Comments
 (0)