Skip to content

pkg

pkg 包用于解析前端依赖文件,包括package.json \ package-lock.json 。对于不同依赖管理器,则对应不同的配置文件。比如pnpm-lock.yaml \ yarn.lock.

开始中创建了rsup的配置文件config.toml,其中包含了pkg服务的配置。

toml
[pkg]
npm_registry = "https://registry.npmmirror.com"

设置依赖源地址,默认是npmmirror源,可以设置为其他源地址。可以查看如何修改配置

pkg接收一个命令行参数--dir,用于指定项目目录,默认是当前目录。

rs
#[derive(Parser, Debug)]
#[command(author,version,about,long_about = None)]
pub struct Args {
    #[arg(
        short,
        long,
        default_value = ".",
        help = "The path to the package.json file"
    )]
    pub dir: String,
}

PkgJson

pkg执行方法fn run(args: Args, package: Package) {...}读取指定目录下的package.json文件并解析为PkgJson数据结构

rs
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct PkgJson {
    pub name: Option<String>,
    pub absolute_path: String,
    pub version: Option<String>,
    pub description: Option<String>,
    pub scripts: Option<HashMap<String, String>>,
    pub dependencies: Option<HashMap<String, String>>,
    #[serde(rename = "devDependencies")]
    pub dev_dependencies: Option<HashMap<String, String>>,
}

需要读取dependenciesdev_dependencies的依赖项,读取依赖的最新版本信息,最终生成我们想要的结构Pkg

Pkg

当前项目的依赖信息,和PkgJson区别在于转换并增加了了一些字段,并且读取了dependenciesdev_dependencies中依赖的最新版本信息。

rs
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Pkg {
    pub path: String,
    pub name: Option<String>,
    pub version: Option<String>,
    pub description: Option<String>,
    pub scripts: HashMap<String, String>,
    // 当前项目的管理工具
    pub manager_name: Option<String>,
    pub dependencies: HashMap<String, PkgInfo>,
    pub dev_dependencies: HashMap<String, PkgInfo>,
}

PkgInfo

rs
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct PkgInfo {
    pub name: String,
    pub readme: Option<String>,
    pub version: Option<String>,
    pub description: Option<String>,
    pub homepage: Option<String>,
    pub keywords: Option<Vec<String>>,
    pub license: Option<String>,
    #[serde(rename = "dist-tags")]
    pub dist_tags: DistTags,
    pub versions: HashMap<String, VersionInfo>,
    #[serde(default)]
    pub is_dev: bool,
    #[serde(default)]
    pub is_finish: bool,
    #[serde(default)]
    pub is_del: bool,
}

DistTags

rs
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct DistTags {
    pub latest: String,
}

VersionInfo

依赖版本信息。

rs
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct VersionInfo {
    pub name: String,
    pub version: Option<String>,
    pub homepage: Option<String>,
    pub description: Option<String>,
    pub keywords: Option<Vec<String>>,
    pub author: Option<Author>,
    pub maintainers: Option<Vec<Memeber>>,
    pub dependencies: Option<HashMap<String, String>>,
    #[serde(rename = "devDependencies")]
    pub dev_dependencies: Option<HashMap<String, String>>,
    #[serde(rename = "peerDependencies")]
    pub peer_dependencies: Option<HashMap<String, String>>,
    pub dist: Option<Dist>,
}

Author

rs
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(untagged)]
pub enum Author {
    Memeber(Memeber),
    String(String),
}

Memeber

rs
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Memeber {
    pub name: String,
    pub email: Option<String>,
}

Dist

rs
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Dist {
    pub shasum: Option<String>,
    pub size: Option<usize>,
    pub tarball: Option<String>,
    pub integrity: Option<String>,
}

Released under the Apache License.