feat(nx-cloud): alias for login and logout (#27358)

<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior
<!-- This is the behavior we have today -->

N/A

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->

When running `npx nx login`, a file server should open and take you to
Nx Cloud to create a new personal access token.

When running `npx nx logout`, the user should be prompted with Nx Cloud
URLs of instances that they can revoke personal access tokens to.

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #
This commit is contained in:
Louie Weng 2024-08-26 20:26:29 +02:00 committed by GitHub
parent f0e419b04f
commit c93f5d6f0d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
17 changed files with 308 additions and 16 deletions

View File

@ -0,0 +1,42 @@
---
title: 'login - CLI command'
description: 'Login to Nx Cloud'
---
# login
Login to Nx Cloud
## Usage
```shell
nx login [nxCloudUrl]
```
Install `nx` globally to invoke the command directly using `nx`, or use `npx nx`, `yarn nx`, or `pnpm nx`.
## Options
### help
Type: `boolean`
Show help
### nxCloudUrl
Type: `string`
The Nx Cloud URL of the instance you are trying to connect to. If no positional argument is provided, this command will connect to https://cloud.nx.app.
### verbose
Type: `boolean`
Prints additional information about the commands (e.g., stack traces)
### version
Type: `boolean`
Show version number

View File

@ -0,0 +1,36 @@
---
title: 'logout - CLI command'
description: 'Logout from Nx Cloud'
---
# logout
Logout from Nx Cloud
## Usage
```shell
nx logout
```
Install `nx` globally to invoke the command directly using `nx`, or use `npx nx`, `yarn nx`, or `pnpm nx`.
## Options
### help
Type: `boolean`
Show help
### verbose
Type: `boolean`
Prints additional information about the commands (e.g., stack traces)
### version
Type: `boolean`
Show version number

View File

@ -8712,6 +8712,22 @@
"isExternal": false,
"children": [],
"disableCollapsible": false
},
{
"name": "login",
"path": "/nx-api/nx/documents/login",
"id": "login",
"isExternal": false,
"children": [],
"disableCollapsible": false
},
{
"name": "logout",
"path": "/nx-api/nx/documents/logout",
"id": "logout",
"isExternal": false,
"children": [],
"disableCollapsible": false
}
],
"isExternal": false,

View File

@ -1967,6 +1967,28 @@
"path": "/nx-api/nx/documents/add",
"tags": [],
"originalFilePath": "generated/cli/add"
},
"/nx-api/nx/documents/login": {
"id": "login",
"name": "login",
"description": "The core Nx plugin contains the core functionality of Nx like the project graph, nx commands and task orchestration.",
"file": "generated/packages/nx/documents/login",
"itemList": [],
"isExternal": false,
"path": "/nx-api/nx/documents/login",
"tags": [],
"originalFilePath": "generated/cli/login"
},
"/nx-api/nx/documents/logout": {
"id": "logout",
"name": "logout",
"description": "The core Nx plugin contains the core functionality of Nx like the project graph, nx commands and task orchestration.",
"file": "generated/packages/nx/documents/logout",
"itemList": [],
"isExternal": false,
"path": "/nx-api/nx/documents/logout",
"tags": [],
"originalFilePath": "generated/cli/logout"
}
},
"root": "/packages/nx",

View File

