-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcheck_build_needed.mjs
70 lines (58 loc) · 2.12 KB
/
check_build_needed.mjs
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { statSync, existsSync, readdirSync } from "fs";
import { join } from "path";
// Paths to check
const sourceDir = "src-python/src";
const configFile = "src-python/pyproject.toml";
const outputDir = "src-python/target/release";
const tauriOutputDir = "src-tauri/binaries";
// Helper: Get the latest modification time of all files in a directory recursively
function getLastModifiedTime(dir) {
let latestTime = 0;
// Read all files and subdirectories
for (const file of readdirSync(dir)) {
const filePath = join(dir, file);
const stats = statSync(filePath);
if (stats.isDirectory()) {
// Recurse into subdirectories
latestTime = Math.max(latestTime, getLastModifiedTime(filePath));
} else {
// Compare file modification times
latestTime = Math.max(latestTime, stats.mtimeMs);
}
}
return latestTime;
}
// Check if a build is needed
function isBuildNeeded() {
// If the output directory does not exist, we need to build
if (!existsSync(outputDir)) {
console.log("BUILD_NEEDED"); // Output a specific string to stdout
process.exit(0)
}
// If the tauri output directory does not exist, we need to build
if (!existsSync(tauriOutputDir)) {
console.log("BUILD_NEEDED"); // Output a specific string to stdout
process.exit(0)
}
// If the tauri output directory exists but is empty, we need to build
if (readdirSync(tauriOutputDir).length === 0) {
console.log("BUILD_NEEDED"); // Output a specific string to stdout
process.exit(0)
}
// Get the last modification times
const sourceTime = getLastModifiedTime(sourceDir);
// console.log("sourceTime", sourceTime);
const configTime = existsSync(configFile) ? statSync(configFile).mtimeMs : 0;
// console.log("configTime", configTime);
const outputTime = statSync(outputDir).mtimeMs;
// console.log("outputTime", outputTime);
// Build if any source or config file is newer than the output
return Math.max(sourceTime, configTime) > outputTime;
}
// Run the check
if (isBuildNeeded()) {
console.log("BUILD_NEEDED"); // Output a specific string to stdout
process.exit(0)
} else {
process.exit(0)
}