-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
whoami.ts
54 lines (42 loc) Β· 2.15 KB
/
whoami.ts
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
import {Configuration, Ident, MessageName} from '@berry/core';
import {PluginConfiguration, StreamReport, structUtils} from '@berry/core';
import {npmHttpUtils} from '@berry/plugin-npm';
import {Clipanion} from 'clipanion';
import {Readable, Writable} from 'stream';
import { PortablePath } from '@berry/fslib';
// eslint-disable-next-line arca/no-default-export
export default (clipanion: Clipanion, pluginConfiguration: PluginConfiguration) => clipanion
.command(`npm whoami [-s,--scope SCOPE]`)
.categorize(`Npm-related commands`)
.describe(`display the name of the authenticated user`)
.detail(`
Print the username associated with the current authentication settings to the standard output.
When using \`-s,--scope\`, the username printed will be the one that matches the authentication for the specific scope (they can be overriden using the \`npmScopes\` settings).
`)
.example(
`Print username for the default registry`,
`yarn npm whoami`,
)
.example(
`Print username for the registry on a given scope`,
`yarn npm whoami --scope company`,
)
.action(async ({cwd, stdin, stdout, scope}: {cwd: PortablePath, stdin: Readable, stdout: Writable, scope: string}) => {
const configuration = await Configuration.find(cwd, pluginConfiguration);
const report = await StreamReport.start({configuration, stdout}, async report => {
let ident: Ident | null = null;
if (scope)
ident = structUtils.makeIdent(scope, ``);
try {
const response = await npmHttpUtils.get(`/-/whoami`, { configuration, ident, authType: npmHttpUtils.AuthType.ALWAYS_AUTH, json: true });
report.reportInfo(MessageName.UNNAMED, response.username);
} catch (err) {
if (err.statusCode === 401 || err.statusCode === 403) {
report.reportError(MessageName.AUTHENTICATION_INVALID, `Authentication failed - your credentials may have expired`);
} else {
report.reportError(MessageName.AUTHENTICATION_INVALID, err.toString());
}
}
});
return report.exitCode();
});