npm-run-scripts 1.0.1

Fast interactive TUI for running npm scripts
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
//! Script execution.
//!
//! Handles running scripts with the appropriate package manager,
//! including single script execution, batch execution, and dry-run mode.

use std::io::{self, Write};
use std::path::Path;
use std::process::{Command, ExitStatus};

use anyhow::{Context, Result};

use crate::package::{Runner, Script};

/// Exit code when interrupted by Ctrl+C (SIGINT).
/// On Unix, this is 128 + signal number (SIGINT = 2).
pub const EXIT_CODE_INTERRUPTED: i32 = 130;

/// Result of script execution.
#[derive(Debug)]
pub struct ExecutionResult {
    /// Exit status of the script.
    pub status: ExitStatus,
    /// The command that was executed.
    pub command: String,
}

impl ExecutionResult {
    /// Check if the execution was successful.
    pub fn success(&self) -> bool {
        self.status.success()
    }

    /// Get the exit code.
    pub fn code(&self) -> Option<i32> {
        self.status.code()
    }
}

/// Run a single script with the given runner.
///
/// # Arguments
///
/// * `runner` - The package manager to use
/// * `script` - The script to run
/// * `args` - Optional additional arguments to pass to the script
/// * `dry_run` - If true, print the command without executing
///
/// # Returns
///
/// Returns the exit code of the script (0 for success, non-zero for failure).
/// Returns 130 if interrupted by Ctrl+C.
///
/// # Errors
///
/// Returns an error if the script fails to spawn.
pub fn run_script(
    runner: Runner,
    script: &Script,
    args: Option<&str>,
    dry_run: bool,
) -> Result<i32> {
    let args_vec: Vec<String> = args
        .map(|a| shell_words::split(a).unwrap_or_else(|_| vec![a.to_string()]))
        .unwrap_or_default();

    let project_dir = std::env::current_dir().context("Failed to get current directory")?;

    execute_script(runner, script.name(), &args_vec, &project_dir, dry_run)
        .map(|result| result.code().unwrap_or(EXIT_CODE_INTERRUPTED))
}

/// Run a single script in a specific directory.
///
/// # Arguments
///
/// * `runner` - The package manager to use
/// * `script` - The script to run
/// * `args` - Optional additional arguments to pass to the script
/// * `project_dir` - The project directory to run in
/// * `dry_run` - If true, print the command without executing
///
/// # Returns
///
/// Returns the exit code of the script (0 for success, non-zero for failure).
/// Returns 130 if interrupted by Ctrl+C.
///
/// # Errors
///
/// Returns an error if the script fails to spawn.
pub fn run_script_in_dir(
    runner: Runner,
    script: &Script,
    args: Option<&str>,
    project_dir: &Path,
    dry_run: bool,
) -> Result<i32> {
    let args_vec: Vec<String> = args
        .map(|a| shell_words::split(a).unwrap_or_else(|_| vec![a.to_string()]))
        .unwrap_or_default();

    execute_script(runner, script.name(), &args_vec, project_dir, dry_run)
        .map(|result| result.code().unwrap_or(EXIT_CODE_INTERRUPTED))
}

/// Run multiple scripts sequentially.
///
/// Scripts are run in order. Execution stops on the first failure.
/// Progress is printed to stdout: "Running 1/3: dev..."
///
/// # Arguments
///
/// * `runner` - The package manager to use
/// * `scripts` - Scripts to run with their optional arguments
/// * `dry_run` - If true, print the commands without executing
///
/// # Returns
///
/// Returns a vector of exit codes for each script that was executed.
/// If a script fails, the vector will contain exit codes up to and including
/// the failed script.
///
/// # Errors
///
/// Returns an error if any script fails to spawn.
pub fn run_scripts(
    runner: Runner,
    scripts: &[(&Script, Option<String>)],
    dry_run: bool,
) -> Result<Vec<i32>> {
    let total = scripts.len();
    let mut results = Vec::with_capacity(total);
    let project_dir = std::env::current_dir().context("Failed to get current directory")?;

    for (i, (script, args)) in scripts.iter().enumerate() {
        // Print progress
        println!(
            "\n\x1b[1;36mRunning {}/{}: {}...\x1b[0m",
            i + 1,
            total,
            script.name()
        );
        io::stdout().flush().ok();

        let args_vec: Vec<String> = args
            .as_ref()
            .map(|a| shell_words::split(a).unwrap_or_else(|_| vec![a.to_string()]))
            .unwrap_or_default();

        let result = execute_script(runner, script.name(), &args_vec, &project_dir, dry_run)?;
        let exit_code = result.code().unwrap_or(EXIT_CODE_INTERRUPTED);
        results.push(exit_code);

        // Stop on first failure (non-zero exit code)
        if exit_code != 0 {
            println!(
                "\n\x1b[1;31mScript '{}' failed with exit code {}\x1b[0m",
                script.name(),
                exit_code
            );
            break;
        }
    }

    Ok(results)
}

