aube 1.38.1

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
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
//! `aube access` — manage package visibility and team access on an npm registry.

use crate::commands::{make_client, split_name_spec};
use clap::{Args, Subcommand};
use miette::{IntoDiagnostic, miette};

#[derive(Debug, Args)]
pub struct AccessArgs {
    #[command(subcommand)]
    pub command: AccessCommand,
    /// Emit registry responses as JSON when the subcommand has a result.
    #[arg(long)]
    pub json: bool,
    /// One-time password from a 2FA authenticator; sent as `npm-otp`.
    #[arg(long)]
    pub otp: Option<String>,
    #[command(flatten)]
    pub network: crate::cli_args::NetworkArgs,
}

#[derive(Debug, Subcommand)]
pub enum AccessCommand {
    /// Get package visibility status.
    Get {
        #[command(subcommand)]
        command: AccessGetCommand,
    },
    /// Grant a team read-only or read-write access to a package.
    #[command(override_usage = "access grant <PERMISSIONS> <TEAM> <PACKAGE>")]
    Grant {
        /// `read-only` or `read-write`.
        permissions: String,
        /// Team in `@scope:team` form.
        team: String,
        /// Package name.
        package: String,
    },
    /// List packages visible to a user, organization, or team.
    List {
        #[command(subcommand)]
        command: AccessListCommand,
    },
    /// Alias for `list packages`.
    #[command(override_usage = "access ls [ENTITY]\n       access ls packages [ENTITY]")]
    Ls {
        /// User, `@organization`, or `@scope:team`. Also accepts pnpm's
        /// `packages [ENTITY]` compatibility form. Accepted forms are
        /// `aube access ls [ENTITY]` and `aube access ls packages [ENTITY]`.
        #[arg(num_args = 0..=2)]
        entities: Vec<String>,
    },
    /// Revoke a team's access to a package.
    Revoke {
        /// Team in `@scope:team` form.
        team: String,
        /// Package name.
        package: String,
    },
    /// Set package visibility or a publish MFA requirement.
    Set {
        /// `status=public|private|restricted` or `mfa=none|publish|automation`.
        setting: String,
        /// Package name.
        package: String,
    },
}

#[derive(Debug, Subcommand)]
pub enum AccessListCommand {
    /// List collaborators for a package, optionally filtering to one user.
    Collaborators {
        /// Package name.
        package: String,
        /// Optional user name.
        user: Option<String>,
    },
    /// List packages visible to the current user or an optional entity.
    Packages {
        /// User, `@organization`, or `@scope:team`.
        entity: Option<String>,
    },
}

#[derive(Debug, Subcommand)]
pub enum AccessGetCommand {
    /// Get a package's public or restricted status.
    Status {
        /// Package name.
        package: String,
    },
}

