Skip to content

Commit e40f73a

Browse files
committed
feat: add wasm build script
1 parent e2fe15f commit e40f73a

File tree

9 files changed

+252
-49
lines changed

9 files changed

+252
-49
lines changed

.github/workflows/pr.yml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,14 @@ jobs:
8181
echo SENTRY_URL=${{ secrets.SENTRY_URL }} >> .env
8282
8383
- name: Build WASM
84-
run: ./scripts/workflow.sh
84+
uses: oven-sh/setup-bun@v2
85+
run: |
86+
bun install
87+
bun run package
8588
8689
- name: Upload WASM module to GitHub
8790
uses: actions/upload-artifact@v4
8891
with:
89-
name: msfs_navigation_data_interface
92+
name: wasm
9093
path: |
91-
./2020.zip
92-
./2024.zip
94+
./wasm.zip

.github/workflows/pre-release.yml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,16 @@ jobs:
1919
echo SENTRY_URL=${{ secrets.SENTRY_URL }} >> .env
2020
2121
- name: Build WASM
22-
run: ./scripts/workflow.sh
22+
uses: oven-sh/setup-bun@v2
23+
run: |
24+
bun install
25+
bun run package
2326
2427
- name: Pre-Release
2528
uses: softprops/action-gh-release@v1
2629
with:
2730
files: |
28-
./2020.zip
29-
./2024.zip
31+
./wasm.zip
3032
prerelease: true
3133
generate_release_notes: true
3234

@@ -51,4 +53,4 @@ jobs:
5153
# - name: Publish to NPM
5254
# env:
5355
# NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
54-
# run: cd src/js && npm publish --tag next
56+
# run: cd src/js && npm publish --tag next

.github/workflows/release.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,16 @@ jobs:
2020
echo SENTRY_URL=${{ secrets.SENTRY_URL }} >> .env
2121
2222
- name: Build WASM
23-
run: ./scripts/workflow.sh
23+
uses: oven-sh/setup-bun@v2
24+
run: |
25+
bun install
26+
bun run package
2427
2528
- name: Release
2629
uses: softprops/action-gh-release@v1
2730
with:
2831
files: |
29-
./2020.zip
30-
./2024.zip
32+
./wasm.zip
3133
generate_release_notes: true
3234

3335
# Enable in future to automate publishing of NPM package

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,4 @@ target/
2323
*.local
2424
.DS_Store
2525

26-
test_work/
27-
dist
26+
dist

Dockerfile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM rust:1.84
1+
FROM rust:1.84.1
22

33
# Install needed packages and clean up
44
RUN apt update && \
@@ -19,6 +19,9 @@ RUN rustup target install wasm32-wasip1
1919
# Install cargo-msfs
2020
RUN cargo install --git https://github.com/navigraph/cargo-msfs
2121

22+
# Cache bust arg to re-install both SDKs
23+
ARG CACHEBUST
24+
2225
# Install MSFS2020 and MSFS2024 SDK
2326
RUN cargo-msfs install msfs2020 && \
2427
cargo-msfs install msfs2024

bun.lock

Lines changed: 113 additions & 32 deletions
Large diffs are not rendered by default.

package.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,16 @@
66
],
77
"scripts": {
88
"lint": "bun run --filter \"*\" lint",
9-
"typecheck": "bun run --filter \"*\" typecheck"
9+
"typecheck": "bun run --filter \"*\" typecheck",
10+
"build:wasm": "bun ./scripts/cargo-msfs.ts",
11+
"build:wasm:2020": "bun ./scripts/cargo-msfs.ts --version msfs2020",
12+
"build:wasm:2024": "bun ./scripts/cargo-msfs.ts --version msfs2024",
13+
"package": "bun run build:wasm && bestzip wasm.zip dist/wasm/*"
1014
},
1115
"devDependencies": {
1216
"@eslint/js": "^9.23.0",
1317
"@types/bun": "latest",
18+
"bestzip": "^2.2.1",
1419
"eslint": "^9.23.0",
1520
"eslint-config-prettier": "^10.1.1",
1621
"eslint-plugin-prettier": "^5.2.6",
@@ -21,4 +26,4 @@
2126
"peerDependencies": {
2227
"typescript": "^5.0.0"
2328
}
24-
}
29+
}

rust-toolchain.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
[toolchain]
22
profile = "default"
3-
channel = "1.84"
3+
channel = "1.84.1"
44
targets = ["wasm32-wasip1"]

scripts/cargo-msfs.ts

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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

Comments
 (0)