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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
//! Script parsing from package.json.

use std::path::Path;

use anyhow::{bail, Context, Result};

use super::descriptions::extract_descriptions;
use super::types::{Package, Script, Scripts};

/// Parse a package.json file from a directory.
///
/// # Arguments
///
/// * `project_dir` - The directory containing package.json
///
/// # Errors
///
/// Returns an error if:
/// - The package.json file cannot be read
/// - The JSON is malformed
pub fn parse_scripts(project_dir: &Path) -> Result<Scripts> {
    let package_json = project_dir.join("package.json");
    let content = std::fs::read_to_string(&package_json)
        .with_context(|| format!("Failed to read {}", package_json.display()))?;

    parse_scripts_from_json(&content)
}

/// Parse the full package.json structure.
///
/// # Arguments
///
/// * `content` - The raw JSON content
///
/// # Errors
///
/// Returns an error if the JSON is malformed.
pub fn parse_package_json(content: &str) -> Result<Package> {
    // First, try to parse as valid JSON
    let json: serde_json::Value = serde_json::from_str(content).map_err(|e| {
        let msg = format_json_error(content, &e);
        anyhow::anyhow!("Failed to parse package.json: {msg}")
    })?;

    // Then deserialize into our Package struct
    let package: Package =
        serde_json::from_value(json).context("Failed to parse package.json structure")?;

    Ok(package)
}

/// Parse scripts from package.json content.
///
/// # Arguments
///
/// * `content` - The raw JSON content
///
/// # Errors
///
/// Returns an error if the JSON is malformed.
///
/// # Examples
///
/// ```
/// use npm_run_scripts::package::scripts::parse_scripts_from_json;
///
/// let json = r#"{"scripts": {"dev": "vite", "build": "vite build"}}"#;
/// let scripts = parse_scripts_from_json(json).unwrap();
/// assert_eq!(scripts.len(), 2);
/// ```
pub fn parse_scripts_from_json(content: &str) -> Result<Scripts> {
    let package = parse_package_json(content)?;
    extract_scripts_from_package(&package)
}

/// Parse scripts from package.json content, returning error if no scripts exist.
///
/// # Errors
///
/// Returns an error if:
/// - The JSON is malformed
/// - No scripts are defined in package.json
pub fn parse_scripts_required(content: &str) -> Result<Scripts> {
    let scripts = parse_scripts_from_json(content)?;

    if scripts.is_empty() {
        bail!("No scripts defined in package.json");
    }

    Ok(scripts)
}

/// Extract scripts from a parsed Package.
pub fn extract_scripts_from_package(package: &Package) -> Result<Scripts> {
    // Extract descriptions from various sources
    let descriptions = extract_descriptions(package);

    let mut scripts = Scripts::new();

    for (name, command) in &package.scripts {
        // Skip comment entries (keys starting with //)
        if name.starts_with("//") {
            continue;
        }

        let mut script = Script::new(name, command);

        // Add description if available
        if let Some(desc) = descriptions.get(name) {
            script.set_description(desc);
        }

        scripts.add(script);
    }

    // Sort alphabetically for consistent ordering
    scripts.sort_alphabetically();

    Ok(scripts)
}