pub async fn run(args: AccessArgs) -> miette::Result<()> {
    args.network.install_overrides();
    let cwd = crate::dirs::project_root_or_cwd().unwrap_or_else(|_| std::path::PathBuf::from("."));
    let client = make_client(&cwd);

    match args.command {
        AccessCommand::List { command } => match command {
            AccessListCommand::Packages { entity } => {
                let value = client
                    .access_list_packages(entity.as_deref())
                    .await
                    .map_err(access_registry_error)?;
                emit_list_packages(&value, args.json)
            }
            AccessListCommand::Collaborators { package, user } => {
                let package = bare_package(&package)?;
                let value = client
                    .access_list_collaborators(&package, user.as_deref())
                    .await
                    .map_err(access_registry_error)?;
                emit_collaborators(&value, args.json)
            }
        },
        AccessCommand::Ls { entities } => {
            let entity = match entities.as_slice() {
                [] => None,
                [entity] if entity == "packages" => None,
                [entity] => Some(entity.as_str()),
                [marker, entity] if marker == "packages" => Some(entity.as_str()),
                [marker, _] => {
                    return Err(miette!(
                        code = aube_codes::errors::ERR_AUBE_ACCESS_INVALID_ARGUMENT,
                        "expected `packages` before the entity in `access ls`, got {marker:?}"
                    ));
                }
                _ => {
                    return Err(miette!(
                        code = aube_codes::errors::ERR_AUBE_ACCESS_INVALID_ARGUMENT,
                        "`access ls` accepts at most an optional `packages` marker and one entity"
                    ));
                }
            };
            let value = client
                .access_list_packages(entity)
                .await
                .map_err(access_registry_error)?;
            emit_list_packages(&value, args.json)
        }
        AccessCommand::Get { command } => match command {
            AccessGetCommand::Status { package } => {
                let package = bare_package(&package)?;
                let value = client
                    .access_get_status(&package)
                    .await
                    .map_err(access_registry_error)?;
                emit_status(&package, &value, args.json)
            }
        },
        AccessCommand::Set { setting, package } => {
            let package = bare_package(&package)?;
            let (kind, value) = setting.split_once('=').ok_or_else(|| {
                miette!(
                    code = aube_codes::errors::ERR_AUBE_ACCESS_INVALID_ARGUMENT,
                    "expected `status=…` or `mfa=…`, got {setting:?}"
                )
            })?;
            match kind {
                "status" => {
                    if !package.starts_with('@') {
                        return Err(miette!(
                            code = aube_codes::errors::ERR_AUBE_ACCESS_INVALID_ARGUMENT,
                            "access status can only be changed for scoped packages"
                        ));
                    }
                    let access = match value {
                        "public" => "public",
                        "private" | "restricted" => "restricted",
                        _ => {
                            return Err(miette!(
                                code = aube_codes::errors::ERR_AUBE_ACCESS_INVALID_ARGUMENT,
                                "invalid access status {value:?}; expected `public`, `private`, or `restricted`"
                            ));
                        }
                    };
                    client
                        .access_set_status(&package, access, args.otp.as_deref())
                        .await
                        .map_err(access_registry_error)?;
                    emit_mutation(
                        args.json,
                        serde_json::json!({ "package": package, "access": access }),
                        format!("{package}: {access}"),
                    )
                }
                "mfa" => {
                    let publish_requires_tfa = match value {
                        "none" => false,
                        "publish" | "automation" => true,
                        _ => {
                            return Err(miette!(
                                code = aube_codes::errors::ERR_AUBE_ACCESS_INVALID_ARGUMENT,
                                "invalid MFA level {value:?}; expected `none`, `publish`, or `automation`"
                            ));
                        }
                    };
                    client
                        .access_set_mfa(&package, publish_requires_tfa, args.otp.as_deref())
                        .await
                        .map_err(access_registry_error)?;
                    emit_mutation(
                        args.json,
                        serde_json::json!({ "package": package, "mfa": value }),
                        format!("{package}: mfa={value}"),
                    )
                }
                _ => Err(miette!(
                    code = aube_codes::errors::ERR_AUBE_ACCESS_INVALID_ARGUMENT,
                    "unknown access setting {kind:?}; expected `status` or `mfa`"
                )),
            }
        }
        AccessCommand::Grant {
            permissions,
            team,
            package,
        } => {
            let package = bare_package(&package)?;
            if !matches!(permissions.as_str(), "read-only" | "read-write") {
                return Err(miette!(
                    code = aube_codes::errors::ERR_AUBE_ACCESS_INVALID_ARGUMENT,
                    "invalid permissions {permissions:?}; expected `read-only` or `read-write`"
                ));
            }
            let (scope, team_name) = split_team(&team)?;
            client
                .access_grant(
                    &package,
                    scope,
                    team_name,
                    &permissions,
                    args.otp.as_deref(),
                )
                .await
                .map_err(access_registry_error)?;
            emit_mutation(
                args.json,
                serde_json::json!({ "package": package, "team": team, "permissions": permissions }),
                format!("+{team} ({permissions}): {package}"),
            )
        }
        AccessCommand::Revoke { team, package } => {
            let package = bare_package(&package)?;
            let (scope, team_name) = split_team(&team)?;
            client
                .access_revoke(&package, scope, team_name, args.otp.as_deref())
                .await
                .map_err(access_registry_error)?;
            emit_mutation(
                args.json,
                serde_json::json!({ "package": package, "team": team }),
                format!("-{team}: {package}"),
            )
        }
    }
}

fn bare_package(raw: &str) -> miette::Result<String> {
    let (name, version) = split_name_spec(raw);
    if version.is_some() || aube_store::validate_and_encode_name(name).is_none() {
        return Err(miette!(
            code = aube_codes::errors::ERR_AUBE_INVALID_PACKAGE_NAME,
            "expected a valid bare package name, got {raw:?}"
        ));
    }
    Ok(name.to_string())
}

fn split_team(raw: &str) -> miette::Result<(&str, &str)> {
    let Some((scope, team)) = raw.split_once(':') else {
        return Err(miette!(
            code = aube_codes::errors::ERR_AUBE_ACCESS_INVALID_ARGUMENT,
            "expected team in `@scope:team` form, got {raw:?}"
        ));
    };
    if scope
        .strip_prefix('@')
        .is_none_or(|scope| scope.is_empty() || scope.contains('@'))
        || team.is_empty()
        || team.contains(':')
    {
        return Err(miette!(
            code = aube_codes::errors::ERR_AUBE_ACCESS_INVALID_ARGUMENT,
            "expected team in `@scope:team` form, got {raw:?}"
        ));
    }
    Ok((scope, team))
}

