Skip to content

Commit c5ef0d1

Browse files
Add script to copy wasm file
Signed-off-by: Thomas Poignant <[email protected]>
1 parent 2bbc06e commit c5ef0d1

File tree

3 files changed

+114
-7
lines changed

3 files changed

+114
-7
lines changed

libs/providers/go-feature-flag/project.json

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,23 @@
2020
"executor": "nx:run-commands",
2121
"options": {
2222
"commands": [
23-
"git submodule update --init wasm-releases",
24-
"cp wasm-releases/evaluation/gofeatureflag-evaluation_v1.45.6.wasm src/lib/wasm/wasm-module/gofeatureflag-evaluation.wasm"
23+
"node scripts/copy-latest-wasm.js"
2524
],
2625
"cwd": "libs/providers/go-feature-flag",
2726
"parallel": false
2827
}
2928
},
3029
"lint": {
3130
"executor": "@nx/eslint:lint",
32-
"outputs": ["{options.outputFile}"]
31+
"outputs": [
32+
"{options.outputFile}"
33+
]
3334
},
3435
"test": {
3536
"executor": "@nx/jest:jest",
36-
"outputs": ["{workspaceRoot}/coverage/libs/providers/go-feature-flag"],
37+
"outputs": [
38+
"{workspaceRoot}/coverage/libs/providers/go-feature-flag"
39+
],
3740
"options": {
3841
"jestConfig": "libs/providers/go-feature-flag/jest.config.ts"
3942
},
@@ -45,7 +48,9 @@
4548
},
4649
"package": {
4750
"executor": "@nx/rollup:rollup",
48-
"outputs": ["{options.outputPath}"],
51+
"outputs": [
52+
"{options.outputPath}"
53+
],
4954
"dependsOn": [
5055
{
5156
"target": "copy-wasm"
@@ -60,7 +65,10 @@
6065
"generateExportsField": true,
6166
"umdName": "go-feature-flag",
6267
"external": "all",
63-
"format": ["cjs", "esm"],
68+
"format": [
69+
"cjs",
70+
"esm"
71+
],
6472
"assets": [
6573
{
6674
"glob": "package.json",
@@ -82,4 +90,4 @@
8290
}
8391
},
8492
"tags": []
85-
}
93+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Scripts
2+
3+
This directory contains utility scripts for the go-feature-flag provider.
4+
5+
## copy-latest-wasm.js
6+
7+
This script copies the go-feature-flag WASM evaluation module from the `wasm-releases` submodule.
8+
9+
### Purpose
10+
11+
Previously, the WASM filename was hardcoded with a specific version (e.g., `gofeatureflag-evaluation_v1.45.6.wasm`), which made updates cumbersome and error-prone. This script replaces that approach with a configurable solution that:
12+
13+
1. Uses an explicit version constant (`TARGET_WASM_VERSION`) for controlled updates
14+
2. Updates the git submodule to get the latest WASM releases
15+
3. Validates that the requested version exists before copying
16+
4. Copies the WASM file to the expected location
17+
18+
### Configuration
19+
20+
The script has one configuration constant at the top:
21+
22+
- `TARGET_WASM_VERSION`: The explicit version to use (e.g., `'v1.45.6'`)
23+
24+
### Usage
25+
26+
The script is automatically executed by the `copy-wasm` target in `project.json` and is used as a dependency for the `test` and `package` targets.
27+
28+
You can also run it manually:
29+
30+
```bash
31+
node scripts/copy-latest-wasm.js
32+
```
33+
34+
### Benefits
35+
36+
- **Explicit control**: You can specify exactly which version to use
37+
- **Easy updates**: Simply change the `TARGET_WASM_VERSION` constant when you want to upgrade
38+
- **Error prevention**: Validates that the requested version exists before copying
39+
- **Maintainability**: Reduces manual maintenance overhead while providing control
40+
- **Consistency**: Ensures reproducible builds with known versions
41+
- **Simplicity**: Clear and straightforward approach without complex logic
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/usr/bin/env node
2+
3+
const fs = require('fs');
4+
const path = require('path');
5+
const { execSync } = require('child_process');
6+
7+
/**
8+
* Script to copy the go-feature-flag WASM evaluation module
9+
* This replaces the hardcoded version approach with a configurable one
10+
*/
11+
const TARGET_WASM_VERSION = 'v1.45.6';
12+
13+
function copyWasmFile() {
14+
try {
15+
// Update git submodule first
16+
console.log('Updating git submodule...');
17+
execSync('git submodule update --init wasm-releases', {
18+
cwd: path.join(__dirname, '..'),
19+
stdio: 'inherit',
20+
});
21+
22+
const wasmFileName = `gofeatureflag-evaluation_${TARGET_WASM_VERSION}.wasm`;
23+
console.log(`Using explicit WASM version: ${TARGET_WASM_VERSION}`);
24+
25+
const sourcePath = path.join(__dirname, '../wasm-releases/evaluation', wasmFileName);
26+
const targetPath = path.join(__dirname, '../src/lib/wasm/wasm-module/gofeatureflag-evaluation.wasm');
27+
28+
// Check if the source file exists
29+
if (!fs.existsSync(sourcePath)) {
30+
console.error(`Error: WASM file not found: ${sourcePath}`);
31+
console.error('Available files in wasm-releases/evaluation:');
32+
const evaluationDir = path.join(__dirname, '../wasm-releases/evaluation');
33+
if (fs.existsSync(evaluationDir)) {
34+
const files = fs.readdirSync(evaluationDir).filter((file) => file.endsWith('.wasm'));
35+
files.forEach((file) => console.error(` - ${file}`));
36+
}
37+
process.exit(1);
38+
}
39+
40+
// Ensure target directory exists
41+
const targetDir = path.dirname(targetPath);
42+
if (!fs.existsSync(targetDir)) {
43+
fs.mkdirSync(targetDir, { recursive: true });
44+
}
45+
46+
// Copy the file
47+
console.log(`Copying ${wasmFileName} to ${targetPath}...`);
48+
fs.copyFileSync(sourcePath, targetPath);
49+
50+
console.log('✅ Successfully copied WASM file');
51+
} catch (error) {
52+
console.error('Error copying WASM file:', error.message);
53+
process.exit(1);
54+
}
55+
}
56+
57+
// Run the script
58+
copyWasmFile();

0 commit comments

Comments
 (0)