Skip to content

Commit 9d04923

Browse files
committed
feat: 处理 package.json
1 parent 98ee21b commit 9d04923

4 files changed

Lines changed: 62 additions & 13 deletions

File tree

index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
/* auto-generated by NAPI-RS */
55

66
export function validateConfig(configStr: string): void
7+
export function validatePackage(appPath: string, nodeModulesPath: string): void

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ if (!nativeBinding) {
252252
throw new Error(`Failed to load native binding`)
253253
}
254254

255-
const { validateConfig } = nativeBinding
255+
const { validateConfig, validatePackage } = nativeBinding
256256

257257
module.exports.validateConfig = validateConfig
258+
module.exports.validatePackage = validatePackage

src/lib.rs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ mod validators;
44

55
use std::{ fs, error::Error, path::PathBuf, env };
66

7-
use validators::{ config::ConfigValidator, common::Validator };
8-
9-
use crate::validators::message::{ Message, self };
7+
use crate::validators::{ message::{ Message, self }, package::PackageValidator, config::ConfigValidator, common::Validator };
108

119
#[macro_use]
1210
extern crate napi_derive;
@@ -20,8 +18,11 @@ pub fn validate_config(config_str: String) {
2018
}
2119

2220
#[napi]
23-
pub fn validate_package(package_path: String) {
24-
21+
pub fn validate_package(app_path: String, node_modules_path: String) {
22+
let result = validate_package_core(app_path, node_modules_path);
23+
if let Err(e) = result {
24+
println!("{}", Message { kind: message::MessageKind::Error, content: e.to_string() });
25+
}
2526
}
2627

2728
fn validate_config_core(config_str: String) -> Result<(), Box<dyn Error>> {
@@ -58,6 +59,17 @@ fn validate_config_core(config_str: String) -> Result<(), Box<dyn Error>> {
5859
Ok(())
5960
}
6061

61-
fn validate_package_core() -> Result<(), Box<dyn Error>> {
62-
62+
fn validate_package_core(app_path: String, node_modules_path: String) -> Result<(), Box<dyn Error>> {
63+
let tip = Message {
64+
kind: validators::message::MessageKind::Info,
65+
content: String::from("开始进行项目依赖安装正确性验证!")
66+
};
67+
println!("{}", tip);
68+
let mut path = PathBuf::new();
69+
path.push(app_path);
70+
path.push("package.json");
71+
let package_str = fs::read_to_string(path.as_path())?;
72+
let package_validator_result = PackageValidator::build(&package_str, &node_modules_path).unwrap();
73+
package_validator_result.get_taro_packages();
74+
Ok(())
6375
}

src/validators/package.rs

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,47 @@
1-
use super::{ message::{ Message, MessageKind }, common::Validator };
1+
use std::{ path::Path };
2+
3+
use jsonschema::ValidationError;
4+
use serde_json::{ self, Value, from_str };
25

3-
struct PackageValidator {
6+
use super::{ message::{ Message, MessageKind }, common::Validator };
47

8+
pub struct PackageValidator<'a>{
9+
pub json: Value,
10+
pub node_modules_path: &'a str
511
}
612

7-
impl PackageValidator {
13+
impl<'a> PackageValidator<'a> {
14+
pub fn build(package_str: &str, node_modules_path: &'a str) -> Result<Self, ValidationError<'static>> {
15+
let package_json = from_str(package_str)?;
16+
Ok(Self { json: package_json, node_modules_path })
17+
}
818

9-
}
19+
pub fn get_taro_packages(&self) {
20+
let dependencies = self.json.get("dependencies").unwrap();
21+
let dev_dependencies = self.json.get("devDependencies").unwrap();
22+
let mut taro_packages = vec![];
23+
if let Value::Object(dependencies_map) = dependencies {
24+
for (key, _) in dependencies_map {
25+
if key.contains("@tarojs/") {
26+
taro_packages.push(key);
27+
}
28+
}
29+
}
1030

11-
impl Validator for PackageValidator {
31+
if let Value::Object(dev_dependencies_map) = dev_dependencies {
32+
for (key, _) in dev_dependencies_map {
33+
if key.contains("@tarojs/") {
34+
taro_packages.push(key);
35+
}
36+
}
37+
}
38+
39+
println!("{:?}", taro_packages);
40+
}
1241
}
42+
43+
// impl Validator for PackageValidator {
44+
// fn validate(&self) -> Vec<Message> {
45+
46+
// }
47+
// }

0 commit comments

Comments
 (0)