aube 1.29.0

Aube — a fast Node.js package manager
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
use std::ffi::OsString;

/// Strip pnpm-style generic `--config.<key>[=<value>]` flags out of the
/// argv before clap sees them. Returns the parsed `(key, value)` pairs
/// in the order they appeared so the last one wins on duplicates. The
/// supported forms are:
///
///   --config.<key>            -> ("<key>", "true")
///   --config.<key>=<value>    -> ("<key>", "<value>")
///
/// `--config.<key> <value>` (space-separated) is NOT consumed: a stray
/// positional after a bool-form switch could shadow a real argument
/// (e.g. `aube add --config.foo lodash`), and the `=` form is what
/// pnpm's docs use anyway. Anything after a bare `--` separator is
/// left untouched so user-supplied positional args containing the
/// literal `--config.` prefix still pass through.
pub(crate) fn extract_config_overrides(args: &mut Vec<OsString>) -> Vec<(String, String)> {
    let mut out = Vec::new();
    let mut i = 1;
    while i < args.len() {
        let Some(s) = args[i].to_str() else {
            i += 1;
            continue;
        };
        if s == "--" {
            break;
        }
        if let Some(rest) = s.strip_prefix("--config.") {
            let (key, value) = match rest.split_once('=') {
                Some((k, v)) => (k.to_string(), v.to_string()),
                None => (rest.to_string(), "true".to_string()),
            };
            if !key.is_empty() {
                out.push((key, value));
                args.remove(i);
                continue;
            }
        }
        i += 1;
    }
    out
}

/// Inspect `argv[0]` and, when invoked as a multicall shim (`aubr`, `aubx`),
/// rewrite the argv so clap sees the equivalent `aube run …` / `aube dlx …`.
pub(crate) fn rewrite_multicall_argv(mut args: Vec<OsString>) -> Vec<OsString> {
    normalize_npm_interpreter_shim_argv(&mut args);
    let Some(argv0) = args.first() else {
        return args;
    };
    let stem = crate::tool_shims::stem_of_argv0(argv0);
    let rewritten = match stem.as_str() {
        "aubr" => rewrite_simple_multicall(args, "run"),
        "aubx" => rewrite_simple_multicall(args, "dlx"),
        "node" => rewrite_tool_to_subcommand(args, "node"),
        "npx" | "pnpx" => rewrite_dlx_tool_argv(args),
        "npm" => rewrite_npm_argv(args),
        "pnpm" => rewrite_pnpm_argv(args),
        "yarn" | "yarnpkg" => rewrite_yarn_argv(args),
        _ => args,
    };
    protect_node_subcommand_args(rewritten)
}

fn rewrite_simple_multicall(mut args: Vec<OsString>, subcommand: &str) -> Vec<OsString> {
    args[0] = OsString::from("aube");
    // `--version` / `-V` belong to the top-level `aube` command; `run` and
    // `dlx` don't accept them, and for `dlx` the bare word would be parsed
    // as a package name and trigger a registry lookup.
    if matches!(
        args.get(1).and_then(|s| s.to_str()),
        Some("--version") | Some("-V")
    ) {
        return args;
    }
    args.insert(1, OsString::from(subcommand));
    args
}

fn rewrite_tool_to_subcommand(mut args: Vec<OsString>, subcommand: &str) -> Vec<OsString> {
    args[0] = OsString::from("aube");
    args.insert(1, OsString::from(subcommand));
    args
}

fn rewrite_dlx_tool_argv(mut args: Vec<OsString>) -> Vec<OsString> {
    args[0] = OsString::from("aube");
    if rewrite_pm_help_or_version(&mut args) {
        return args;
    }
    args.insert(1, OsString::from("dlx"));
    args
}

fn protect_node_subcommand_args(mut args: Vec<OsString>) -> Vec<OsString> {
    if args.get(1).and_then(|s| s.to_str()) != Some("node") {
        return args;
    }
    if args.len() <= 2 || args.get(2).and_then(|s| s.to_str()) == Some("--") {
        return args;
    }
    args.insert(2, OsString::from("--"));
    args
}

fn rewrite_pnpm_argv(mut args: Vec<OsString>) -> Vec<OsString> {
    args[0] = OsString::from("aube");
    rewrite_pm_help_or_version(&mut args);
    args
}