/// Run multiple scripts sequentially in a specific directory.
///
/// Scripts are run in order. Execution stops on the first failure.
/// Progress is printed to stdout: "Running 1/3: dev..."
///
/// # Arguments
///
/// * `runner` - The package manager to use
/// * `scripts` - Scripts to run with their optional arguments
/// * `project_dir` - The project directory to run in
/// * `dry_run` - If true, print the commands without executing
///
/// # Returns
///
/// Returns a vector of exit codes for each script that was executed.
/// If a script fails, the vector will contain exit codes up to and including
/// the failed script.
///
/// # Errors
///
/// Returns an error if any script fails to spawn.
pub fn run_scripts_in_dir(
    runner: Runner,
    scripts: &[(&Script, Option<String>)],
    project_dir: &Path,
    dry_run: bool,
) -> Result<Vec<i32>> {
    let total = scripts.len();
    let mut results = Vec::with_capacity(total);

    for (i, (script, args)) in scripts.iter().enumerate() {
        // Print progress
        println!(
            "\n\x1b[1;36mRunning {}/{}: {}...\x1b[0m",
            i + 1,
            total,
            script.name()
        );
        io::stdout().flush().ok();

        let args_vec: Vec<String> = args
            .as_ref()
            .map(|a| shell_words::split(a).unwrap_or_else(|_| vec![a.to_string()]))
            .unwrap_or_default();

        let result = execute_script(runner, script.name(), &args_vec, project_dir, dry_run)?;
        let exit_code = result.code().unwrap_or(EXIT_CODE_INTERRUPTED);
        results.push(exit_code);

        // Stop on first failure (non-zero exit code)
        if exit_code != 0 {
            println!(
                "\n\x1b[1;31mScript '{}' failed with exit code {}\x1b[0m",
                script.name(),
                exit_code
            );
            break;
        }
    }

    Ok(results)
}

/// Execute a script with the given runner.
///
/// This is the low-level execution function that spawns the process.
///
/// # Arguments
///
/// * `runner` - The package manager to use
/// * `script` - The script name to run
/// * `args` - Additional arguments to pass to the script
/// * `project_dir` - The project directory to run in
/// * `dry_run` - If true, print the command without executing
///
/// # Errors
///
/// Returns an error if the script fails to execute.
pub fn execute_script(
    runner: Runner,
    script: &str,
    args: &[String],
    project_dir: &Path,
    dry_run: bool,
) -> Result<ExecutionResult> {
    let cmd_parts = runner.run_command_with_args(script, args);
    let command_str = cmd_parts.join(" ");

    if dry_run {
        println!("Would run: {command_str}");
        return Ok(ExecutionResult {
            status: std::process::ExitStatus::default(),
            command: command_str,
        });
    }

    let mut command = Command::new(&cmd_parts[0]);
    command.args(&cmd_parts[1..]);
    command.current_dir(project_dir);

    // Inherit stdio for interactive scripts
    command.stdin(std::process::Stdio::inherit());
    command.stdout(std::process::Stdio::inherit());
    command.stderr(std::process::Stdio::inherit());

    let status = command
        .status()
        .with_context(|| format!("Failed to execute: {command_str}"))?;

    Ok(ExecutionResult {
        status,
        command: command_str,
    })
}

/// Format a command for display in dry-run mode.
pub fn format_dry_run_command(runner: Runner, script: &str, args: Option<&str>) -> String {
    let args_vec: Vec<String> = args
        .map(|a| shell_words::split(a).unwrap_or_else(|_| vec![a.to_string()]))
        .unwrap_or_default();

    let cmd = runner.run_command_with_args(script, &args_vec);
    format!("Would run: {}", cmd.join(" "))
}

