-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathnpm-auth-refresh.mjs
More file actions
154 lines (137 loc) · 4.93 KB
/
Copy pathnpm-auth-refresh.mjs
File metadata and controls
154 lines (137 loc) · 4.93 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
import { spawnSync } from "node:child_process";
import { readFileSync, writeFileSync } from "node:fs";
import path from "node:path";
import { fileURLToPath, pathToFileURL } from "node:url";
export const azureFeedLocalRegistry =
"https://pkgs.dev.azure.com/devdiv/_packaging/copilot-canary@Local/npm/registry/";
export const cfsRegistry = "https://packagefeedproxy.microsoft.io/npm/";
export const credentialProviderRegistry =
"https://pkgs.dev.azure.com/artifacts-public/23934c1b-a3b5-4b70-9dd3-d1bef4cc72a0/_packaging/AzureArtifacts/npm/registry/";
export function getProjectNpmrcPaths(scriptUrl = import.meta.url) {
const repositoryRoot = path.resolve(path.dirname(fileURLToPath(scriptUrl)), "..");
return [
path.join(repositoryRoot, "nodejs", ".npmrc"),
path.join(repositoryRoot, "test", "harness", ".npmrc"),
path.join(repositoryRoot, "java", "scripts", "codegen", ".npmrc"),
];
}
export function buildProjectNpmConfig(existingConfig = "") {
const newline = existingConfig.includes("\r\n") ? "\r\n" : "\n";
const config = existingConfig.replace(
/^[ \t]*@github:registry[ \t]*=[^\r\n]*(?:\r?\n|$)/gm,
""
);
const separator = config.length > 0 && !config.endsWith("\n") ? newline : "";
return `${config}${separator}@github:registry=${azureFeedLocalRegistry}${newline}`;
}
export function writeProjectNpmConfigs(npmrcPaths) {
for (const npmrcPath of npmrcPaths) {
let existingConfig = "";
try {
existingConfig = readFileSync(npmrcPath, "utf8");
} catch (error) {
if (error.code !== "ENOENT") {
throw error;
}
}
writeFileSync(npmrcPath, buildProjectNpmConfig(existingConfig), "utf8");
}
}
export function getAuthCommands(platform, npmrcPath) {
const cwd = (platform === "win32" ? path.win32 : path.posix).dirname(npmrcPath);
if (platform === "win32") {
return [
{
command: "npm.cmd",
args: ["install", "--global", "vsts-npm-auth@0.43.0", `--registry=${cfsRegistry}`],
},
{
command: "vsts-npm-auth.cmd",
args: ["-config", ".npmrc", "-Force", "-ReadOnly"],
cwd,
},
];
}
return [
{
command: "npm",
args: [
"install",
"--global",
"@microsoft/artifacts-npm-credprovider@1.1.3",
`--registry=${credentialProviderRegistry}`,
`--@microsoft:registry=${credentialProviderRegistry}`,
],
},
{
command: "artifacts-npm-credprovider",
args: ["-f", "-c", ".npmrc"],
cwd,
},
];
}
export function getCommandInvocation(platform, command, args) {
if (platform === "win32") {
return {
command: "cmd.exe",
args: ["/d", "/s", "/c", command, ...args],
};
}
return { command, args };
}
export function runCommand(command, args, platform = process.platform, cwd) {
const invocation = getCommandInvocation(platform, command, args);
const result = spawnSync(invocation.command, invocation.args, {
stdio: "inherit",
cwd,
});
if (result.error) {
throw result.error;
}
if (result.status !== 0) {
const outcome =
result.status === null
? `terminated by signal ${result.signal ?? "unknown"}`
: `exited with code ${result.status}`;
throw new Error(`${command} ${outcome}`);
}
}
export function refreshNpmAuthentication(
platform = process.platform,
npmrcPaths = getProjectNpmrcPaths(),
writer = writeProjectNpmConfigs,
runner = runCommand
) {
writer(npmrcPaths);
for (const { command, args, cwd } of getAuthCommands(platform, npmrcPaths[0])) {
runner(command, args, platform, cwd);
}
}
function usage() {
console.log(`Usage: npm run auth:refresh (from the nodejs directory)
Update scoped project .npmrc files for the copilot-canary @Local view, then
refresh Azure Artifacts credentials in the user-level npm configuration.`);
}
export function main(args = process.argv.slice(2), refresh = refreshNpmAuthentication) {
if (args.length === 1 && (args[0] === "--help" || args[0] === "-h")) {
usage();
return 0;
}
if (args.length !== 1 || args[0] !== "--run") {
usage();
return 1;
}
refresh();
return 0;
}
if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
try {
process.exitCode = main();
} catch (error) {
console.error(error);
process.exitCode = 1;
}
}