changepacks_node/
package.rs1use anyhow::Result;
2use async_trait::async_trait;
3use changepacks_core::{Language, Package, UpdateType};
4use changepacks_utils::next_version;
5use std::path::{Path, PathBuf};
6use tokio::fs::{read_to_string, write};
7
8#[derive(Debug)]
9pub struct NodePackage {
10 name: String,
11 version: String,
12 path: PathBuf,
13 relative_path: PathBuf,
14 is_changed: bool,
15}
16
17impl NodePackage {
18 pub fn new(name: String, version: String, path: PathBuf, relative_path: PathBuf) -> Self {
19 Self {
20 name,
21 version,
22 path,
23 relative_path,
24 is_changed: false,
25 }
26 }
27}
28
29#[async_trait]
30impl Package for NodePackage {
31 fn name(&self) -> &str {
32 &self.name
33 }
34
35 fn version(&self) -> &str {
36 &self.version
37 }
38
39 fn path(&self) -> &Path {
40 &self.path
41 }
42
43 fn relative_path(&self) -> &Path {
44 &self.relative_path
45 }
46
47 async fn update_version(&self, update_type: UpdateType) -> Result<()> {
48 let next_version = next_version(&self.version, update_type)?;
49
50 let package_json = read_to_string(&self.path).await?;
51 let mut package_json: serde_json::Value = serde_json::from_str(&package_json)?;
52 package_json["version"] = serde_json::Value::String(next_version);
53 write(&self.path, serde_json::to_string_pretty(&package_json)?).await?;
54 Ok(())
55 }
56
57 fn language(&self) -> Language {
58 Language::Node
59 }
60
61 fn set_changed(&mut self, changed: bool) {
62 self.is_changed = changed;
63 }
64 fn is_changed(&self) -> bool {
65 self.is_changed
66 }
67}