Skip to content

Commit bef529e

Browse files
authored
chore: support Windows and MacOS in binding version update (#265)
Add `replace-file-content` utility to fix cross-platform compatibility in release workflow ## What changed? - Created a new cross-platform Node.js utility script `replace-file-content` that replaces text in files - Updated the release workflow to use this utility instead of `sed` for modifying the binding version - Added tests for the new utility to ensure it works correctly - Added the tools package as a workspace dependency in the root package.json ## Why? The previous approach using `sed` in the release workflow is not cross-platform compatible, which can cause issues when running on different operating systems. The new utility provides a consistent way to replace file content across all platforms. ## How to test? The PR includes snapshot tests for the new utility that verify it correctly replaces content in files. You can also manually test by running: ``` pnpm exec replace-file-content <filename> <search-text> <replacement-text> ``` For example: ``` pnpm exec replace-file-content example.toml 'version = "0.0.0"' 'version = "1.0.0"' ```
1 parent 930f74c commit bef529e

8 files changed

Lines changed: 82 additions & 3 deletions

File tree

.github/workflows/release.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ jobs:
4848
- uses: oxc-project/setup-node@fdbf0dfd334c4e6d56ceeb77d91c76339c2a0885 # v1.0.4
4949

5050
- name: Set binding version
51-
run: |
52-
sed -i 's/version = "0.0.0"/version = "0.0.0-${{ github.sha }}"/' packages/cli/binding/Cargo.toml
51+
run: pnpm exec replace-file-content packages/cli/binding/Cargo.toml 'version = "0.0.0"' 'version = "0.0.0-${{ github.sha }}"'
5352

5453
- name: Build
5554
if: ${{ matrix.settings.target == 'x86_64-unknown-linux-gnu' }}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"@oxc-node/core": "catalog:",
2323
"@types/node": "catalog:",
2424
"@voidzero-dev/vite-plus": "workspace:*",
25+
"@voidzero-dev/vite-plus-tools": "workspace:*",
2526
"husky": "catalog:",
2627
"lint-staged": "catalog:",
2728
"oxfmt": "catalog:",

packages/tools/package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@
44
"type": "module",
55
"bin": {
66
"json-edit": "./src/json-edit.ts",
7-
"snap-test": "./src/snap-test.ts"
7+
"snap-test": "./src/snap-test.ts",
8+
"replace-file-content": "./src/replace-file-content.ts"
89
},
910
"devDependencies": {
1011
"minimatch": "catalog:"
12+
},
13+
"scripts": {
14+
"snap-test": "snap-test"
1115
}
1216
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[package]
2+
name = "foo"
3+
version = "0.0.0"
4+
edition = "2024"
5+
6+
[[bin]]
7+
name = "vite"
8+
path = "src/main.rs"
9+
10+
[dependencies]
11+
clap = { workspace = true, features = ["derive"] }
12+
crossterm = { workspace = true }
13+
napi = { workspace = true }
14+
napi-derive = { workspace = true }
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
> cat foo/example.toml # should show original toml file
2+
[package]
3+
name = "foo"
4+
version = "0.0.0"
5+
edition = "2024"
6+
7+
[[bin]]
8+
name = "vite"
9+
path = "src/main.rs"
10+
11+
[dependencies]
12+
clap = { workspace = true, features = ["derive"] }
13+
crossterm = { workspace = true }
14+
napi = { workspace = true }
15+
napi-derive = { workspace = true }
16+
17+
> replace-file-content foo/example.toml 'version = "0.0.0"' 'version = "1.0.0"' && cat foo/example.toml # should edit toml file
18+
[package]
19+
name = "foo"
20+
version = "1.0.0"
21+
edition = "2024"
22+
23+
[[bin]]
24+
name = "vite"
25+
path = "src/main.rs"
26+
27+
[dependencies]
28+
clap = { workspace = true, features = ["derive"] }
29+
crossterm = { workspace = true }
30+
napi = { workspace = true }
31+
napi-derive = { workspace = true }
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"env": {
3+
},
4+
"commands": [
5+
"cat foo/example.toml # should show original toml file",
6+
"replace-file-content foo/example.toml 'version = \"0.0.0\"' 'version = \"1.0.0\"' && cat foo/example.toml # should edit toml file"
7+
]
8+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env node
2+
3+
import { readFileSync, writeFileSync } from 'node:fs';
4+
import path from 'node:path';
5+
6+
const filename = process.argv[2];
7+
const searchValue = process.argv[3];
8+
const newValue = process.argv[4];
9+
10+
if (!filename || !searchValue || !newValue) {
11+
console.error('Usage: replace-file-content <filename> <searchValue> <newValue>');
12+
console.error('Example: replace-file-content example.toml \'version = "0.0.0"\' \'version = "0.0.1"\'');
13+
process.exit(1);
14+
}
15+
16+
const filepath = path.resolve(filename);
17+
const content = readFileSync(filepath, 'utf-8');
18+
const newContent = content.replace(searchValue, newValue);
19+
writeFileSync(filepath, newContent, 'utf-8');

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)