Skip to content

Commit

Permalink
feat(nx-cloud): alias for login and logout (#27358)
Browse files Browse the repository at this point in the history
<!-- 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 #
  • Loading branch information
lourw authored Aug 26, 2024
1 parent f0e419b commit c93f5d6
Show file tree
Hide file tree
Showing 17 changed files with 308 additions and 16 deletions.
42 changes: 42 additions & 0 deletions docs/generated/cli/login.md
Original file line number Diff line number Diff line change
@@ -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
36 changes: 36 additions & 0 deletions docs/generated/cli/logout.md
Original file line number Diff line number Diff line change
@@ -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
16 changes: 16 additions & 0 deletions docs/generated/manifests/menus.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
22 changes: 22 additions & 0 deletions docs/generated/manifests/nx-api.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
22 changes: 22 additions & 0 deletions docs/generated/packages-metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
42 changes: 42 additions & 0 deletions docs/generated/packages/nx/documents/login.md
Original file line number Diff line number Diff line change
@@ -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
36 changes: 36 additions & 0 deletions docs/generated/packages/nx/documents/logout.md
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions docs/map.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
]
},
Expand Down
2 changes: 2 additions & 0 deletions docs/shared/reference/sitemap.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 7 additions & 6 deletions packages/nx/src/command-line/connect/connect-to-nx-cloud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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}`,
],
});

Expand Down
19 changes: 19 additions & 0 deletions packages/nx/src/command-line/login/command-object.ts
Original file line number Diff line number Diff line change
@@ -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));
},
};
20 changes: 20 additions & 0 deletions packages/nx/src/command-line/login/login.ts
Original file line number Diff line number Diff line change
@@ -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();
});
}
11 changes: 11 additions & 0 deletions packages/nx/src/command-line/logout/command-object.ts
Original file line number Diff line number Diff line change
@@ -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));
},
};
15 changes: 15 additions & 0 deletions packages/nx/src/command-line/logout/logout.ts
Original file line number Diff line number Diff line change
@@ -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();
});
}
4 changes: 4 additions & 0 deletions packages/nx/src/command-line/nx-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
Loading

0 comments on commit c93f5d6

Please sign in to comment.