Skip to content

Commit

Permalink
Make node-azureauth use bundles to avoid pulling in 70 packages for a…
Browse files Browse the repository at this point in the history
… build tool (#40)

* Make node-azureauth use bundles to avoid pulling in 70 packages for a build tool
* bundle node-ado-auth
* remove terser webpack plugin dependency
  • Loading branch information
dannyvv authored Sep 10, 2024
1 parent 323f30c commit 5d5c5cd
Show file tree
Hide file tree
Showing 10 changed files with 508 additions and 1,239 deletions.
11 changes: 11 additions & 0 deletions change/change-9d431c56-aa66-4270-8b01-5b7bbab2ba7f.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"changes": [
{
"type": "patch",
"comment": "Make ado-npm-auth use bundles to avoid pulling in a bunch of packages for a build tool",
"packageName": "ado-npm-auth",
"email": "[email protected]",
"dependentChangeType": "patch"
}
]
}
11 changes: 11 additions & 0 deletions change/change-c0f07880-1dec-4b60-9579-2a1884b53af1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"changes": [
{
"type": "minor",
"comment": "Make node-azureauth use bundles to avoid pulling in 70 packages for a build tool",
"packageName": "azureauth",
"email": "[email protected]",
"dependentChangeType": "patch"
}
]
}
24 changes: 12 additions & 12 deletions packages/ado-npm-auth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,46 +5,46 @@
"repository": "https://github.com/microsoft/ado-npm-auth",
"license": "MIT",
"type": "module",
"main": "./lib/index.js",
"main": "./dist/index.js",
"bin": {
"ado-npm-auth": "./bin/index.js"
},
"files": [
"dist",
"lib",
"dist/index.js",
"dist/ado-npm-auth.cjs",
"bin",
"static",
"LICENCSE.txt",
"README.md"
],
"scripts": {
"build": "tsc",
"bundle": "webpack",
"bundle": "npm run bundleBin && npm run bundleIndex",
"bundleBin": "esbuild --sourcemap --bundle --minify --platform=node src/cli.ts --outfile=dist/ado-npm-auth.cjs",
"bundleIndex": "esbuild --sourcemap --bundle --minify --platform=node --format=esm src/index.ts --outfile=dist/index.js",
"lint": "prettier --check src/**/*.ts",
"performance-test": "node lib/tests/performance.test.js",
"test": "vitest run src"
},
"dependencies": {
"@npmcli/config": "^4.0.1",
"azureauth": "^0.4.7",
"js-yaml": "^4.1.0",
"workspace-tools": "^0.26.3",
"yargs": "^17.7.2"
"azureauth": "^0.4.7"
},
"devDependencies": {
"@npmcli/config": "^4.0.1",
"@types/js-yaml": "4.0.9",
"@types/node": "^20.5.9",
"@types/npmcli__config": "^6.0.0",
"@types/yargs": "^17.0.32",
"esbuild": "^0.23.1",
"eslint": "^8.30.0",
"js-yaml": "^4.1.0",
"prettier": "^3.2.5",
"rimraf": "^5.0.1",
"terser-webpack-plugin": "^1.4.3",
"tslib": "^2.2.0",
"typescript": "^5.2.2",
"vite": "^5.2.8",
"vitest": "^2.0.5",
"webpack-cli": "^4.10.0",
"webpack": "^5.64.4"
"workspace-tools": "^0.26.3",
"yargs": "^17.7.2"
}
}
45 changes: 27 additions & 18 deletions packages/ado-npm-auth/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,21 +119,30 @@ if (!isSupportedPlatformAndArchitecture()) {

const args = parseArgs(process.argv);

const result = await run(args);

if (result === null) {
// current auth is valid, do nothing
logTelemetry({ success: true });
console.log("✅ Current authentication is valid");
} else if (result) {
// automatic auth was performed
// advertise success
logTelemetry({ success: true, automaticSuccess: true });
console.log("✅ Automatic authentication successful");
} else {
// automatic auth failed (for some reason)
// advertise failure and link wiki to fix
console.log("❌ Authentication to package feed failed.");

process.exitCode = 1;
}
run(args)
.then((result) => {
if (result === null) {
// current auth is valid, do nothing
logTelemetry({ success: true });
console.log("✅ Current authentication is valid");
} else if (result) {
// automatic auth was performed
// advertise success
logTelemetry({ success: true, automaticSuccess: true });
console.log("✅ Automatic authentication successful");
} else {
// automatic auth failed (for some reason)
// advertise failure and link wiki to fix
console.log("❌ Authentication to package feed failed.");

process.exitCode = 1;
}
})
.catch((error) => {
console.error(error);
console.log("❌ Authentication to package feed failed.");

process.exitCode = 1;
})


5 changes: 1 addition & 4 deletions packages/node-azureauth/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@
import path from "node:path";
import process from "node:process";
import { execa } from "execa";
import { fileURLToPath } from "url";

const __dirname = fileURLToPath(new URL(".", import.meta.url));

let azureauth = path.join(__dirname, "bin", "azureauth", "azureauth");
let azureauth = path.join(__dirname, "..", "bin", "azureauth", "azureauth");

if (process.platform === "win32") {
azureauth = azureauth + ".exe";
Expand Down
27 changes: 18 additions & 9 deletions packages/node-azureauth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,29 @@
"version": "0.4.7",
"description": "node-azure auth wrapps the AzureAuth CLI wrapper for performing AAD Authentication",
"bin": {
"azureauth": "./cli.js"
"azureauth": "./bin/cli.js"
},
"repository": {
"url": "https://github.com/microsoft/ado-npm-auth"
},
"main": "lib/index.js",
"main": "dist/index.js",
"type": "module",
"scripts": {
"build": "tsc",
"lint": "prettier --check src/**/*.ts ./cli.js",
"bundle": "npm run bundleInstall && npm run bundleCli && npm run bundleIndex",
"bundleInstall": "esbuild --sourcemap --bundle --minify --platform=node --outfile=dist/install.cjs src/install.ts",
"bundleCli": "esbuild --sourcemap --bundle --minify --platform=node cli.js --outfile=dist/cli.cjs",
"bundleIndex": "esbuild --sourcemap --bundle --minify --platform=node --format=esm src/index.ts --outfile=dist/index.js",
"lint": "prettier --check src/**/*.ts ./cli.js ",
"start": "tsc --watch",
"test": "vitest run src",
"postinstall": "node ./scripts/install.js"
"postinstall": "node ./scripts/install.cjs"
},
"files": [
"dist/install.cjs",
"dist/cli.js",
"dist/index.js"
],
"keywords": [
"node",
"azureauth",
Expand All @@ -26,13 +35,13 @@
],
"author": "Jonathan Creamer",
"license": "MIT",
"dependencies": {
"decompress": "^4.2.1",
"execa": "^7.2.0",
"node-downloader-helper": "^2.1.9"
},
"devDependencies": {
"@types/node": "^20.12.7",
"@types/decompress": "^4.2.7",
"decompress": "^4.2.1",
"esbuild": "^0.23.1",
"execa": "^7.2.0",
"node-downloader-helper": "^2.1.9",
"prettier": "^3.2.5",
"typescript": "^5.4.5",
"vitest": "^2.0.5",
Expand Down
9 changes: 9 additions & 0 deletions packages/node-azureauth/scripts/install.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const path = require("node:path");
const fs = require("node:fs");

const installScript = path.join(__dirname, "dist", "install.js");
if (fs.existsSync(installScript)) {
require(installScript);
} else {
console.log(`Skipping downloading of azureauth tool. This package is incomplete.`);
}
2 changes: 0 additions & 2 deletions packages/node-azureauth/src/azure-auth-command.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import path from "node:path";
import process from "node:process";

const __dirname = path.dirname(new URL(import.meta.url).pathname).substring(1);

export const azureAuthCommand = () => {
let azureauth = path.join(__dirname, "..", "bin", "azureauth", "azureauth");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import path from "path";
import fs from "fs";
import path from "node:path";
import fs from "node:fs";

import { DownloaderHelper } from "node-downloader-helper";
import decompress from "decompress";
import { fileURLToPath } from "url";

const __dirname = fileURLToPath(new URL(".", import.meta.url));
const AZURE_AUTH_VERSION = "0.8.4";

async function download(url, saveDirectory) {
async function download(url: string, saveDirectory: string): Promise<void> {
const downloader = new DownloaderHelper(url, saveDirectory);
return new Promise((resolve, reject) => {
downloader.on("end", () => resolve());
Expand Down Expand Up @@ -35,7 +34,7 @@ const AZUREAUTH_INFO = {
version: AZURE_AUTH_VERSION,
};

const AZUREAUTH_NAME_MAP = {
const AZUREAUTH_NAME_MAP: any = {
def: "azureauth",
win32: "azureauth.exe",
linux: "azureauth.exe",
Expand All @@ -48,9 +47,9 @@ export const AZUREAUTH_NAME =

export const install = async () => {
const OUTPUT_DIR = path.join(__dirname, "..", "bin");
const fileExist = (path) => {
const fileExist = (pathToCheck: string) => {
try {
return fs.existsSync(path);
return fs.existsSync(pathToCheck);
} catch (err) {
return false;
}
Expand All @@ -66,7 +65,7 @@ export const install = async () => {
return;
}
// if platform is missing, download source instead of executable
const DOWNLOAD_MAP = {
const DOWNLOAD_MAP: any = {
win32: {
x64: `azureauth-${AZUREAUTH_INFO.version}-win10-x64.zip`,
},
Expand Down Expand Up @@ -95,7 +94,7 @@ export const install = async () => {
console.log(`Downloading azureauth from ${url}`);
try {
await download(url, OUTPUT_DIR);
} catch (err) {
} catch (err: any) {
throw new Error(`Download failed: ${err.message}`);
}
console.log(`Downloaded in ${OUTPUT_DIR}`);
Expand All @@ -121,15 +120,22 @@ export const install = async () => {
}
};

const MAX_RETRIES = 3;
for (let i = 0; i < MAX_RETRIES; i++) {
try {
await install();
break; // success, so exit the loop
} catch (err) {
console.log(`Install failed: ${err.message}`);
}
if (i === MAX_RETRIES - 1) {
throw new Error(`Install failed after ${MAX_RETRIES} attempts`);
async function retryLoop() {
const MAX_RETRIES = 3;
for (let i = 0; i < MAX_RETRIES; i++) {
try {
await install();
break; // success, so exit the loop
} catch (err: any) {
console.log(`Install failed: ${err.message}`);
}
if (i === MAX_RETRIES - 1) {
throw new Error(`Install failed after ${MAX_RETRIES} attempts`);
}
}
}

retryLoop().catch((err) => {
console.error(err);
process.exit(1);
});
Loading

0 comments on commit 5d5c5cd

Please sign in to comment.