-
Notifications
You must be signed in to change notification settings - Fork 192
Expand file tree
/
Copy pathpackage.rs
More file actions
473 lines (444 loc) · 15.7 KB
/
package.rs
File metadata and controls
473 lines (444 loc) · 15.7 KB
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
use ast_grep_config::RuleConfig;
use ast_grep_language::SupportLang;
use serde_json::{Map, Value};
use vite_error::Error;
use crate::{ast_grep, eslint::rewrite_eslint_script};
// Marker to replace "cross-env " before ast-grep processing
// Using a fake env var assignment that won't match our rules
const CROSS_ENV_MARKER: &str = "__CROSS_ENV__=1 ";
const CROSS_ENV_REPLACEMENT: &str = "cross-env ";
/// rewrite a single script command string using rules
fn rewrite_script(script: &str, rules: &[RuleConfig<SupportLang>]) -> String {
// Only handle cross-env replacement if it's present in the script
let has_cross_env = script.contains(CROSS_ENV_REPLACEMENT);
// Step 1: Replace "cross-env " with marker so ast-grep can see the actual commands
let preprocessed = if has_cross_env {
script.replace(CROSS_ENV_REPLACEMENT, CROSS_ENV_MARKER)
} else {
script.to_string()
};
// Step 2: Process with ast-grep
let result = ast_grep::apply_loaded_rules(&preprocessed, rules);
// Step 3: Replace cross-env marker back with "cross-env " (only if we replaced it)
let result = if has_cross_env {
result.replace(CROSS_ENV_MARKER, CROSS_ENV_REPLACEMENT)
} else {
result
};
result
}
/// Transform all script strings in a JSON object using the provided function.
/// Handles both string values and arrays of strings (lint-staged format).
/// Returns the updated JSON if any scripts were modified, or None if unchanged.
fn transform_scripts_json(
scripts_json: &str,
mut transform_fn: impl FnMut(&str) -> String,
) -> Result<Option<String>, Error> {
let mut scripts: Map<String, Value> = serde_json::from_str(scripts_json)?;
let mut updated = false;
for value in scripts.values_mut() {
if value.is_array() {
// lint-staged scripts can be an array of strings
// https://github.com/lint-staged/lint-staged?tab=readme-ov-file#packagejson-example
if let Some(sub_scripts) = value.as_array_mut() {
for sub_script in sub_scripts.iter_mut() {
if sub_script.is_string()
&& let Some(raw_script) = sub_script.as_str()
{
let new_script = transform_fn(raw_script);
if new_script != raw_script {
updated = true;
*sub_script = Value::String(new_script);
}
}
}
}
} else if value.is_string() {
if let Some(raw_script) = value.as_str() {
let new_script = transform_fn(raw_script);
if new_script != raw_script {
updated = true;
*value = Value::String(new_script);
}
}
}
}
if updated {
let new_content = serde_json::to_string_pretty(&scripts)?;
Ok(Some(new_content))
} else {
Ok(None)
}
}
/// Rewrite ESLint scripts in JSON content: rename `eslint` → `vp lint` and strip ESLint-only flags.
///
/// Uses brush-parser to parse shell commands, so it correctly handles env var prefixes,
/// compound commands (`&&`, `||`, `|`), and quoted arguments.
pub fn rewrite_eslint(scripts_json: &str) -> Result<Option<String>, Error> {
transform_scripts_json(scripts_json, rewrite_eslint_script)
}
/// rewrite scripts json content using rules from rules_yaml
pub fn rewrite_scripts(scripts_json: &str, rules_yaml: &str) -> Result<Option<String>, Error> {
let rules = ast_grep::load_rules(rules_yaml)?;
transform_scripts_json(scripts_json, |raw_script| rewrite_script(raw_script, &rules))
}
#[cfg(test)]
mod tests {
use super::*;
const RULES_YAML: &str = r#"
# vite --version / vite -v => vp --version / vp -v (global flags, not dev-specific)
---
id: replace-vite-version
language: bash
rule:
kind: command_name
regex: '^vite$'
inside:
kind: command
regex: 'vite\s+(-v|--version)'
fix: vp
# vite => vp dev (handles all cases: with/without env var prefix and flag args)
# Match command_name to preserve env var prefix and arguments
# Excludes subcommands like "vite build", "vite test", etc.
---
id: replace-vite
language: bash
rule:
kind: command_name
regex: '^vite$'
inside:
kind: command
not:
# ignore non-flag arguments (subcommands like build, test, etc.)
regex: 'vite\s+[^-]'
fix: vp dev
# vite <subcommand> => vp <subcommand> (handles vite build, vite test, vite dev, etc.)
# Match command_name when followed by a subcommand, replace only the command name
---
id: replace-vite-subcommand
language: bash
rule:
kind: command_name
regex: '^vite$'
inside:
kind: command
regex: 'vite\s+[^-]'
fix: vp
# oxlint => vp lint (handles all cases: with/without env var prefix and args)
# Match command_name to preserve env var prefix and arguments
---
id: replace-oxlint
language: bash
rule:
kind: command_name
regex: '^oxlint$'
fix: vp lint
# oxfmt => vp fmt
---
id: replace-oxfmt
language: bash
rule:
kind: command_name
regex: '^oxfmt$'
fix: vp fmt
# vitest => vp test
---
id: replace-vitest
language: bash
rule:
kind: command_name
regex: '^vitest$'
fix: vp test
# tsdown => vp pack
---
id: replace-tsdown
language: bash
rule:
kind: command_name
regex: '^tsdown$'
fix: vp pack
"#;
#[test]
fn test_rewrite_script() {
let rules = ast_grep::load_rules(RULES_YAML).unwrap();
// vp commands should not be rewritten
assert_eq!(rewrite_script("vp dev", &rules), "vp dev");
assert_eq!(rewrite_script("vp build", &rules), "vp build");
assert_eq!(rewrite_script("vp test", &rules), "vp test");
assert_eq!(rewrite_script("vp lint", &rules), "vp lint");
assert_eq!(rewrite_script("vp fmt", &rules), "vp fmt");
assert_eq!(rewrite_script("vp pack", &rules), "vp pack");
assert_eq!(rewrite_script("vp dev --port 3000", &rules), "vp dev --port 3000");
// vite version flags (global, not dev-specific)
assert_eq!(rewrite_script("vite --version", &rules), "vp --version");
assert_eq!(rewrite_script("vite -v", &rules), "vp -v");
// vite commands
assert_eq!(rewrite_script("vite", &rules), "vp dev");
assert_eq!(rewrite_script("vite dev", &rules), "vp dev");
assert_eq!(rewrite_script("vite i", &rules), "vp i");
assert_eq!(rewrite_script("vite install", &rules), "vp install");
assert_eq!(rewrite_script("vite test", &rules), "vp test");
assert_eq!(rewrite_script("vite lint", &rules), "vp lint");
assert_eq!(rewrite_script("vite fmt", &rules), "vp fmt");
assert_eq!(rewrite_script("vite pack", &rules), "vp pack");
assert_eq!(rewrite_script("vite preview", &rules), "vp preview");
assert_eq!(rewrite_script("vite optimize", &rules), "vp optimize");
assert_eq!(rewrite_script("vite build -r", &rules), "vp build -r");
assert_eq!(rewrite_script("vite --port 3000", &rules), "vp dev --port 3000");
assert_eq!(
rewrite_script("vite --port 3000 --host 0.0.0.0 --open", &rules),
"vp dev --port 3000 --host 0.0.0.0 --open"
);
assert_eq!(
rewrite_script("vite --port 3000 || vite --port 3001", &rules),
"vp dev --port 3000 || vp dev --port 3001"
);
assert_eq!(
rewrite_script("npm run lint && vite --port 3000", &rules),
"npm run lint && vp dev --port 3000"
);
assert_eq!(
rewrite_script("vite --port 3000 && npm run lint", &rules),
"vp dev --port 3000 && npm run lint"
);
assert_eq!(
rewrite_script("vite && tsc --check && vite run -r build", &rules),
"vp dev && tsc --check && vp run -r build"
);
assert_eq!(
rewrite_script("vite && tsc --check && vite run test", &rules),
"vp dev && tsc --check && vp run test"
);
assert_eq!(
rewrite_script("vite && tsc --check && vite test", &rules),
"vp dev && tsc --check && vp test"
);
assert_eq!(
rewrite_script("prettier --write src/** vite", &rules),
"prettier --write src/** vite"
);
// complex examples
assert_eq!(
rewrite_script("if [ -f file.txt ]; then vite; fi", &rules),
"if [ -f file.txt ]; then vp dev; fi"
);
assert_eq!(
rewrite_script("if [ -f file.txt ]; then vite --port 3000; fi", &rules),
"if [ -f file.txt ]; then vp dev --port 3000; fi"
);
assert_eq!(
rewrite_script("if [ -f file.txt ]; then vite --port 3000 && npm run lint; fi", &rules,),
"if [ -f file.txt ]; then vp dev --port 3000 && npm run lint; fi"
);
assert_eq!(
rewrite_script(
"if [ -f file.txt ]; then vite dev --port 3000 && npm run lint; fi",
&rules,
),
"if [ -f file.txt ]; then vp dev --port 3000 && npm run lint; fi"
);
// env variable commands
assert_eq!(
rewrite_script("NODE_ENV=test VITE_CJS_IGNORE_WARNING=true vite", &rules),
"NODE_ENV=test VITE_CJS_IGNORE_WARNING=true vp dev"
);
assert_eq!(
rewrite_script("FOO=bar vite --port 3000", &rules),
"FOO=bar vp dev --port 3000"
);
// env variable with oxlint commands
assert_eq!(rewrite_script("DEBUG=1 oxlint", &rules), "DEBUG=1 vp lint");
assert_eq!(
rewrite_script("NODE_ENV=test oxlint --type-aware", &rules),
"NODE_ENV=test vp lint --type-aware"
);
// oxlint commands
assert_eq!(rewrite_script("oxlint", &rules), "vp lint");
assert_eq!(rewrite_script("oxlint --type-aware", &rules), "vp lint --type-aware");
assert_eq!(
rewrite_script("oxlint --type-aware --config .oxlintrc", &rules),
"vp lint --type-aware --config .oxlintrc"
);
assert_eq!(rewrite_script("oxlint && vite dev", &rules), "vp lint && vp dev");
assert_eq!(
rewrite_script("npm run type-check && oxlint --type-aware", &rules),
"npm run type-check && vp lint --type-aware"
);
// npx/pnpx/bunx eslint wrappers remain unchanged (no preprocessing)
assert_eq!(rewrite_script("npx eslint .", &rules), "npx eslint .");
assert_eq!(rewrite_script("npx eslint --fix .", &rules), "npx eslint --fix .");
assert_eq!(rewrite_script("pnpx eslint .", &rules), "pnpx eslint .");
assert_eq!(rewrite_script("bunx eslint .", &rules), "bunx eslint .");
assert_eq!(rewrite_script("pnpm exec eslint --fix .", &rules), "pnpm exec eslint --fix .");
assert_eq!(rewrite_script("yarn exec eslint --fix .", &rules), "yarn exec eslint --fix .");
// npx with non-eslint tools should NOT be affected
assert_eq!(rewrite_script("npx prettier .", &rules), "npx prettier .");
// npx eslint-plugin-foo should NOT match
assert_eq!(rewrite_script("npx eslint-plugin-foo", &rules), "npx eslint-plugin-foo");
// husky commands should NOT be rewritten by vite-tools rules
// (husky rule is in separate vite-prepare.yml, applied only to scripts.prepare)
assert_eq!(rewrite_script("husky", &rules), "husky");
assert_eq!(rewrite_script("husky install", &rules), "husky install");
assert_eq!(rewrite_script("husky || true", &rules), "husky || true");
}
#[test]
fn test_rewrite_package_json_scripts_success() {
let package_json_scripts = r#"
{
"dev": "vite"
}
"#;
let updated = rewrite_scripts(package_json_scripts, &RULES_YAML)
.expect("failed to rewrite package.json scripts");
assert!(updated.is_some());
assert_eq!(
updated.unwrap(),
r#"
{
"dev": "vp dev"
}
"#
.trim()
);
}
#[test]
fn test_rewrite_package_json_scripts_with_env_variable_success() {
let package_json_scripts = r#"
{
"dev:cjs": "VITE_CJS_IGNORE_WARNING=true vite",
"lint": "VITE_CJS_IGNORE_WARNING=true FOO=bar oxlint --fix"
}
"#;
let updated = rewrite_scripts(package_json_scripts, &RULES_YAML)
.expect("failed to rewrite package.json scripts");
assert!(updated.is_some());
assert_eq!(
updated.unwrap(),
r#"
{
"dev:cjs": "VITE_CJS_IGNORE_WARNING=true vp dev",
"lint": "VITE_CJS_IGNORE_WARNING=true FOO=bar vp lint --fix"
}
"#
.trim()
);
}
#[test]
fn test_rewrite_package_json_scripts_using_cross_env() {
let package_json_scripts = r#"
{
"dev:cjs": "cross-env VITE_CJS_IGNORE_WARNING=true vite && cross-env FOO=bar vitest run",
"lint": "cross-env VITE_CJS_IGNORE_WARNING=true FOO=bar oxlint --fix",
"test": "vite build && cross-env FOO=bar vitest run && echo ' cross-env test done ' || echo ' cross-env test failed '"
}
"#;
let updated = rewrite_scripts(package_json_scripts, &RULES_YAML)
.expect("failed to rewrite package.json scripts");
assert!(updated.is_some());
assert_eq!(
updated.unwrap(),
r#"
{
"dev:cjs": "cross-env VITE_CJS_IGNORE_WARNING=true vp dev && cross-env FOO=bar vp test run",
"lint": "cross-env VITE_CJS_IGNORE_WARNING=true FOO=bar vp lint --fix",
"test": "vp build && cross-env FOO=bar vp test run && echo ' cross-env test done ' || echo ' cross-env test failed '"
}
"#
.trim()
);
}
#[test]
fn test_rewrite_package_json_scripts_lint_staged() {
let package_json_scripts = r#"
{
"*.js": ["oxlint --fix --type-aware", "oxfmt --fix"],
"*.ts": "oxfmt --fix"
}
"#;
let updated = rewrite_scripts(package_json_scripts, &RULES_YAML)
.expect("failed to rewrite package.json scripts");
assert!(updated.is_some());
assert_eq!(
updated.unwrap(),
r#"
{
"*.js": [
"vp lint --fix --type-aware",
"vp fmt --fix"
],
"*.ts": "vp fmt --fix"
}
"#
.trim()
);
}
#[test]
fn test_rewrite_package_json_scripts_no_update() {
let package_json_scripts = r#"
{
"foo": "bar"
}
"#;
let updated = rewrite_scripts(package_json_scripts, &RULES_YAML)
.expect("failed to rewrite package.json scripts");
assert!(updated.is_none());
}
#[test]
fn test_rewrite_eslint_json() {
let scripts_json = r#"
{
"lint": "eslint --fix --ext .ts,.tsx .",
"lint:cached": "eslint --cache --fix .",
"build": "vite build"
}
"#;
let updated = rewrite_eslint(scripts_json).expect("failed to rewrite eslint");
assert!(updated.is_some());
assert_eq!(
updated.unwrap(),
r#"
{
"lint": "vp lint --fix .",
"lint:cached": "vp lint --fix .",
"build": "vite build"
}
"#
.trim()
);
}
#[test]
fn test_rewrite_eslint_json_no_update() {
let scripts_json = r#"
{
"lint": "vp lint --fix .",
"build": "vite build"
}
"#;
let updated = rewrite_eslint(scripts_json).expect("failed to rewrite eslint");
assert!(updated.is_none());
}
#[test]
fn test_rewrite_eslint_json_lint_staged_array() {
let scripts_json = r#"
{
"*.js": ["eslint --ext .ts --fix", "oxfmt --fix"],
"*.ts": "eslint --cache --fix"
}
"#;
let updated = rewrite_eslint(scripts_json).expect("failed to rewrite eslint");
assert!(updated.is_some());
assert_eq!(
updated.unwrap(),
r#"
{
"*.js": [
"vp lint --fix",
"oxfmt --fix"
],
"*.ts": "vp lint --fix"
}
"#
.trim()
);
}
}