From a26e0219ebd559932e7bd3acb204df7b77530731 Mon Sep 17 00:00:00 2001 From: nexryai Date: Fri, 12 Jul 2024 14:43:58 +0900 Subject: [PATCH] Add files --- .gitignore | 5 ++++ .idea/.gitignore | 5 ++++ .idea/modules.xml | 8 +++++++ .idea/npmrun.iml | 11 +++++++++ .idea/vcs.xml | 6 +++++ Cargo.toml | 8 +++++++ src/main.rs | 61 +++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 104 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/modules.xml create mode 100644 .idea/npmrun.iml create mode 100644 .idea/vcs.xml create mode 100644 Cargo.toml create mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore index 6985cf1..196e176 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,8 @@ Cargo.lock # MSVC Windows builds of rustc generate these, which store debugging information *.pdb + + +# Added by cargo + +/target diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..b58b603 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,5 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..74800b9 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/npmrun.iml b/.idea/npmrun.iml new file mode 100644 index 0000000..cf84ae4 --- /dev/null +++ b/.idea/npmrun.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..5e4ac4d --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "npmrun" +version = "0.1.0" +edition = "2021" + +[dependencies] +serde = { version = "1.0.204", features = ["derive"] } +serde_json = "1.0.120" diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..c61bf6f --- /dev/null +++ b/src/main.rs @@ -0,0 +1,61 @@ +use std::env; +use std::fs::File; +use std::io::BufReader; +use serde::{Deserialize}; + +#[derive(Debug, Deserialize)] +struct PackageJson { + scripts: Scripts, +} + +type Scripts = std::collections::HashMap; + +fn exec(command: &str) { + println!(" > EXEC: {}", command); + + use std::process::Command; + let mut parts = command.split_whitespace(); + let command = parts.next().unwrap(); + let args = parts; + let status = Command::new(command) + .args(args) + .status() + .expect("Failed to execute command"); + if !status.success() { + eprintln!("Command failed with exit code: {}", status); + std::process::exit(1); + } +} + +// コマンドを&&でつないで実行する +fn exec_commands(commands: &str) { + println!("Running commands: {}", commands); + + for command in commands.split("&&") { + exec(command.trim()); + } +} + +fn main() { + const FILENAME: &str = "package.json"; + + // ファイルを開いてJSONをパースする + let file = File::open(FILENAME).expect("Failed to open file"); + let reader = BufReader::new(file); + let package_json: PackageJson = serde_json::from_reader(reader).expect("Failed to parse JSON"); + + // コマンドライン引数から指定されたスクリプト名を取得 + let args: Vec = env::args().collect(); + if args.len() < 2 { + eprintln!("Usage: {} ", args[0]); + std::process::exit(1); + } + + let script_name = &args[1]; // 第2引数をスクリプト名とする + + // スクリプトを実行 + match package_json.scripts.get(script_name) { + Some(script) => exec_commands(script), + None => std::process::exit(1), + } +}