rsw/core/
link.rs

1//! rsw link
2
3use std::path::PathBuf;
4
5use crate::core::RswInfo;
6use crate::utils::{get_root, os_cli, print};
7
8pub struct Link {
9    cli: String,
10    name: String,
11    cwd: PathBuf,
12}
13
14impl Link {
15    pub fn new(cli: String, cwd: PathBuf, name: String) -> Link {
16        Link { cli, cwd, name }
17    }
18    pub fn init(self) {
19        if self.cli == "yarn" {
20            self.yarn_link();
21        }
22        if self.cli == "pnpm" {
23            self.pnpm_link();
24        }
25    }
26    pub fn npm_link(cli: String, crates: Vec<String>) {
27        os_cli(cli, [&["link".into()], &crates[..]].concat(), get_root());
28        print(RswInfo::CrateLink("npm link".into(), crates.join(" ")));
29    }
30
31    pub fn yarn_link(&self) {
32        // register package
33        // 1. cd <root>/<name>
34        // 2. yarn link
35        os_cli(self.cli.clone(), ["link".into()].to_vec(), &self.cwd);
36
37        // yarn link <name>
38        os_cli(
39            self.cli.clone(),
40            ["link".into(), self.name.clone()].to_vec(),
41            get_root(),
42        );
43
44        print(RswInfo::CrateLink(
45            "yarn link".into(),
46            self.name.to_string(),
47        ));
48    }
49
50    pub fn pnpm_link(&self) {
51        // pnpm link --dir <root_path>
52        let dir = get_root().to_string_lossy().to_string();
53        os_cli(
54            self.cli.clone(),
55            ["link".into(), "--dir".into(), dir].to_vec(),
56            &self.cwd,
57        );
58
59        print(RswInfo::CrateLink(
60            "pnpm link".into(),
61            self.name.to_string(),
62        ));
63    }
64
65    pub fn unlink(cli: &String, crates: Vec<String>) {
66        let root = get_root();
67
68        // <yarn|pnpm> unlink foo bar
69        os_cli(
70            cli.clone(),
71            [&["unlink".into()], &crates[..]].concat(),
72            &root,
73        );
74
75        if cli == "npm" {
76            // npm unlink -g foo bar
77            os_cli(
78                cli.clone(),
79                [&["unlink".into(), "-g".into()], &crates[..]].concat(),
80                root,
81            );
82        }
83
84        print(RswInfo::Clean(format!("{} unlink", cli), crates.join(" ")));
85    }
86}