Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions crates/vp_migration/src/eslint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,29 @@ mod tests {
rewrite_eslint_script("cross-env NODE_ENV=test CI=true eslint --cache ."),
"cross-env NODE_ENV=test CI=true vp lint ."
);

assert_eq!(
rewrite_eslint_script("cross-env NODE_ENV=test bunx --bun eslint --cache ."),
"cross-env NODE_ENV=test bunx --bun vp lint ."
);
assert_eq!(
rewrite_eslint_script("cross-env NODE_ENV=test echo -- bunx --bun eslint --cache ."),
"cross-env NODE_ENV=test echo -- bunx --bun eslint --cache ."
);
assert_eq!(
rewrite_eslint_script("cross-env-shell CI=true pnpm exec bunx --bun eslint ."),
"cross-env-shell CI=true pnpm exec bunx --bun vp lint ."
);

// eslint passed as an argument to another command under cross-env stays untouched
assert_eq!(
rewrite_eslint_script("cross-env NODE_ENV=test vitest run eslint"),
"cross-env NODE_ENV=test vitest run eslint"
);
assert_eq!(
rewrite_eslint_script("cross-env NODE_ENV=test echo eslint --cache"),
"cross-env NODE_ENV=test echo eslint --cache"
);
}

#[test]
Expand Down
6 changes: 6 additions & 0 deletions crates/vp_migration/src/prettier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,12 @@ mod tests {
rewrite_prettier_script("cross-env NODE_ENV=test CI=true prettier --write --cache ."),
"cross-env NODE_ENV=test CI=true vp fmt ."
);

// prettier passed as an argument to another command under cross-env stays untouched
assert_eq!(
rewrite_prettier_script("cross-env NODE_ENV=test echo prettier --write"),
"cross-env NODE_ENV=test echo prettier --write"
);
}