/// Format a JSON parsing error with context.
fn format_json_error(content: &str, error: &serde_json::Error) -> String {
    let line = error.line();
    let column = error.column();

    // Try to show the problematic line
    if let Some(error_line) = content.lines().nth(line.saturating_sub(1)) {
        let pointer = " ".repeat(column.saturating_sub(1)) + "^";
        format!(
            "{}\n  at line {}, column {}:\n    {}\n    {}",
            error, line, column, error_line, pointer
        )
    } else {
        format!("{} at line {}, column {}", error, line, column)
    }
}

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

    #[test]
    fn test_parse_basic_scripts() {
        let json = r#"{
            "name": "test-project",
            "scripts": {
                "dev": "vite",
                "build": "vite build"
            }
        }"#;

        let scripts = parse_scripts_from_json(json).unwrap();
        assert_eq!(scripts.len(), 2);
        assert!(scripts.get("dev").is_some());
        assert_eq!(scripts.get("dev").unwrap().command(), "vite");
    }

    #[test]
    fn test_parse_empty_scripts() {
        let json = r#"{
            "name": "test-project",
            "scripts": {}
        }"#;

        let scripts = parse_scripts_from_json(json).unwrap();
        assert!(scripts.is_empty());
    }

    #[test]
    fn test_parse_no_scripts_field() {
        let json = r#"{
            "name": "test-project"
        }"#;

        let scripts = parse_scripts_from_json(json).unwrap();
        assert!(scripts.is_empty());
    }

    #[test]
    fn test_parse_scripts_required_fails_when_empty() {
        let json = r#"{
            "name": "test-project",
            "scripts": {}
        }"#;

        let result = parse_scripts_required(json);
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("No scripts defined"));
    }

    #[test]
    fn test_parse_invalid_json() {
        let json = r#"{ invalid json }"#;

        let result = parse_scripts_from_json(json);
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(err.contains("Failed to parse"));
    }

    #[test]
    fn test_parse_skips_comment_entries() {
        let json = r#"{
            "scripts": {
                "//dev": "This is a comment",
                "dev": "vite",
                "// build": "Another comment",
                "build": "vite build"
            }
        }"#;

        let scripts = parse_scripts_from_json(json).unwrap();
        assert_eq!(scripts.len(), 2);
        assert!(scripts.get("dev").is_some());
        assert!(scripts.get("build").is_some());
        assert!(scripts.get("//dev").is_none());
    }

    #[test]
    fn test_parse_with_scripts_info_descriptions() {
        let json = r#"{
            "scripts": {
                "dev": "vite",
                "build": "vite build"
            },
            "scripts-info": {
                "dev": "Start development server",
                "build": "Build for production"
            }
        }"#;

        let scripts = parse_scripts_from_json(json).unwrap();
        assert_eq!(
            scripts.get("dev").unwrap().description(),
            Some("Start development server")
        );
        assert_eq!(
            scripts.get("build").unwrap().description(),
            Some("Build for production")
        );
    }

    #[test]
    fn test_parse_with_ntl_descriptions() {
        let json = r#"{
            "scripts": {
                "test": "vitest"
            },
            "ntl": {
                "descriptions": {
                    "test": "Run tests with vitest"
                }
            }
        }"#;

        let scripts = parse_scripts_from_json(json).unwrap();
        assert_eq!(
            scripts.get("test").unwrap().description(),
            Some("Run tests with vitest")
        );
    }

    #[test]
    fn test_parse_with_comment_descriptions() {
        let json = r#"{
            "scripts": {
                "//lint": "Run ESLint",
                "lint": "eslint ."
            }
        }"#;

        let scripts = parse_scripts_from_json(json).unwrap();
        assert_eq!(
            scripts.get("lint").unwrap().description(),
            Some("Run ESLint")
        );
    }

    #[test]
    fn test_parse_scripts_sorted_alphabetically() {
        let json = r#"{
            "scripts": {
                "zebra": "echo z",
                "alpha": "echo a",
                "middle": "echo m"
            }
        }"#;

        let scripts = parse_scripts_from_json(json).unwrap();
        let names: Vec<_> = scripts.iter().map(|s| s.name()).collect();
        assert_eq!(names, vec!["alpha", "middle", "zebra"]);
    }

    #[test]
    fn test_parse_package_json_full() {
        let json = r#"{
            "name": "my-app",
            "version": "1.0.0",
            "description": "A test application",
            "packageManager": "pnpm@8.0.0",
            "scripts": {
                "dev": "vite"
            }
        }"#;

        let package = parse_package_json(json).unwrap();
        assert_eq!(package.name, "my-app");
        assert_eq!(package.version, "1.0.0");
        assert_eq!(package.description, Some("A test application".to_string()));
        assert_eq!(package.package_manager, Some("pnpm@8.0.0".to_string()));
        assert!(package.has_scripts());
    }

    #[test]
    fn test_parse_special_characters_in_script_names() {
        let json = r#"{
            "scripts": {
                "build:prod": "vite build --mode production",
                "test:unit": "vitest",
                "lint:fix": "eslint --fix ."
            }
        }"#;

        let scripts = parse_scripts_from_json(json).unwrap();
        assert_eq!(scripts.len(), 3);
        assert!(scripts.get("build:prod").is_some());
        assert!(scripts.get("test:unit").is_some());
        assert!(scripts.get("lint:fix").is_some());
    }

    #[test]
    fn test_lifecycle_scripts_filtered() {
        let json = r#"{
            "scripts": {
                "dev": "vite",
                "preinstall": "echo preinstall",
                "postinstall": "husky install",
                "build": "vite build"
            }
        }"#;

        let scripts = parse_scripts_from_json(json).unwrap();
        assert_eq!(scripts.len(), 4);

        let filtered = scripts.without_lifecycle();
        assert_eq!(filtered.len(), 2);
        assert!(filtered.get("dev").is_some());
        assert!(filtered.get("build").is_some());
        assert!(filtered.get("preinstall").is_none());
        assert!(filtered.get("postinstall").is_none());
    }

    // ==================== Edge Case Tests ====================

    #[test]
    fn test_parse_unicode_script_names() {
        let json = r#"{
            "scripts": {
                "开发": "vite",
                "ビルド": "vite build",
                "тест": "vitest",
                "développement": "vite dev"
            }
        }"#;

        let scripts = parse_scripts_from_json(json).unwrap();
        assert_eq!(scripts.len(), 4);
        assert!(scripts.get("开发").is_some());
        assert!(scripts.get("ビルド").is_some());
        assert!(scripts.get("тест").is_some());
        assert!(scripts.get("développement").is_some());
    }

    #[test]
    fn test_parse_emoji_script_names() {
        let json = r#"{
            "scripts": {
                "🚀": "npm start",
                "🔧:fix": "eslint --fix .",
                "test:🎉": "vitest"
            }
        }"#;

        let scripts = parse_scripts_from_json(json).unwrap();
        assert_eq!(scripts.len(), 3);
        assert!(scripts.get("🚀").is_some());
        assert!(scripts.get("🔧:fix").is_some());
        assert!(scripts.get("test:🎉").is_some());
    }

    #[test]
    fn test_parse_empty_command() {
        let json = r#"{
            "scripts": {
                "empty": "",
                "normal": "echo hello"
            }
        }"#;

        let scripts = parse_scripts_from_json(json).unwrap();
        assert_eq!(scripts.len(), 2);
        assert_eq!(scripts.get("empty").unwrap().command(), "");
        assert_eq!(scripts.get("normal").unwrap().command(), "echo hello");
    }

    #[test]
    fn test_parse_very_long_script_name() {
        let long_name = "a".repeat(200);
        let json = format!(
            r#"{{
            "scripts": {{
                "{}": "echo test"
            }}
        }}"#,
            long_name
        );

        let scripts = parse_scripts_from_json(&json).unwrap();
        assert_eq!(scripts.len(), 1);
        assert!(scripts.get(&long_name).is_some());
    }

    #[test]
    fn test_parse_very_long_command() {
        let long_command = "echo ".to_string() + &"x".repeat(10000);
        let json = format!(
            r#"{{
            "scripts": {{
                "test": "{}"
            }}
        }}"#,
            long_command
        );

        let scripts = parse_scripts_from_json(&json).unwrap();
        assert_eq!(scripts.get("test").unwrap().command(), long_command);
    }

    #[test]
    fn test_parse_many_scripts() {
        // Generate a package.json with 1000 scripts
        let mut scripts_obj = String::from("{");
        for i in 0..1000 {
            if i > 0 {
                scripts_obj.push(',');
            }
            scripts_obj.push_str(&format!(r#""script_{}": "echo {}""#, i, i));
        }
        scripts_obj.push('}');

        let json = format!(r#"{{"scripts": {}}}"#, scripts_obj);

        let scripts = parse_scripts_from_json(&json).unwrap();
        assert_eq!(scripts.len(), 1000);
        assert!(scripts.get("script_0").is_some());
        assert!(scripts.get("script_999").is_some());
    }

    #[test]
    fn test_parse_special_characters_in_command() {
        let json = r#"{
            "scripts": {
                "test": "echo \"hello world\" && echo 'single quotes'",
                "env": "FOO=bar BAZ=\"quoted value\" npm start",
                "redirect": "npm build > output.log 2>&1",
                "pipe": "cat file.txt | grep pattern | wc -l",
                "subshell": "$(npm bin)/eslint .",
                "semicolon": "echo first; echo second",
                "escape": "echo \\\"escaped\\\"",
                "dollar": "echo $HOME $USER ${PWD}"
            }
        }"#;

        let scripts = parse_scripts_from_json(json).unwrap();
        assert_eq!(scripts.len(), 8);
        assert!(scripts.get("test").is_some());
        assert!(scripts.get("env").is_some());
        assert!(scripts.get("redirect").is_some());
        assert!(scripts.get("pipe").is_some());
        assert!(scripts.get("subshell").is_some());
        assert!(scripts.get("semicolon").is_some());
        assert!(scripts.get("escape").is_some());
        assert!(scripts.get("dollar").is_some());
    }

    #[test]
    fn test_parse_multiline_command() {
        // package.json doesn't support actual multiline strings, but escaped newlines
        let json = r#"{
            "scripts": {
                "complex": "echo start && npm test && npm build && echo done"
            }
        }"#;

        let scripts = parse_scripts_from_json(json).unwrap();
        assert!(scripts.get("complex").is_some());
    }

    #[test]
    fn test_parse_json_with_trailing_comma() {
        // Standard JSON doesn't allow trailing commas, verify we reject it
        let json = r#"{
            "scripts": {
                "dev": "vite",
            }
        }"#;

        let result = parse_scripts_from_json(json);
        assert!(result.is_err());
    }

    #[test]
    fn test_parse_json_with_comments() {
        // Standard JSON doesn't allow comments, verify we reject them
        let json = r#"{
            // This is a comment
            "scripts": {
                "dev": "vite"
            }
        }"#;

        let result = parse_scripts_from_json(json);
        assert!(result.is_err());
    }

    #[test]
    fn test_parse_minimal_valid_json() {
        let json = r#"{}"#;
        let scripts = parse_scripts_from_json(json).unwrap();
        assert!(scripts.is_empty());
    }

    #[test]
    fn test_parse_scripts_field_as_array() {
        // scripts should be an object, not an array
        let json = r#"{
            "scripts": ["dev", "build"]
        }"#;

        let result = parse_scripts_from_json(json);
        assert!(result.is_err());
    }

    #[test]
    fn test_parse_scripts_field_as_string() {
        // scripts should be an object, not a string
        let json = r#"{
            "scripts": "dev"
        }"#;

        let result = parse_scripts_from_json(json);
        assert!(result.is_err());
    }

    #[test]
    fn test_parse_scripts_field_as_null() {
        let json = r#"{
            "scripts": null
        }"#;

        let result = parse_scripts_from_json(json);
        assert!(result.is_err());
    }

    #[test]
    fn test_parse_script_value_as_number() {
        // Script values should be strings, not numbers
        let json = r#"{
            "scripts": {
                "test": 123
            }
        }"#;

        let result = parse_scripts_from_json(json);
        assert!(result.is_err());
    }

    #[test]
    fn test_parse_script_value_as_object() {
        // Script values should be strings, not objects
        let json = r#"{
            "scripts": {
                "test": {"command": "vitest"}
            }
        }"#;

        let result = parse_scripts_from_json(json);
        assert!(result.is_err());
    }

    #[test]
    fn test_format_json_error_shows_context() {
        let json = r#"{
    "scripts": {
        "dev": vite
    }
}"#;

        let result = parse_scripts_from_json(json);
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        // Should show line and column information
        assert!(err.contains("line"));
        assert!(err.contains("column"));
    }

    #[test]
    fn test_parse_whitespace_in_script_names() {
        // While unusual, whitespace in keys is valid JSON
        let json = r#"{
            "scripts": {
                " dev ": "vite",
                "build test": "vite build"
            }
        }"#;

        let scripts = parse_scripts_from_json(json).unwrap();
        assert_eq!(scripts.len(), 2);
        assert!(scripts.get(" dev ").is_some());
        assert!(scripts.get("build test").is_some());
    }

    #[test]
    fn test_parse_hyphen_and_underscore_names() {
        let json = r#"{
            "scripts": {
                "my-script": "echo hyphen",
                "my_script": "echo underscore",
                "my-long-script-name": "echo long",
                "__internal__": "echo internal"
            }
        }"#;

        let scripts = parse_scripts_from_json(json).unwrap();
        assert_eq!(scripts.len(), 4);
        assert!(scripts.get("my-script").is_some());
        assert!(scripts.get("my_script").is_some());
        assert!(scripts.get("my-long-script-name").is_some());
        assert!(scripts.get("__internal__").is_some());
    }
}