fn access_registry_error(error: aube_registry::Error) -> miette::Report {
    match error {
        aube_registry::Error::NotFound(name) => miette!(
            code = aube_codes::errors::ERR_AUBE_PACKAGE_NOT_FOUND,
            "package not found: {name}"
        ),
        aube_registry::Error::AccessEntityNotFound(entity) => miette!(
            code = aube_codes::errors::ERR_AUBE_ACCESS_ENTITY_NOT_FOUND,
            "access entity not found: {entity}"
        ),
        aube_registry::Error::Unauthorized => miette!(
            code = aube_codes::errors::ERR_AUBE_UNAUTHORIZED,
            "authentication required\nhelp: run `{}` first, then retry",
            aube_util::cmd("login")
        ),
        aube_registry::Error::RegistryWrite { status, body } => miette!(
            code = aube_codes::errors::ERR_AUBE_REGISTRY_WRITE_REJECTED,
            "registry rejected access request: HTTP {status}: {body}"
        ),
        other => miette!(
            code = aube_codes::errors::ERR_AUBE_REGISTRY_ERROR,
            "{other}"
        ),
    }
}

fn emit_list_packages(value: &serde_json::Value, json: bool) -> miette::Result<()> {
    if json {
        return emit_json(value);
    }
    let object = value.as_object().ok_or_else(|| {
        miette!(
            code = aube_codes::errors::ERR_AUBE_REGISTRY_ERROR,
            "registry returned an invalid package access list"
        )
    })?;
    let mut lines: Vec<String> = object
        .iter()
        .map(|(name, access)| match access.as_str() {
            Some(access) => format!("{name}: {access}"),
            None => name.clone(),
        })
        .collect();
    lines.sort();
    for line in lines {
        println!("{line}");
    }
    Ok(())
}

fn emit_collaborators(value: &serde_json::Value, json: bool) -> miette::Result<()> {
    if json {
        return emit_json(value);
    }
    for line in format_collaborators(value)? {
        println!("{line}");
    }
    Ok(())
}

fn format_collaborators(value: &serde_json::Value) -> miette::Result<Vec<String>> {
    let collaborators = value.as_object().ok_or_else(|| {
        miette!(
            code = aube_codes::errors::ERR_AUBE_REGISTRY_ERROR,
            "registry returned an invalid collaborator list"
        )
    })?;
    let mut lines: Vec<String> = collaborators
        .iter()
        .map(|(user, permissions)| match permissions.as_str() {
            Some(permissions) => format!("{user}: {permissions}"),
            None => user.clone(),
        })
        .collect();
    lines.sort();
    Ok(lines)
}

fn emit_status(package: &str, value: &serde_json::Value, json: bool) -> miette::Result<()> {
    if json {
        return emit_json(value);
    }
    let access = value
        .get("access")
        .and_then(serde_json::Value::as_str)
        .unwrap_or("public");
    println!("package: {package}");
    println!("access: {access}");
    Ok(())
}

fn emit_mutation(json: bool, value: serde_json::Value, text: String) -> miette::Result<()> {
    if json {
        emit_json(&value)
    } else {
        println!("{text}");
        Ok(())
    }
}

fn emit_json(value: &serde_json::Value) -> miette::Result<()> {
    println!("{}", serde_json::to_string_pretty(value).into_diagnostic()?);
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::{AccessArgs, AccessCommand, bare_package, format_collaborators, split_team};
    use clap::Parser;

    #[test]
    fn bare_package_rejects_specifiers() {
        assert!(bare_package("@scope/pkg").is_ok());
        assert!(bare_package("@scope/pkg@1.0.0").is_err());
        assert!(bare_package("../../tmp/pkg").is_err());
    }

    #[test]
    fn split_team_requires_one_scope_separator() {
        assert_eq!(
            split_team("@scope:developers").unwrap(),
            ("@scope", "developers")
        );
        assert!(split_team("@scope").is_err());
        assert!(split_team("@scope:dev:ops").is_err());
        assert!(split_team("scope:developers").is_err());
        assert!(split_team("@@scope:developers").is_err());
    }

    #[test]
    fn ls_accepts_the_pnpm_packages_marker() {
        let cli = crate::Cli::try_parse_from(["aube", "access", "ls", "packages", "@scope"])
            .expect("access ls packages should parse");
        let Some(crate::Commands::Access(AccessArgs {
            command: AccessCommand::Ls { entities },
            ..
        })) = cli.command
        else {
            panic!("expected access ls command");
        };
        assert_eq!(entities, ["packages", "@scope"]);
    }

    #[test]
    fn collaborator_map_formats_sorted_permissions() {
        let collaborators = serde_json::json!({
            "zoe": "read-only",
            "amy": "read-write",
        });
        assert_eq!(
            format_collaborators(&collaborators).unwrap(),
            ["amy: read-write", "zoe: read-only"]
        );
    }
}