Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "minor",
"comment": "added sizeCheck as a flag",
"packageName": "@minecraft/core-build-tasks",
"email": "keyyard8888@gmail.com",
"dependentChangeType": "patch"
}
45 changes: 41 additions & 4 deletions tools/core-build-tasks/src/tasks/helpers/copyFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,59 @@
import { FileSystem } from '@rushstack/node-core-library';
import path from 'path';

export function copyFiles(originPaths: string[], outputPath: string) {
let destinationPath = path.resolve(outputPath);
export function copyFiles(originPaths: string[], outputPath: string, skipIfPossible: boolean = true) {
const destinationPath = path.resolve(outputPath);
const MTIME_TOLERANCE_MS = 1000; // 1 second tolerance, avoid the case when file copying across system get delayed
for (const originPath of originPaths) {
const inputPath = path.resolve(originPath);
const pathStats = FileSystem.getLinkStatistics(inputPath);
if (pathStats.isDirectory()) {
console.log(`Copying folder ${inputPath} to ${destinationPath}`);
} else {
const filename = path.parse(inputPath).base;
destinationPath = path.resolve(destinationPath, filename);
console.log(`Copying file ${inputPath} to ${destinationPath}`);
const fileDestinationPath = path.resolve(destinationPath, filename);

let shouldCopy = true;
if (skipIfPossible) {
try {
const destFileStats = FileSystem.getStatistics(fileDestinationPath);
// If sizes differ => must copy
if (destFileStats.size !== pathStats.size) {
shouldCopy = true;
} else {
// sizes equal -> check mtimes within tolerance
const srcMtime = pathStats.mtimeMs ?? pathStats.mtime.getTime();
const destMtime = destFileStats.mtimeMs ?? destFileStats.mtime.getTime();
if (Math.abs(srcMtime - destMtime) > MTIME_TOLERANCE_MS) {
shouldCopy = true;
} else {
// sizes equal and mtimes within tolerance -> skip copy
shouldCopy = false;
}
}
} catch {
shouldCopy = true;
}
}

if (!shouldCopy) {
console.log(`Skipping copy for ${inputPath}; no change detected`);
continue;
}

console.log(`Copying file ${inputPath} to ${fileDestinationPath}`);
FileSystem.copyFiles({
sourcePath: inputPath,
destinationPath: fileDestinationPath,
preserveTimestamps: true,
});
continue;
}

FileSystem.copyFiles({
sourcePath: inputPath,
destinationPath: destinationPath,
preserveTimestamps: true,
});
}
}