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:
parent
f0e419b04f
commit
c93f5d6f0d
42
docs/generated/cli/login.md
Normal file
42
docs/generated/cli/login.md
Normal 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
|
||||||
36
docs/generated/cli/logout.md
Normal file
36
docs/generated/cli/logout.md
Normal 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
|
||||||
@ -8712,6 +8712,22 @@
|
|||||||
"isExternal": false,
|
"isExternal": false,
|
||||||
"children": [],
|
"children": [],
|
||||||
"disableCollapsible": false
|
"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,
|
"isExternal": false,
|
||||||
|
|||||||
@ -1967,6 +1967,28 @@
|
|||||||
"path": "/nx-api/nx/documents/add",
|
"path": "/nx-api/nx/documents/add",
|
||||||
"tags": [],
|
"tags": [],
|
||||||
"originalFilePath": "generated/cli/add"
|
"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",
|
"root": "/packages/nx",
|
||||||
|
|||||||
@ -1946,6 +1946,28 @@
|
|||||||
"path": "nx/documents/add",
|
"path": "nx/documents/add",
|
||||||
"tags": [],
|
"tags": [],
|
||||||
"originalFilePath": "generated/cli/add"
|
"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": [
|
"executors": [
|
||||||
|
|||||||
42
docs/generated/packages/nx/documents/login.md
Normal file
42
docs/generated/packages/nx/documents/login.md
Normal 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
|
||||||
36
docs/generated/packages/nx/documents/logout.md
Normal file
36
docs/generated/packages/nx/documents/logout.md
Normal 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
|
||||||
@ -2112,6 +2112,16 @@
|
|||||||
"name": "add",
|
"name": "add",
|
||||||
"id": "add",
|
"id": "add",
|
||||||
"file": "generated/cli/add"
|
"file": "generated/cli/add"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "login",
|
||||||
|
"id": "login",
|
||||||
|
"file": "generated/cli/login"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "logout",
|
||||||
|
"id": "logout",
|
||||||
|
"file": "generated/cli/logout"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|||||||
@ -558,6 +558,8 @@
|
|||||||
- [view-logs](/nx-api/nx/documents/view-logs)
|
- [view-logs](/nx-api/nx/documents/view-logs)
|
||||||
- [release](/nx-api/nx/documents/release)
|
- [release](/nx-api/nx/documents/release)
|
||||||
- [add](/nx-api/nx/documents/add)
|
- [add](/nx-api/nx/documents/add)
|
||||||
|
- [login](/nx-api/nx/documents/login)
|
||||||
|
- [logout](/nx-api/nx/documents/logout)
|
||||||
- [executors](/nx-api/nx/executors)
|
- [executors](/nx-api/nx/executors)
|
||||||
- [noop](/nx-api/nx/executors/noop)
|
- [noop](/nx-api/nx/executors/noop)
|
||||||
- [run-commands](/nx-api/nx/executors/run-commands)
|
- [run-commands](/nx-api/nx/executors/run-commands)
|
||||||
|
|||||||
@ -80,10 +80,12 @@ export async function connectToNxCloudCommand(
|
|||||||
|
|
||||||
if (isNxCloudUsed(nxJson)) {
|
if (isNxCloudUsed(nxJson)) {
|
||||||
const token =
|
const token =
|
||||||
process.env.NX_CLOUD_ACCESS_TOKEN || nxJson.nxCloudAccessToken;
|
process.env.NX_CLOUD_ACCESS_TOKEN ||
|
||||||
|
nxJson.nxCloudAccessToken ||
|
||||||
|
nxJson.nxCloudId;
|
||||||
if (!token) {
|
if (!token) {
|
||||||
throw new Error(
|
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(
|
const connectCloudUrl = await createNxCloudOnboardingURL(
|
||||||
@ -93,10 +95,9 @@ export async function connectToNxCloudCommand(
|
|||||||
output.log({
|
output.log({
|
||||||
title: '✔ This workspace already has Nx Cloud set up',
|
title: '✔ This workspace already has Nx Cloud set up',
|
||||||
bodyLines: [
|
bodyLines: [
|
||||||
'If you have not done so already, connect your workspace to your Nx Cloud account:',
|
'If you have not done so already, connect your workspace to your Nx Cloud account with the following URL:',
|
||||||
`- Connect with Nx Cloud at:
|
'',
|
||||||
|
`${connectCloudUrl}`,
|
||||||
${connectCloudUrl}`,
|
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
19
packages/nx/src/command-line/login/command-object.ts
Normal file
19
packages/nx/src/command-line/login/command-object.ts
Normal 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));
|
||||||
|
},
|
||||||
|
};
|
||||||
20
packages/nx/src/command-line/login/login.ts
Normal file
20
packages/nx/src/command-line/login/login.ts
Normal 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();
|
||||||
|
});
|
||||||
|
}
|
||||||
11
packages/nx/src/command-line/logout/command-object.ts
Normal file
11
packages/nx/src/command-line/logout/command-object.ts
Normal 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));
|
||||||
|
},
|
||||||
|
};
|
||||||
15
packages/nx/src/command-line/logout/logout.ts
Normal file
15
packages/nx/src/command-line/logout/logout.ts
Normal 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();
|
||||||
|
});
|
||||||
|
}
|
||||||
@ -37,6 +37,8 @@ import { yargsWatchCommand } from './watch/command-object';
|
|||||||
import { yargsResetCommand } from './reset/command-object';
|
import { yargsResetCommand } from './reset/command-object';
|
||||||
import { yargsReleaseCommand } from './release/command-object';
|
import { yargsReleaseCommand } from './release/command-object';
|
||||||
import { yargsAddCommand } from './add/command-object';
|
import { yargsAddCommand } from './add/command-object';
|
||||||
|
import { yargsLoginCommand } from './login/command-object';
|
||||||
|
import { yargsLogoutCommand } from './logout/command-object';
|
||||||
import {
|
import {
|
||||||
yargsPrintAffectedCommand,
|
yargsPrintAffectedCommand,
|
||||||
yargsAffectedGraphCommand,
|
yargsAffectedGraphCommand,
|
||||||
@ -94,6 +96,8 @@ export const commandsObject = yargs
|
|||||||
.command(yargsViewLogsCommand)
|
.command(yargsViewLogsCommand)
|
||||||
.command(yargsWatchCommand)
|
.command(yargsWatchCommand)
|
||||||
.command(yargsNxInfixCommand)
|
.command(yargsNxInfixCommand)
|
||||||
|
.command(yargsLoginCommand)
|
||||||
|
.command(yargsLogoutCommand)
|
||||||
.scriptName('nx')
|
.scriptName('nx')
|
||||||
.help()
|
.help()
|
||||||
// NOTE: we handle --version in nx.ts, this just tells yargs that the option exists
|
// NOTE: we handle --version in nx.ts, this just tells yargs that the option exists
|
||||||
|
|||||||
@ -18,15 +18,9 @@ export function createApiAxiosInstance(options: CloudTaskRunnerOptions) {
|
|||||||
|
|
||||||
// TODO(lourw): Update message with NxCloudId once it is supported
|
// TODO(lourw): Update message with NxCloudId once it is supported
|
||||||
if (!accessToken && !nxCloudId) {
|
if (!accessToken && !nxCloudId) {
|
||||||
if (process.env.NX_ENABLE_LOGIN === 'true' && !nxCloudId) {
|
throw new Error(
|
||||||
throw new Error(
|
`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.`
|
||||||
`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.`
|
);
|
||||||
);
|
|
||||||
} 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) {
|
if (options.customProxyConfigPath) {
|
||||||
|
|||||||
@ -671,7 +671,7 @@ function getTasksRunnerPath(
|
|||||||
nxJson.tasksRunnerOptions?.[runner]?.options?.accessToken ||
|
nxJson.tasksRunnerOptions?.[runner]?.options?.accessToken ||
|
||||||
// Cloud access token specified in env var.
|
// Cloud access token specified in env var.
|
||||||
process.env.NX_CLOUD_ACCESS_TOKEN ||
|
process.env.NX_CLOUD_ACCESS_TOKEN ||
|
||||||
// Nx Cloud Id specified in nxJson
|
// Nx Cloud ID specified in nxJson
|
||||||
nxJson.nxCloudId;
|
nxJson.nxCloudId;
|
||||||
|
|
||||||
return isCloudRunner ? 'nx-cloud' : require.resolve('./default-tasks-runner');
|
return isCloudRunner ? 'nx-cloud' : require.resolve('./default-tasks-runner');
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user