fn rewrite_npm_argv(mut args: Vec<OsString>) -> Vec<OsString> {
    args[0] = OsString::from("aube");
    if rewrite_pm_help_or_version(&mut args) {
        return args;
    }
    let Some(cmd) = args.get(1).and_then(|s| s.to_str()) else {
        return args;
    };
    match cmd {
        "i" | "install" => {
            args.remove(1);
            let subcommand = if npm_install_has_package_specs(&args[1..]) {
                "add"
            } else {
                "install"
            };
            args.insert(1, OsString::from(subcommand));
            args
        }
        "ci" => replace_arg(&mut args, 1, "ci"),
        "exec" => replace_arg(&mut args, 1, "exec"),
        "remove" | "rm" | "uninstall" | "un" => replace_arg(&mut args, 1, "remove"),
        "run" => replace_arg(&mut args, 1, "run"),
        "restart" => replace_arg(&mut args, 1, "restart"),
        "start" => replace_arg(&mut args, 1, "start"),
        "stop" => replace_arg(&mut args, 1, "stop"),
        "test" | "t" => replace_arg(&mut args, 1, "test"),
        _ => args,
    }
}

fn rewrite_yarn_argv(mut args: Vec<OsString>) -> Vec<OsString> {
    args[0] = OsString::from("aube");
    if rewrite_pm_help_or_version(&mut args) {
        return args;
    }
    let Some(cmd) = args.get(1).and_then(|s| s.to_str()) else {
        args.insert(1, OsString::from("install"));
        return args;
    };
    match cmd {
        "add" => replace_arg(&mut args, 1, "add"),
        "dlx" => replace_arg(&mut args, 1, "dlx"),
        "exec" => replace_arg(&mut args, 1, "exec"),
        "install" => replace_arg(&mut args, 1, "install"),
        "remove" => replace_arg(&mut args, 1, "remove"),
        "run" => replace_arg(&mut args, 1, "run"),
        _ => args,
    }
}

fn replace_arg(args: &mut [OsString], idx: usize, value: &str) -> Vec<OsString> {
    args[idx] = OsString::from(value);
    args.to_vec()
}

fn rewrite_pm_help_or_version(args: &mut [OsString]) -> bool {
    match args.get(1).and_then(|s| s.to_str()) {
        Some("--help") | Some("-h") if args.len() == 2 => {
            args[1] = OsString::from("--help");
            true
        }
        Some("--version") | Some("-v") | Some("-V") if args.len() == 2 => {
            args[1] = OsString::from("--version");
            true
        }
        _ => false,
    }
}

fn npm_install_has_package_specs(args: &[OsString]) -> bool {
    let mut skip_value = false;
    let mut after_separator = false;
    for arg in args {
        if after_separator {
            return true;
        }
        let Some(s) = arg.to_str() else {
            continue;
        };
        if skip_value {
            skip_value = false;
            continue;
        }
        if s == "--" {
            after_separator = true;
            continue;
        }
        if let Some(flag) = s.strip_prefix("--") {
            let (name, has_inline_value) = match flag.split_once('=') {
                Some((name, _)) => (name, true),
                None => (flag, false),
            };
            if !has_inline_value && npm_install_flag_takes_value(name) {
                skip_value = true;
            }
            continue;
        }
        if s.starts_with('-') && s != "-" {
            if matches!(s, "-C" | "-F" | "-w") {
                skip_value = true;
            }
            continue;
        }
        return true;
    }
    false
}

fn npm_install_flag_takes_value(name: &str) -> bool {
    matches!(
        name,
        "config"
            | "before"
            | "cache"
            | "cpu"
            | "dir"
            | "filter"
            | "filter-prod"
            | "fetch-retries"
            | "fetch-retry-factor"
            | "fetch-retry-maxtimeout"
            | "fetch-retry-mintimeout"
            | "fetch-timeout"
            | "lockfile-dir"
            | "loglevel"
            | "network-concurrency"
            | "node-linker"
            | "omit"
            | "os"
            | "package-import-method"
            | "prefix"
            | "public-hoist-pattern"
            | "registry"
            | "reporter"
            | "resolution-mode"
            | "tag"
            | "userconfig"
            | "workspace"
    ) || name.starts_with("config.")
}

/// npm's Windows `.cmd` shim can only execute extensioned native binaries.
/// When npm invokes `aube.exe bin/aube ...`, drop the shebang file and use
/// it as argv[0] so multicall dispatch still sees `aubr` / `aubx`.
fn normalize_npm_interpreter_shim_argv(args: &mut Vec<OsString>) {
    let Some(shim) = args.get(1).cloned() else {
        return;
    };
    let shim_path = std::path::Path::new(&shim);
    let Some(stem) = shim_path.file_stem().and_then(|s| s.to_str()) else {
        return;
    };
    if !matches!(stem, "aube" | "aubr" | "aubx") {
        return;
    }
    let Ok(bytes) = std::fs::read(shim_path) else {
        return;
    };
    if !bytes.starts_with(b"#!") {
        return;
    }
    args[0] = shim;
    args.remove(1);
}

