Skip to content

Commit

Permalink
feat: don't run on ci servers
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnyreilly committed Nov 8, 2024
1 parent 8eea252 commit 5db3ac1
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
20 changes: 15 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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?

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions src/bin/index.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 5db3ac1

Please sign in to comment.