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