@ -1946,6 +1946,28 @@
"path": "nx/documents/add",
"tags": [],
"originalFilePath": "generated/cli/add"
},
{
"id": "login",
"name": "login",
"description": "The core Nx plugin contains the core functionality of Nx like the project graph, nx commands and task orchestration.",
"file": "generated/packages/nx/documents/login",
"itemList": [],
"isExternal": false,
"path": "nx/documents/login",
"tags": [],
"originalFilePath": "generated/cli/login"
},
{
"id": "logout",
"name": "logout",
"description": "The core Nx plugin contains the core functionality of Nx like the project graph, nx commands and task orchestration.",
"file": "generated/packages/nx/documents/logout",
"itemList": [],
"isExternal": false,
"path": "nx/documents/logout",
"tags": [],
"originalFilePath": "generated/cli/logout"
}
],
"executors": [

View File

@ -0,0 +1,42 @@
---
title: 'login - CLI command'
description: 'Login to Nx Cloud'
---
# login
Login to Nx Cloud
## Usage
```shell
nx login [nxCloudUrl]
```
Install `nx` globally to invoke the command directly using `nx`, or use `npx nx`, `yarn nx`, or `pnpm nx`.
## Options
### help
Type: `boolean`
Show help
### nxCloudUrl
Type: `string`
The Nx Cloud URL of the instance you are trying to connect to. If no positional argument is provided, this command will connect to https://cloud.nx.app.
### verbose
Type: `boolean`
Prints additional information about the commands (e.g., stack traces)
### version
Type: `boolean`
Show version number

View File

@ -0,0 +1,36 @@
---
title: 'logout - CLI command'
description: 'Logout from Nx Cloud'
---
# logout
Logout from Nx Cloud
## Usage
```shell
nx logout
```
Install `nx` globally to invoke the command directly using `nx`, or use `npx nx`, `yarn nx`, or `pnpm nx`.
## Options
### help
Type: `boolean`
Show help
### verbose
Type: `boolean`
Prints additional information about the commands (e.g., stack traces)
### version
Type: `boolean`
Show version number

View File

@ -2112,6 +2112,16 @@
"name": "add",
"id": "add",
"file": "generated/cli/add"
},
{
"name": "login",
"id": "login",
"file": "generated/cli/login"
},
{
"name": "logout",
"id": "logout",
"file": "generated/cli/logout"
}
]
},

View File

@ -558,6 +558,8 @@
- [view-logs](/nx-api/nx/documents/view-logs)
- [release](/nx-api/nx/documents/release)
- [add](/nx-api/nx/documents/add)
- [login](/nx-api/nx/documents/login)
- [logout](/nx-api/nx/documents/logout)
- [executors](/nx-api/nx/executors)
- [noop](/nx-api/nx/executors/noop)
- [run-commands](/nx-api/nx/executors/run-commands)

View File

