Skip to content

Commit be252b0

Browse files
committed
chore(infra): adds build script to update static resource
1 parent 749bfc1 commit be252b0

File tree

1 file changed

+161
-0
lines changed

1 file changed

+161
-0
lines changed

scripts/build-flow-scanner.js

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
#!/usr/bin/env node
2+
3+
const {execSync} = require("child_process");
4+
const fs = require("fs");
5+
const path = require("path");
6+
const os = require("os");
7+
8+
function createTempDir() {
9+
"use strict";
10+
const tempDir = path.join(os.tmpdir(), `flow-scanner-build-${Date.now()}`);
11+
fs.mkdirSync(tempDir, {recursive: true});
12+
return tempDir;
13+
}
14+
15+
function cleanupTempDir(tempDir) {
16+
"use strict";
17+
try {
18+
fs.rmSync(tempDir, {recursive: true, force: true});
19+
} catch (error) {
20+
console.log(`Warning: Could not clean up temporary directory: ${error.message}`);
21+
}
22+
}
23+
24+
function copyDirSync(src, dest) {
25+
"use strict";
26+
fs.mkdirSync(dest, {recursive: true});
27+
const entries = fs.readdirSync(src, {withFileTypes: true});
28+
for (const entry of entries) {
29+
const srcPath = path.join(src, entry.name);
30+
const destPath = path.join(dest, entry.name);
31+
if (entry.isDirectory()) {
32+
copyDirSync(srcPath, destPath);
33+
} else {
34+
fs.copyFileSync(srcPath, destPath);
35+
}
36+
}
37+
}
38+
39+
function getLatestTag(cwd) {
40+
"use strict";
41+
try {
42+
const tagsOutput = execSync('git tag -l "core-v*"', {cwd, encoding: "utf8"});
43+
return tagsOutput
44+
.trim()
45+
.split(/\r?\n/)
46+
.map(t => t.trim())
47+
.filter(Boolean)
48+
.sort((a, b) => b.localeCompare(a, undefined, {numeric: true, sensitivity: "base"}))[0];
49+
} catch {
50+
return null;
51+
}
52+
}
53+
54+
function setupRemoteRepo(tempDir) {
55+
"use strict";
56+
const repoUrl = "https://github.com/Flow-Scanner/lightning-flow-scanner";
57+
const cloneDir = path.join(tempDir, ".full-clone");
58+
fs.mkdirSync(cloneDir, {recursive: true});
59+
60+
execSync(`git clone --depth 1 ${repoUrl} "${cloneDir}"`, {stdio: "inherit"});
61+
execSync("git fetch --tags --force", {cwd: cloneDir, stdio: "inherit"});
62+
63+
const targetVersion = process.argv[2];
64+
let tagToCheckout;
65+
66+
if (targetVersion) {
67+
tagToCheckout = targetVersion.startsWith("core-v") ? targetVersion : `core-v${targetVersion}`;
68+
console.log(`Using specified version: ${tagToCheckout}`);
69+
} else {
70+
tagToCheckout = getLatestTag(cloneDir);
71+
if (tagToCheckout) {
72+
console.log(`Auto-detected latest version: ${tagToCheckout}`);
73+
}
74+
}
75+
76+
if (!tagToCheckout) {
77+
console.error("No core-v* tags found. Aborting build.");
78+
process.exit(1);
79+
}
80+
81+
try {
82+
execSync(`git checkout tags/${tagToCheckout}`, {cwd: cloneDir, stdio: "inherit"});
83+
} catch (e) {
84+
console.error(`Failed to checkout tag ${tagToCheckout}: ${e.message}`);
85+
process.exit(1);
86+
}
87+
88+
const coreSource = path.join(cloneDir, "packages", "core");
89+
if (!fs.existsSync(coreSource)) {
90+
console.error("packages/core directory not found in repository");
91+
process.exit(1);
92+
}
93+
94+
copyDirSync(coreSource, tempDir);
95+
fs.rmSync(cloneDir, {recursive: true, force: true});
96+
}
97+
98+
function findUMDFile(distDir) {
99+
"use strict";
100+
const files = fs.readdirSync(distDir);
101+
const umdFile = files.find(file => file.endsWith(".umd.js") || file.endsWith(".umd.cjs"));
102+
if (!umdFile) {
103+
throw new Error("No UMD file found in dist directory");
104+
}
105+
return path.join(distDir, umdFile);
106+
}
107+
108+
function main() {
109+
"use strict";
110+
console.log("Lightning Flow Scanner Core Build Script");
111+
112+
const targetVersion = process.argv[2];
113+
if (targetVersion) {
114+
console.log(`Building specific version: ${targetVersion}`);
115+
} else {
116+
console.log("Building latest version (use 'node scripts/build-flow-scanner.js <version>' to specify a version)");
117+
}
118+
119+
let tempDir;
120+
121+
try {
122+
tempDir = createTempDir();
123+
setupRemoteRepo(tempDir);
124+
125+
const packageJson = JSON.parse(fs.readFileSync(path.join(tempDir, "package.json"), "utf8"));
126+
const version = packageJson.version;
127+
console.log(`Version: ${version}`);
128+
129+
execSync("npm install", {stdio: "inherit", cwd: tempDir});
130+
execSync("npm run vite:dist", {stdio: "inherit", cwd: tempDir});
131+
132+
const distDir = path.join(tempDir, "dist");
133+
if (!fs.existsSync(distDir)) {
134+
console.error("Dist directory not found. Build may have failed.");
135+
process.exit(1);
136+
}
137+
138+
const umdFilePath = findUMDFile(distDir);
139+
const umdContent = fs.readFileSync(umdFilePath, "utf8");
140+
141+
const outputPath = path.join(process.cwd(), "force-app", "main", "default", "staticresources", "LFS_Core.js");
142+
fs.writeFileSync(outputPath, umdContent, "utf8");
143+
144+
const fileSizeInKB = (fs.statSync(outputPath).size / 1024).toFixed(2);
145+
console.log(`Static resource updated: ${outputPath} (${fileSizeInKB} KB)`);
146+
147+
} catch (error) {
148+
console.error(`Build failed: ${error.message}`);
149+
process.exit(1);
150+
} finally {
151+
if (tempDir) {
152+
cleanupTempDir(tempDir);
153+
}
154+
}
155+
}
156+
157+
if (require.main === module) {
158+
main();
159+
}
160+
161+
module.exports = {main};

0 commit comments

Comments
 (0)