/// Execute a workspace script with the given runner.
///
/// This runs a script in a specific workspace package from the monorepo root.
///
/// # Arguments
///
/// * `runner` - The package manager to use
/// * `workspace` - The workspace name (package name)
/// * `script` - The script name to run
/// * `args` - Additional arguments to pass to the script
/// * `project_dir` - The project directory (monorepo root) to run in
/// * `dry_run` - If true, print the command without executing
///
/// # Errors
///
/// Returns an error if the script fails to execute.
pub fn execute_workspace_script(
    runner: Runner,
    workspace: &str,
    script: &str,
    args: &[String],
    project_dir: &Path,
    dry_run: bool,
) -> Result<ExecutionResult> {
    let cmd_parts = runner.workspace_command_with_args(workspace, script, args);
    let command_str = cmd_parts.join(" ");

    if dry_run {
        println!("Would run: {command_str}");
        return Ok(ExecutionResult {
            status: std::process::ExitStatus::default(),
            command: command_str,
        });
    }

    let mut command = Command::new(&cmd_parts[0]);
    command.args(&cmd_parts[1..]);
    command.current_dir(project_dir);

    // Inherit stdio for interactive scripts
    command.stdin(std::process::Stdio::inherit());
    command.stdout(std::process::Stdio::inherit());
    command.stderr(std::process::Stdio::inherit());

    let status = command
        .status()
        .with_context(|| format!("Failed to execute: {command_str}"))?;

    Ok(ExecutionResult {
        status,
        command: command_str,
    })
}

/// Run a workspace script.
///
/// # Arguments
///
/// * `runner` - The package manager to use
/// * `workspace` - The workspace name (package name)
/// * `script` - The script to run
/// * `args` - Optional additional arguments to pass to the script
/// * `project_dir` - The project directory (monorepo root) to run in
/// * `dry_run` - If true, print the command without executing
///
/// # Returns
///
/// Returns the exit code of the script (0 for success, non-zero for failure).
pub fn run_workspace_script(
    runner: Runner,
    workspace: &str,
    script: &Script,
    args: Option<&str>,
    project_dir: &Path,
    dry_run: bool,
) -> Result<i32> {
    let args_vec: Vec<String> = args
        .map(|a| shell_words::split(a).unwrap_or_else(|_| vec![a.to_string()]))
        .unwrap_or_default();

    execute_workspace_script(
        runner,
        workspace,
        script.name(),
        &args_vec,
        project_dir,
        dry_run,
    )
    .map(|result| result.code().unwrap_or(EXIT_CODE_INTERRUPTED))
}

/// Format a workspace command for display in dry-run mode.
pub fn format_workspace_dry_run_command(
    runner: Runner,
    workspace: &str,
    script: &str,
    args: Option<&str>,
) -> String {
    let args_vec: Vec<String> = args
        .map(|a| shell_words::split(a).unwrap_or_else(|_| vec![a.to_string()]))
        .unwrap_or_default();

    let cmd = runner.workspace_command_with_args(workspace, script, &args_vec);
    format!("Would run: {}", cmd.join(" "))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_dry_run() {
        let result = execute_script(Runner::Npm, "test", &[], Path::new("."), true).unwrap();

        assert_eq!(result.command, "npm run test");
    }

    #[test]
    fn test_dry_run_with_args() {
        let args = vec!["--watch".to_string(), "--coverage".to_string()];
        let result = execute_script(Runner::Npm, "test", &args, Path::new("."), true).unwrap();

        assert_eq!(result.command, "npm run test -- --watch --coverage");
    }

    #[test]
    fn test_format_dry_run_command() {
        assert_eq!(
            format_dry_run_command(Runner::Npm, "dev", None),
            "Would run: npm run dev"
        );

        assert_eq!(
            format_dry_run_command(Runner::Npm, "dev", Some("--host")),
            "Would run: npm run dev -- --host"
        );

        assert_eq!(
            format_dry_run_command(Runner::Yarn, "dev", Some("--host")),
            "Would run: yarn dev --host"
        );

        assert_eq!(
            format_dry_run_command(Runner::Pnpm, "dev", Some("--host")),
            "Would run: pnpm dev -- --host"
        );

        assert_eq!(
            format_dry_run_command(Runner::Bun, "dev", Some("--host")),
            "Would run: bun run dev --host"
        );
    }

    #[test]
    fn test_run_script_dry_run() {
        let script = Script::new("dev", "vite");
        let result = run_script(Runner::Npm, &script, None, true).unwrap();
        assert_eq!(result, 0);
    }

    #[test]
    fn test_run_script_with_args_dry_run() {
        let script = Script::new("dev", "vite");
        let result = run_script(Runner::Npm, &script, Some("--host"), true).unwrap();
        assert_eq!(result, 0);
    }

    #[test]
    fn test_run_scripts_dry_run() {
        let script1 = Script::new("build", "vite build");
        let script2 = Script::new("test", "vitest");
        let scripts: Vec<(&Script, Option<String>)> =
            vec![(&script1, None), (&script2, Some("--coverage".to_string()))];

        let results = run_scripts(Runner::Npm, &scripts, true).unwrap();
        assert_eq!(results, vec![0, 0]);
    }
}