@ -80,10 +80,12 @@ export async function connectToNxCloudCommand(
if (isNxCloudUsed(nxJson)) {
const token =
process.env.NX_CLOUD_ACCESS_TOKEN || nxJson.nxCloudAccessToken;
process.env.NX_CLOUD_ACCESS_TOKEN ||
nxJson.nxCloudAccessToken ||
nxJson.nxCloudId;
if (!token) {
throw new Error(
`Unable to authenticate. Either define accessToken in nx.json or set the NX_CLOUD_ACCESS_TOKEN env variable.`
`Unable to authenticate. If you are connecting to Nx Cloud locally, set Nx Cloud ID in nx.json. If you are connecting in a CI context, either define accessToken in nx.json or set the NX_CLOUD_ACCESS_TOKEN env variable.`
);
}
const connectCloudUrl = await createNxCloudOnboardingURL(
@ -93,10 +95,9 @@ export async function connectToNxCloudCommand(
output.log({
title: '✔ This workspace already has Nx Cloud set up',
bodyLines: [
'If you have not done so already, connect your workspace to your Nx Cloud account:',
`- Connect with Nx Cloud at:
${connectCloudUrl}`,
'If you have not done so already, connect your workspace to your Nx Cloud account with the following URL:',
'',
`${connectCloudUrl}`,
],
});

View File

@ -0,0 +1,19 @@
import { CommandModule } from 'yargs';
import { withVerbose } from '../../command-line/yargs-utils/shared-options';
export const yargsLoginCommand: CommandModule = {
command: 'login [nxCloudUrl]',
describe: 'Login to Nx Cloud',
builder: (yargs) =>
withVerbose(
yargs.positional('nxCloudUrl', {
describe:
'The Nx Cloud URL of the instance you are trying to connect to. If no positional argument is provided, this command will connect to https://cloud.nx.app.',
type: 'string',
required: false,
})
),
handler: async (args: any) => {
process.exit(await (await import('./login')).loginHandler(args));
},
};

View File

@ -0,0 +1,20 @@
import { verifyOrUpdateNxCloudClient } from '../../nx-cloud/update-manager';
import { getCloudOptions } from '../../nx-cloud/utilities/get-cloud-options';
import { handleErrors } from '../../utils/params';
export interface LoginArgs {
nxCloudUrl?: string;
verbose?: boolean;
}
export function loginHandler(args: LoginArgs): Promise<number> {
if (args.nxCloudUrl) {
process.env.NX_CLOUD_API = args.nxCloudUrl;
}
return handleErrors(args.verbose, async () => {
const nxCloudClient = (await verifyOrUpdateNxCloudClient(getCloudOptions()))
.nxCloudClient;
await nxCloudClient.commands.login();
});
}

View File

@ -0,0 +1,11 @@
import { CommandModule } from 'yargs';
import { withVerbose } from '../../command-line/yargs-utils/shared-options';
export const yargsLogoutCommand: CommandModule = {
command: 'logout',
describe: 'Logout from Nx Cloud',
builder: (yargs) => withVerbose(yargs),
handler: async (args: any) => {
process.exit(await (await import('./logout')).logoutHandler(args));
},
};

View File

@ -0,0 +1,15 @@
import { verifyOrUpdateNxCloudClient } from '../../nx-cloud/update-manager';
import { getCloudOptions } from '../../nx-cloud/utilities/get-cloud-options';
import { handleErrors } from '../../utils/params';
export interface LogoutArgs {
verbose?: boolean;
}
export function logoutHandler(args: LogoutArgs): Promise<number> {
return handleErrors(args.verbose, async () => {
const nxCloudClient = (await verifyOrUpdateNxCloudClient(getCloudOptions()))
.nxCloudClient;
await nxCloudClient.commands.logout();
});
}

View File

@ -37,6 +37,8 @@ import { yargsWatchCommand } from './watch/command-object';
import { yargsResetCommand } from './reset/command-object';
import { yargsReleaseCommand } from './release/command-object';
import { yargsAddCommand } from './add/command-object';
import { yargsLoginCommand } from './login/command-object';
import { yargsLogoutCommand } from './logout/command-object';
import {
yargsPrintAffectedCommand,
yargsAffectedGraphCommand,
@ -94,6 +96,8 @@ export const commandsObject = yargs
.command(yargsViewLogsCommand)
.command(yargsWatchCommand)
.command(yargsNxInfixCommand)
.command(yargsLoginCommand)
.command(yargsLogoutCommand)
.scriptName('nx')
.help()
// NOTE: we handle --version in nx.ts, this just tells yargs that the option exists

View File

@ -18,15 +18,9 @@ export function createApiAxiosInstance(options: CloudTaskRunnerOptions) {
// TODO(lourw): Update message with NxCloudId once it is supported
if (!accessToken && !nxCloudId) {
if (process.env.NX_ENABLE_LOGIN === 'true' && !nxCloudId) {
throw new Error(
`Unable to authenticate. Please connect your workspace to Nx Cloud to define a valid Nx Cloud Id. If you are in a CI context, please set the NX_CLOUD_ACCESS_TOKEN environment variable or define an access token in your nx.json.`
`Unable to authenticate. If you are connecting to Nx Cloud locally, set an Nx Cloud ID in your nx.json with "nx connect". If you are in a CI context, please set the NX_CLOUD_ACCESS_TOKEN environment variable or define an access token in your nx.json.`
);
} else {
throw new Error(
`Unable to authenticate. Either define accessToken in nx.json or set the NX_CLOUD_ACCESS_TOKEN env variable. If you do not want to use Nx Cloud for this command, either set NX_NO_CLOUD=true, or pass the --no-cloud flag.`
);
}
}
if (options.customProxyConfigPath) {

View File

@ -671,7 +671,7 @@ function getTasksRunnerPath(
nxJson.tasksRunnerOptions?.[runner]?.options?.accessToken ||
// Cloud access token specified in env var.
process.env.NX_CLOUD_ACCESS_TOKEN ||
// Nx Cloud Id specified in nxJson
// Nx Cloud ID specified in nxJson
nxJson.nxCloudId;
return isCloudRunner ? 'nx-cloud' : require.resolve('./default-tasks-runner');