Skip to content

Commit

Permalink
Add files
Browse files Browse the repository at this point in the history
  • Loading branch information
nexryai committed Jul 12, 2024
1 parent 924bb6d commit a26e021
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,8 @@ Cargo.lock

# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb


# Added by cargo

/target
5 changes: 5 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions .idea/npmrun.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Cargo.toml
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"
61 changes: 61 additions & 0 deletions src/main.rs
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),
}
}

0 comments on commit a26e021

Please sign in to comment.