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

feat: don't run on ci servers #3

Merged
merged 2 commits into from
Nov 8, 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
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: 11 additions & 1 deletion 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 @@ -45,7 +46,6 @@ export async function bin(args: string[]) {
}

prompts.intro(introPrompts);

logLine();

const mappedOptions = {
Expand Down Expand Up @@ -73,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]"}`);
Expand Down
Loading