From 5db3ac136663bcd2d6aca5e6d03a6fd6bdcf438c Mon Sep 17 00:00:00 2001 From: John Date: Fri, 8 Nov 2024 06:35:13 +0200 Subject: [PATCH 1/2] feat: don't run on ci servers --- README.md | 20 +++++++++++++++----- package.json | 1 + pnpm-lock.yaml | 3 +++ src/bin/index.ts | 12 ++++++++++++ 4 files changed, 31 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index e5a5760..b3c922d 100644 --- a/README.md +++ b/README.md @@ -12,24 +12,34 @@ ## Usage -To use `ado-npm-auth-lite` do this: +You know that you need to create a user `.npmrc` file if you encounter the following message when you try to `npm i`: + +```sh +npm error code E401 +npm error Unable to authenticate, your authentication token seems to be invalid. +npm error To correct this please try logging in again with: +npm error npm login +``` + +To get `ado-npm-auth-lite` to create the necessary user `.npmrc` file on your behalf, run the following command: ```shell -az login npx --yes ado-npm-auth-lite --config .npmrc ``` +This requires that you are authenticated with Azure. To authenticate, run `az login`. [Follow these instructions to install the Azure CLI](https://learn.microsoft.com/en-us/cli/azure/install-azure-cli). The `az login` command will prompt you to log in to Azure, so that a token may be acquired by `ado-npm-auth-lite`. It is not necessary to run `az login` if you are already authenticated with Azure. + Typically `ado-npm-auth-lite` will be used as part of a `preinstall` script in your `package.json`: ```json "scripts": { - "preinstall": "az login && npx --yes ado-npm-auth-lite" + "preinstall": "npx --yes ado-npm-auth-lite" }, ``` -This will ensure that the necessary authentication is set up before any `npm install` commands are run. +`ado-npm-auth-lite` detects whether it is running in a CI environment and does not create a users `.npmrc` file in that situation. It uses the [ci-info](https://github.com/watson/ci-info) library to achieve this. -The `az login` command will prompt you to log in to Azure, so that a token may be acquired. It is not necessary to run this command if you are already logged in. The `config` is optional, and if not supplied will default to the `.npmrc` in the project directory. Crucially, `ado-npm-auth-lite` requires the project `.npmrc` in order to operate. +The `config` parameter is optional, and if not supplied will default to the `.npmrc` in the project directory. Crucially, `ado-npm-auth-lite` requires the project `.npmrc` file exists in order that it can acquire the information to run. ## Why Azure DevOps npm auth-lite? diff --git a/package.json b/package.json index db8db2b..e0d5e93 100644 --- a/package.json +++ b/package.json @@ -44,6 +44,7 @@ "@azure/identity": "^4.5.0", "@clack/prompts": "^0.7.0", "chalk": "^5.3.0", + "ci-info": "^4.0.0", "zod": "^3.23.8", "zod-validation-error": "^3.3.1" }, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 8e7d9d2..3ee4dfc 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -17,6 +17,9 @@ importers: chalk: specifier: ^5.3.0 version: 5.3.0 + ci-info: + specifier: ^4.0.0 + version: 4.0.0 zod: specifier: ^3.23.8 version: 3.23.8 diff --git a/src/bin/index.ts b/src/bin/index.ts index d0732f7..b69bd14 100644 --- a/src/bin/index.ts +++ b/src/bin/index.ts @@ -1,5 +1,6 @@ import * as prompts from "@clack/prompts"; import chalk from "chalk"; +import ci from "ci-info"; import { parseArgs } from "node:util"; import { fromZodError } from "zod-validation-error"; @@ -28,6 +29,17 @@ export async function bin(args: string[]) { const introPrompts = `${chalk.blueBright(`📦🔑 Welcome to`)} ${chalk.bgBlueBright.black(`ado-npm-auth-lite`)} ${chalk.blueBright(`${version}! 📦🔑`)}`; const outroPrompts = `${chalk.blueBright(`📦🔑 Thanks for using`)} ${chalk.bgBlueBright.black(`ado-npm-auth-lite`)} ${chalk.blueBright(`${version}! 📦🔑`)}`; + if (ci.isCI) { + prompts.intro(introPrompts); + logLine(); + logLine( + `Detected that you are running on a CI server (${ci.name ?? ""}) and so will not generate a user .npmrc file`, + ); + prompts.outro(outroPrompts); + + return StatusCodes.Success; + } + const { values } = parseArgs({ args, options, From aaa6fe8821904e28cce692824bd5057285001183 Mon Sep 17 00:00:00 2001 From: John Date: Fri, 8 Nov 2024 06:43:01 +0200 Subject: [PATCH 2/2] fix: tests --- src/bin/index.ts | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/src/bin/index.ts b/src/bin/index.ts index b69bd14..d8b2afb 100644 --- a/src/bin/index.ts +++ b/src/bin/index.ts @@ -29,17 +29,6 @@ export async function bin(args: string[]) { const introPrompts = `${chalk.blueBright(`📦🔑 Welcome to`)} ${chalk.bgBlueBright.black(`ado-npm-auth-lite`)} ${chalk.blueBright(`${version}! 📦🔑`)}`; const outroPrompts = `${chalk.blueBright(`📦🔑 Thanks for using`)} ${chalk.bgBlueBright.black(`ado-npm-auth-lite`)} ${chalk.blueBright(`${version}! 📦🔑`)}`; - if (ci.isCI) { - prompts.intro(introPrompts); - logLine(); - logLine( - `Detected that you are running on a CI server (${ci.name ?? ""}) and so will not generate a user .npmrc file`, - ); - prompts.outro(outroPrompts); - - return StatusCodes.Success; - } - const { values } = parseArgs({ args, options, @@ -57,7 +46,6 @@ export async function bin(args: string[]) { } prompts.intro(introPrompts); - logLine(); const mappedOptions = { @@ -85,6 +73,16 @@ export async function bin(args: string[]) { const { config, email } = optionsParseResult.data; + // TODO: this will prevent this file from running tests on the server after this - create an override parameter + if (ci.isCI) { + logLine( + `Detected that you are running on a CI server (${ci.name ?? ""}) and so will not generate a user .npmrc file`, + ); + prompts.outro(outroPrompts); + + return StatusCodes.Success; + } + prompts.log.info(`options: - config: ${config ?? "[NONE SUPPLIED - WILL USE DEFAULT]"} - email: ${email ?? "[NONE SUPPLIED - WILL USE DEFAULT]"}`);