|
| 1 | +import { $ } from "bun"; |
| 2 | +import { existsSync, mkdirSync, readFileSync, rmdirSync } from "node:fs"; |
| 3 | +import { resolve, normalize, join, dirname } from "node:path"; |
| 4 | + |
| 5 | +/// The type returned from the `cargo-msfs info -f` command |
| 6 | +interface InstalledSdkVersions { |
| 7 | + versions: { sim: "Msfs2020" | "Msfs2024"; up_to_date: boolean; installed?: string; latest: string }[]; |
| 8 | +} |
| 9 | + |
| 10 | +/// The docker image name |
| 11 | +const IMAGE_NAME = "navigation-data-interface-wasm-build"; |
| 12 | + |
| 13 | +/// Find workspace root |
| 14 | +function findWorkspaceRoot() { |
| 15 | + let previous = null; |
| 16 | + let current = normalize(process.cwd()); |
| 17 | + |
| 18 | + do { |
| 19 | + // Try reading a package.json in this directory |
| 20 | + const packageJson = join(current, "package.json"); |
| 21 | + if (existsSync(packageJson)) { |
| 22 | + const manifest = JSON.parse(readFileSync(packageJson, "utf8")); |
| 23 | + // Check if there is workspaces, meaning this is root |
| 24 | + if (manifest.workspaces) { |
| 25 | + return current; |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + // Iterate up |
| 30 | + previous = current; |
| 31 | + current = dirname(current); |
| 32 | + } while (current !== previous); |
| 33 | + |
| 34 | + return null; |
| 35 | +} |
| 36 | + |
| 37 | +async function main() { |
| 38 | + // Get workspace root for docker commands |
| 39 | + const workspaceRoot = findWorkspaceRoot(); |
| 40 | + if (!workspaceRoot) { |
| 41 | + console.error("[-] Unable to find workspace root. Exiting..."); |
| 42 | + process.exit(1); |
| 43 | + } |
| 44 | + |
| 45 | + // Ensure docker is installed and available |
| 46 | + await $`docker ps`.quiet().catch(() => { |
| 47 | + console.error("[-] Docker is not installed or not running"); |
| 48 | + process.exit(1); |
| 49 | + }); |
| 50 | + |
| 51 | + // Ensure image is built |
| 52 | + await $`docker image inspect ${IMAGE_NAME}:latest`.quiet().catch(async () => { |
| 53 | + const dockerfilePath = resolve(workspaceRoot, "Dockerfile"); |
| 54 | + console.info(`[*] Building '${IMAGE_NAME}' image from ${dockerfilePath}`); |
| 55 | + await $`docker build -t ${IMAGE_NAME} -f ${dockerfilePath} .`; |
| 56 | + }); |
| 57 | + |
| 58 | + // Ensure SDKs are up to date, rebuilding if needed |
| 59 | + const installedSdks = JSON.parse( |
| 60 | + await $`docker run --rm ${IMAGE_NAME} bash -c "cargo-msfs info -f"`.text(), |
| 61 | + ) as InstalledSdkVersions; |
| 62 | + if (installedSdks.versions.some(v => !v.up_to_date)) { |
| 63 | + console.info("[*] Updating SDK in Docker image..."); |
| 64 | + await $`docker build --build-arg CACHEBUST=${Date.now()} -t ${IMAGE_NAME} -f ${resolve(workspaceRoot, "Dockerfile")} .`; |
| 65 | + } |
| 66 | + |
| 67 | + // Clear out dir |
| 68 | + const outDir = resolve(workspaceRoot, "dist/wasm"); |
| 69 | + if (existsSync(outDir)) rmdirSync(outDir, { recursive: true }); |
| 70 | + |
| 71 | + // The work directory, relative to workspace root |
| 72 | + const relativeWorkdDir = process.cwd().replace(workspaceRoot, "").replaceAll("\\", "/"); |
| 73 | + |
| 74 | + // Determine which version(s) to build based on command line argument --version |
| 75 | + const allowedVersions = ["msfs2020", "msfs2024"]; |
| 76 | + let versionsToBuild = allowedVersions; |
| 77 | + |
| 78 | + const versionArgIndex = process.argv.indexOf("--version"); |
| 79 | + if (versionArgIndex !== -1 && process.argv[versionArgIndex + 1]) { |
| 80 | + const versionArg = process.argv[versionArgIndex + 1]; |
| 81 | + if (versionArg && allowedVersions.includes(versionArg)) { |
| 82 | + versionsToBuild = [versionArg]; |
| 83 | + } else { |
| 84 | + console.error(`Invalid version argument: ${versionArg}. Allowed values are ${allowedVersions.join(", ")}`); |
| 85 | + process.exit(1); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + // Build the selected versions |
| 90 | + for (const version of versionsToBuild) { |
| 91 | + console.info(`[*] Building for ${version}`); |
| 92 | + |
| 93 | + // Create the subfolder |
| 94 | + const simDir = join(outDir, version); |
| 95 | + const relativeSimDir = simDir.replace(workspaceRoot, "").replaceAll("\\", "/"); |
| 96 | + mkdirSync(simDir, { recursive: true }); |
| 97 | + |
| 98 | + // Run cargo-msfs |
| 99 | + await $`docker run \ |
| 100 | + -v ${workspaceRoot}:/workspace \ |
| 101 | + -w /workspace${relativeWorkdDir} \ |
| 102 | + ${IMAGE_NAME} \ |
| 103 | + bash -c "cargo-msfs build ${version} -i ./src/wasm -o ./${relativeSimDir}/msfs_navigation_data_interface.wasm"`.catch( |
| 104 | + err => process.exit(err.exitCode ?? 1), |
| 105 | + ); |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +await main(); |
0 commit comments