-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String, String>; | ||
|
||
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<String> = env::args().collect(); | ||
if args.len() < 2 { | ||
eprintln!("Usage: {} <filename>", 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), | ||
} | ||
} |