返回 CodeWhale
export.rs
1 use std::path::{Path, PathBuf};
2
3 fn retain(path: &Path, bytes: &[u8], write: bool) -> std::io::Result<()> {
4 if write {
5 if let Some(parent) = path.parent() {
6 std::fs::create_dir_all(parent)?;
7 }
8 std::fs::write(path, bytes)
9 } else if std::fs::read(path)? == bytes {
10 Ok(())
11 } else {
12 Err(std::io::Error::other(format!(
13 "generated contract drift: {}",
14 path.display()
15 )))
16 }
17 }
18
19 fn main() -> Result<(), Box<dyn std::error::Error>> {
20 let arguments: Vec<String> = std::env::args().skip(1).collect();
21 if !matches!(arguments.as_slice(), [mode] if mode == "--write" || mode == "--check") {
22 return Err("use --write or --check against the declared package and SDK outputs".into());
23 }
24 let write = arguments[0] == "--write";
25 let package = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
26 let repository = package
27 .parent()
28 .and_then(Path::parent)
29 .ok_or("package repository root")?;
30 let schema = codewhale_app_server_protocol::export::runtime_schema();
31 let typescript = codewhale_app_server_protocol::export::runtime_typescript(&schema);
32 let json = serde_json::to_string_pretty(&schema)? + "\n";
33 retain(
34 &package.join("schema/json/runtime.schema.json"),
35 json.as_bytes(),
36 write,
37 )?;
38 retain(
39 &package.join("schema/typescript/runtime.d.ts"),
40 typescript.as_bytes(),
41 write,
42 )?;
43 retain(
44 &repository.join("npm/runtime-sdk/generated/runtime.d.ts"),
45 typescript.as_bytes(),
46 write,
47 )?;
48 Ok(())
49 }
50
50 lines RUST