#[test]
Expand Down
108 changes: 42 additions & 66 deletions crates/vp_migration/src/script_rewrite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,25 +46,15 @@ pub(crate) fn rewrite_pack_flags(script: &str) -> String {

fn rewrite_direct_pack_flags(script: &str) -> String {
rewrite_simple_commands(script, |cmd| {
let words = collect_command_words(cmd);
let start = match words.first().map(|word| word.value.as_str()) {
Some("vp") => 0,
Some("cross-env" | "cross-env-shell") => {
let Some(index) = words.iter().skip(1).position(|word| !word.value.contains('='))
else {
return false;
};
index + 1
}
_ => return false,
let words = collect_launched_command_words(cmd);
let [command, subcommand, args @ ..] = words.as_slice() else {
return false;
};
if words.get(start).is_none_or(|word| word.value != "vp")
|| words.get(start + 1).is_none_or(|word| word.value != "pack")
{
if command.value != "vp" || subcommand.value != "pack" {
return false;
}
let mut changed = false;
for word in words.iter().skip(start + 2) {
for word in args {
if word.value == "--" {
break;
}
Expand Down Expand Up @@ -247,17 +237,32 @@ struct BunxInvocation {
target_suffix_index: usize,
}

fn collect_command_words(cmd: &ast::SimpleCommand) -> Vec<CommandWord> {
fn cross_env_command_index(suffix: &ast::CommandSuffix) -> Option<usize> {
suffix.0.iter().position(
|item| matches!(item, ast::CommandPrefixOrSuffixItem::Word(w) if !w.value.contains('=')),
)
}

fn collect_launched_command_words(cmd: &ast::SimpleCommand) -> Vec<CommandWord> {
let mut words = Vec::new();
if let Some(name) = &cmd.word_or_name {
let Some(name) = &cmd.word_or_name else {
return words;
};
let mut start = 0;
if matches!(name.value.as_str(), "cross-env" | "cross-env-shell") {
let Some(index) = cmd.suffix.as_ref().and_then(cross_env_command_index) else {
return words;
};
start = index;
} else {
words.push(CommandWord {
position: CommandWordPosition::Name,
ordinal: 0,
value: name.value.clone(),
});
}
if let Some(suffix) = &cmd.suffix {
for (index, item) in suffix.0.iter().enumerate() {
for (index, item) in suffix.0.iter().enumerate().skip(start) {
if let ast::CommandPrefixOrSuffixItem::Word(word) = item {
words.push(CommandWord {
position: CommandWordPosition::Suffix(index),
Expand Down Expand Up @@ -290,7 +295,7 @@ fn bunx_target(words: &[CommandWord], start: usize) -> Option<usize> {
}

/// Commands that launch a following command, either env-injection proxies
/// (`dotenv`, `cross-env`, `portless`, ...) or package runners (`npm`, `pnpm`,
/// (`dotenv`, `portless`, ...) or package runners (`npm`, `pnpm`,
/// `yarn`, ...). Only when the command name is one of these does a trailing
/// `--`/`run`/`exec` separator turn a subsequent `bunx` into a launcher whose
/// inner command should be rewritten.
Expand All @@ -300,19 +305,9 @@ fn bunx_target(words: &[CommandWord], start: usize) -> Option<usize> {
/// their arguments are never corrupted by the rewrite.
const KNOWN_RUNNER_WRAPPERS: &[&str] = &[
// Package runners.
"npm",
"pnpm",
"yarn",
"yarnpkg",
"bun",
"npx",
"pnpx",
"npm", "pnpm", "yarn", "yarnpkg", "bun", "npx", "pnpx",
// Env-injection / proxy wrappers.
"cross-env",
"cross-env-shell",
"dotenv",
"dotenvx",
"portless",
"dotenv", "dotenvx", "portless",
];

/// Whether `name` is a command that launches a following command (see
Expand All @@ -322,7 +317,7 @@ fn is_known_runner_wrapper(name: &str) -> bool {
}

fn find_bunx_invocations(cmd: &ast::SimpleCommand) -> Vec<BunxInvocation> {
let words = collect_command_words(cmd);
let words = collect_launched_command_words(cmd);
let mut invocations = Vec::new();

for start in 0..words.len() {
Expand All @@ -333,33 +328,13 @@ fn find_bunx_invocations(cmd: &ast::SimpleCommand) -> Vec<BunxInvocation> {
continue;
};

let allowed_position = match words[start].position {
// `bunx` is itself the command being run.
CommandWordPosition::Name => true,
// `bunx` appears as an argument: only treat it as a launcher when a
// recognized wrapper command (`words[0]`) hands off to it via a
// `--`/`run`/`exec` separator. A bare `--`/`run`/`exec` on a
// non-wrapper command (e.g. `echo -- bunx ...`) is literal text.
CommandWordPosition::Suffix(runner_index) => {
let preceded_by_separator = cmd
.suffix
.as_ref()
.and_then(|suffix| runner_index.checked_sub(1).and_then(|i| suffix.0.get(i)))
.is_some_and(|item| {
matches!(
item,
ast::CommandPrefixOrSuffixItem::Word(word)
if matches!(word.value.as_str(), "--" | "run" | "exec")
)
});
let command_is_wrapper = cmd
.word_or_name
.as_ref()
.is_some_and(|name| is_known_runner_wrapper(&name.value));
preceded_by_separator && command_is_wrapper
}
};
if allowed_position {
// Only a known launcher can hand off to bunx after a separator.
// The ordinals keep a redirection between the two from looking adjacent.
let after_separator = start > 0
&& is_known_runner_wrapper(&words[0].value)
&& words[start - 1].ordinal + 1 == words[start].ordinal
&& matches!(words[start - 1].value.as_str(), "--" | "run" | "exec");
if start == 0 || after_separator {
invocations.push(BunxInvocation { target_suffix_index });
}
}
Expand Down Expand Up @@ -475,16 +450,17 @@ fn rewrite_in_cross_env(cmd: &mut ast::SimpleCommand, config: &ScriptRewriteConf
None => return false,
};

let source_idx = suffix.0.iter().position(|item| {
matches!(item, ast::CommandPrefixOrSuffixItem::Word(w) if w.value == config.source_command)
});
let Some(idx) = source_idx else {
let Some(idx) = cross_env_command_index(suffix) else {
return false;
};

if let ast::CommandPrefixOrSuffixItem::Word(w) = &mut suffix.0[idx] {
w.value = "vp".to_owned();
let ast::CommandPrefixOrSuffixItem::Word(w) = &mut suffix.0[idx] else {
return false;
};
if w.value != config.source_command {
return false;
}

w.value = "vp".to_owned();
suffix.0.insert(idx + 1, make_suffix_word(config.target_subcommand));

strip_flags_from_suffix(cmd, idx + 2, config);
Expand Down