Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose functionality as a library #48

Merged
merged 6 commits into from
Oct 7, 2024
Merged
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
11 changes: 11 additions & 0 deletions change/change-14f1e6d4-2a6d-446d-bf38-f3aec810f8a2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"changes": [
{
"type": "minor",
"comment": "Expose functionality as a library",
"packageName": "azureauth",
"email": "[email protected]",
"dependentChangeType": "patch"
}
]
}
11 changes: 11 additions & 0 deletions change/change-44c62a72-3c87-4839-9edf-303ecac8725f.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"changes": [
{
"type": "minor",
"comment": "Expose functionality as a library",
"packageName": "ado-npm-auth",
"email": "[email protected]",
"dependentChangeType": "patch"
}
]
}
2 changes: 2 additions & 0 deletions packages/ado-npm-auth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
"license": "MIT",
"type": "module",
"main": "./dist/index.js",
"typings": "./lib/index.d.ts",
"bin": {
"ado-npm-auth": "./bin/index.js"
},
"files": [
"dist/index.js",
"dist/ado-npm-auth.cjs",
"lib/*.d.ts",
"bin",
"static",
"LICENCSE.txt",
Expand Down
18 changes: 13 additions & 5 deletions packages/ado-npm-auth/src/azureauth/ado.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,20 @@ export type AdoPatResponse = {
*/
export const adoPat = async (
options: AdoPatOptions,
azureAuthLocation?: string,
): Promise<AdoPatResponse | string> => {
if (!isSupportedPlatformAndArchitecture()) {
throw new Error(
`AzureAuth is not supported for platform ${platform()} and architecture ${arch()}`,
);
}

const { command: authCommand, env } = azureAuthCommand();
const { command: authCommand, env } = azureAuthLocation
? {
command: [azureAuthLocation],
env: process.env,
}
: azureAuthCommand();

const command = [
...authCommand,
Expand Down Expand Up @@ -75,8 +81,10 @@ export const adoPat = async (
try {
result = spawnSync(command[0], command.slice(1), { encoding: "utf-8" });

if (result.error) {
throw new Error(result.error.message);
if (result.status === 0 || (result.stderr && !result.stdout)) {
throw new Error(
`Azure Auth failed with exit code ${result.status}: ${result.stderr}`,
);
}
} catch (error: any) {
throw new Error(
Expand All @@ -87,7 +95,7 @@ export const adoPat = async (
try {
result = await exec(command.join(" "), { env });

if (result.stderr) {
if (result.stderr && !result.stdout) {
throw new Error(result.stderr);
}
} catch (error: any) {
Expand All @@ -108,7 +116,7 @@ export const adoPat = async (
return result.stdout;
} catch (error: any) {
if (!(await isAzureAuthInstalled())) {
throw new Error(`AzureAuth is not installed.`);
throw new Error(`AzureAuth is not installed: ${error}`);
}

throw new Error(error.message);
Expand Down
2 changes: 1 addition & 1 deletion packages/ado-npm-auth/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export const run = async (args: Args): Promise<null | boolean> => {
(feed) => feed.fileProvider,
);
for (const [fileProvider, updatedFeeds] of invalidFeedsByProvider) {
await fileProvider.writeWorspaceRegistries(
await fileProvider.writeWorkspaceRegistries(
updatedFeeds.map((updatedFeed) => updatedFeed.feed),
);
}
Expand Down
4 changes: 3 additions & 1 deletion packages/ado-npm-auth/src/fileProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,5 +108,7 @@ export abstract class FileProvider {
abstract prepUserFile(): Promise<void>;
abstract getUserFeeds(): Promise<Map<string, Feed>>;
abstract getWorkspaceRegistries(): Promise<string[]>;
abstract writeWorspaceRegistries(feedsToPatch: Iterable<Feed>): Promise<void>;
abstract writeWorkspaceRegistries(
feedsToPatch: Iterable<Feed>,
): Promise<void>;
}
5 changes: 5 additions & 0 deletions packages/ado-npm-auth/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export * from "./yarnrc/yarnrcFileProvider.js";
export * from "./npmrc/npmrcFileProvider.js";
export * from "./fileProvider.js";

export * from "./npmrc/generate-npmrc-pat.js";
20 changes: 12 additions & 8 deletions packages/ado-npm-auth/src/npmrc/generate-npmrc-pat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,20 @@ import { toBase64 } from "../utils/encoding.js";
export const generateNpmrcPat = async (
organization: string,
encode = false,
azureAuthLocation?: string,
): Promise<string> => {
const name = `${hostname()}-${organization}`;
const pat = await adoPat({
promptHint: `${name} .npmrc PAT`,
organization,
displayName: `${name}-npmrc-pat`,
scope: ["vso.packaging"],
timeout: "30",
output: "json",
});
const pat = await adoPat(
{
promptHint: `${name} .npmrc PAT`,
organization,
displayName: `${name}-npmrc-pat`,
scope: ["vso.packaging"],
timeout: "30",
output: "json",
},
azureAuthLocation,
);

const rawToken = (pat as AdoPatResponse).token;

Expand Down
2 changes: 1 addition & 1 deletion packages/ado-npm-auth/src/npmrc/npmrcFileProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export class NpmrcFileProvider extends FileProvider {
return `//${registry}:${field}=`;
}

override async writeWorspaceRegistries(
override async writeWorkspaceRegistries(
feedsToPatch: Iterable<Feed>,
): Promise<void> {
const newLinesByRegistryAndField = new Map<string, string>();
Expand Down
2 changes: 1 addition & 1 deletion packages/ado-npm-auth/src/yarnrc/yarnrcFileProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export class YarnRcFileProvider extends FileProvider {
return registries;
}

override async writeWorspaceRegistries(
override async writeWorkspaceRegistries(
feedsToPatch: Iterable<Feed>,
): Promise<void> {
const yarnrc = (await this.paseYarnRc(this.userFilePath)) || {};
Expand Down
Empty file modified packages/node-azureauth/scripts/azureauth.cjs
100644 → 100755
Empty file.
Loading