/// pnpm-compat: shift flag tokens that used to be `global = true` on
/// `Cli` past the subcommand so `aube --frozen-lockfile install`,
/// `aube --registry=URL install`, etc. keep parsing after those flags
/// moved into per-command Args groups.
pub(crate) fn lift_per_subcommand_flags(mut args: Vec<OsString>) -> Vec<OsString> {
    // (long_name_without_dashes, takes_value)
    const LIFTED_LONGS: &[(&str, bool)] = &[
        ("frozen-lockfile", false),
        ("no-frozen-lockfile", false),
        ("prefer-frozen-lockfile", false),
        ("registry", true),
        ("fetch-retries", true),
        ("fetch-retry-factor", true),
        ("fetch-retry-maxtimeout", true),
        ("fetch-retry-mintimeout", true),
        ("fetch-timeout", true),
        ("disable-global-virtual-store", false),
        ("disable-gvs", false),
        ("enable-global-virtual-store", false),
        ("enable-gvs", false),
    ];
    // Long-form Cli flags that still live on `Cli` *and* take a value.
    // We must skip past `flag value` pairs so the value isn't mistaken
    // for the subcommand. Bool flags need no entry here.
    const KEPT_LONGS_WITH_VALUE: &[&str] = &[
        "dir",
        "cd",
        "prefix",
        "loglevel",
        "reporter",
        "filter",
        "filter-prod",
    ];
    const KEPT_SHORTS_WITH_VALUE: &[&str] = &["-C", "-F"];

    // True when the token at `args[idx]` looks like another flag rather
    // than a free-form value. Used to avoid eating the next flag as the
    // current flag's value when the user wrote `--dir --frozen-lockfile
    // install` (omitting the `--dir` value); without this guard we'd
    // silently consume `--frozen-lockfile` as a directory name and
    // `--frozen-lockfile` would never get lifted past the subcommand.
    let token_looks_like_flag = |args: &[OsString], idx: usize| -> bool {
        args.get(idx)
            .and_then(|t| t.to_str())
            .is_some_and(|s| s.starts_with('-') && s != "-")
    };

    let mut lifted: Vec<OsString> = Vec::new();
    let mut subcommand_idx: Option<usize> = None;
    let mut i = 1;
    while i < args.len() {
        let Some(s) = args[i].to_str() else { break };
        if s == "--" {
            break;
        }
        if let Some(rest) = s.strip_prefix("--") {
            let (bare, has_inline_value) = match rest.split_once('=') {
                Some((bare, _)) => (bare, true),
                None => (rest, false),
            };
            if let Some((_, takes_value)) =
                LIFTED_LONGS.iter().copied().find(|(name, _)| *name == bare)
            {
                lifted.push(args.remove(i));
                if takes_value
                    && !has_inline_value
                    && i < args.len()
                    && !token_looks_like_flag(&args, i)
                {
                    lifted.push(args.remove(i));
                }
                continue;
            }
            if KEPT_LONGS_WITH_VALUE.contains(&bare) {
                i += 1;
                if !has_inline_value && i < args.len() && !token_looks_like_flag(&args, i) {
                    i += 1;
                }
                continue;
            }
            i += 1;
            continue;
        }
        if s == "-" {
            subcommand_idx = Some(i);
            break;
        }
        if s.strip_prefix('-').is_some() {
            if s == "-F" {
                i += 1;
                if i < args.len() && !token_looks_like_flag(&args, i) {
                    i += 1;
                }
                continue;
            }
            if let Some(rest) = s.strip_prefix("-F")
                && !rest.is_empty()
            {
                i += 1;
                continue;
            }
            if KEPT_SHORTS_WITH_VALUE.contains(&s) {
                i += 1;
                if i < args.len() && !token_looks_like_flag(&args, i) {
                    i += 1;
                }
                continue;
            }
            i += 1;
            continue;
        }
        subcommand_idx = Some(i);
        break;
    }
    if let Some(idx) = subcommand_idx {
        let insert_at = idx + 1;
        for (j, tok) in lifted.into_iter().enumerate() {
            args.insert(insert_at + j, tok);
        }
    } else {
        // No subcommand found — restore the lifted tokens at their
        // original front position so clap's error message still
        // mentions them in argv order.
        for tok in lifted.into_iter().rev() {
            args.insert(1, tok);
        }
    }
    args
}