-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathbuild.rs
More file actions
49 lines (41 loc) · 1.49 KB
/
build.rs
File metadata and controls
49 lines (41 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
use std::{env, fs, path::Path, process::Command};
/// Build script: sets up napi bindings and syncs package.json version
/// with the Cargo workspace version.
fn main() {
println!("cargo:rerun-if-changed=package.json");
sync_package_json_version();
napi_build::setup();
}
/// Read the Cargo package version and update package.json if different.
fn sync_package_json_version() {
let cargo_version = env::var("CARGO_PKG_VERSION").expect("CARGO_PKG_VERSION not set");
let package_json_path = Path::new("package.json");
let Ok(contents) = fs::read_to_string(package_json_path) else {
return;
};
let expected = format!(" \"version\": \"{cargo_version}\",");
let mut result = String::with_capacity(contents.len());
let mut changed = false;
for line in contents.lines() {
if !changed && line.starts_with(" \"version\"") {
if line == expected {
return;
}
result.push_str(&expected);
changed = true;
} else {
result.push_str(line);
}
result.push('\n');
}
if !changed {
return;
}
eprintln!("Updating package.json version to {cargo_version}");
fs::write(package_json_path, &result).expect("failed to write package.json");
let status = Command::new("npm")
.args(["install", "--package-lock-only"])
.status()
.expect("failed to run npm");
assert!(status.success(), "npm install --package-lock-only failed");
}