Skip to content

Commit

Permalink
feat(core): read cloud url from nx.json (#19468)
Browse files Browse the repository at this point in the history
  • Loading branch information
AgentEnder authored Oct 6, 2023
1 parent df8be31 commit e97bbc0
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 9 deletions.
10 changes: 10 additions & 0 deletions docs/generated/devkit/NxJsonConfiguration.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Nx.json configuration
- [namedInputs](../../devkit/documents/NxJsonConfiguration#namedinputs): Object
- [npmScope](../../devkit/documents/NxJsonConfiguration#npmscope): string
- [nxCloudAccessToken](../../devkit/documents/NxJsonConfiguration#nxcloudaccesstoken): string
- [nxCloudUrl](../../devkit/documents/NxJsonConfiguration#nxcloudurl): string
- [parallel](../../devkit/documents/NxJsonConfiguration#parallel): number
- [plugins](../../devkit/documents/NxJsonConfiguration#plugins): string[]
- [pluginsConfig](../../devkit/documents/NxJsonConfiguration#pluginsconfig): Record<string, unknown>
Expand Down Expand Up @@ -169,6 +170,15 @@ To use a different runner that accepts an access token, define it in [tasksRunne

---

### nxCloudUrl

`Optional` **nxCloudUrl**: `string`

Specifies the url pointing to an instance of nx cloud. Used for remote
caching and displaying run links.

---

### parallel

`Optional` **parallel**: `number`
Expand Down
14 changes: 14 additions & 0 deletions docs/generated/devkit/Workspace.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use ProjectsConfigurations or NxJsonConfiguration
- [namedInputs](../../devkit/documents/Workspace#namedinputs): Object
- [npmScope](../../devkit/documents/Workspace#npmscope): string
- [nxCloudAccessToken](../../devkit/documents/Workspace#nxcloudaccesstoken): string
- [nxCloudUrl](../../devkit/documents/Workspace#nxcloudurl): string
- [parallel](../../devkit/documents/Workspace#parallel): number
- [plugins](../../devkit/documents/Workspace#plugins): string[]
- [pluginsConfig](../../devkit/documents/Workspace#pluginsconfig): Record<string, unknown>
Expand Down Expand Up @@ -213,6 +214,19 @@ To use a different runner that accepts an access token, define it in [tasksRunne

---

### nxCloudUrl

`Optional` **nxCloudUrl**: `string`

Specifies the url pointing to an instance of nx cloud. Used for remote
caching and displaying run links.

#### Inherited from

[NxJsonConfiguration](../../devkit/documents/NxJsonConfiguration).[nxCloudUrl](../../devkit/documents/NxJsonConfiguration#nxcloudurl)

---

### parallel

`Optional` **parallel**: `number`
Expand Down
6 changes: 5 additions & 1 deletion packages/nx/schemas/nx-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,14 @@
"type": "string",
"description": "Default project. When project isn't provided, the default project will be used."
},
"accessToken": {
"nxCloudAccessToken": {
"type": "string",
"description": "The access token to use for nx-cloud. If set, the default tasks runner will be nx-cloud."
},
"nxCloudUrl": {
"type": "string",
"description": "Specifies the url pointing to an instance of nx cloud. Used for remote caching and displaying run links."
},
"parallel": {
"type": "number",
"description": "Specifies how many tasks are ran in parallel by Nx for the default tasks runner."
Expand Down
57 changes: 50 additions & 7 deletions packages/nx/src/adapter/compat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import {
createProjectGraphAsync,
readProjectsConfigurationFromProjectGraph,
} from '../project-graph/project-graph';
import { ProjectsConfigurations } from '../config/workspace-json-project-json';
import {
ProjectConfiguration,
ProjectsConfigurations,
} from '../config/workspace-json-project-json';
import { readNxJson } from '../config/configuration';
import { NxJsonConfiguration } from '../config/nx-json';
import { toOldFormat } from './angular-json';
Expand All @@ -13,7 +16,21 @@ const originalRequire: NodeRequire = Module.prototype.require;

let patched = false;

const allowedProjectExtensions = [
// Checks that a given const array has all keys in the union provided as T2,
// and marks it mutable. In this case, this is useful s.t. we can ensure the
// arrays we pass to angular for allowedProjectExtensions and allowedWorkspaceExtensions
// contain all of the keys which we may be passing through.
type CheckHasKeys<Arr extends readonly unknown[], Keys extends Arr[number]> = {
-readonly [P in keyof Arr]: Arr[P];
};

// If we pass props on a project that angular doesn't know about,
// it throws a warning that users see. We want to pass them still,
// so older plugins writtin in Ng Devkit can update these.
//
// There are some props in here (root) that angular already knows about,
// but it doesn't hurt to have them in here as well to help static analysis.
export const allowedProjectExtensions = [
'tags',
'implicitDependencies',
'configFilePath',
Expand All @@ -22,9 +39,18 @@ const allowedProjectExtensions = [
'namedInputs',
'name',
'files',
];
'root',
'sourceRoot',
'projectType',
] as const;

const allowedWorkspaceExtensions = [
// If we pass props on the workspace that angular doesn't know about,
// it throws a warning that users see. We want to pass them still,
// so older plugins writtin in Ng Devkit can update these.
//
// There are some props in here (root) that angular already knows about,
// but it doesn't hurt to have them in here as well to help static analysis.
export const allowedWorkspaceExtensions = [
'implicitDependencies',
'affected',
'npmScope',
Expand All @@ -35,7 +61,18 @@ const allowedWorkspaceExtensions = [
'files',
'generators',
'namedInputs',
];
'extends',
'cli',
'pluginsConfig',
'defaultProject',
'installation',
'release',
'nxCloudAccessToken',
'nxCloudUrl',
'parallel',
'cacheDirectory',
'useDaemonProcess',
] as const;

if (!patched) {
Module.prototype.require = function () {
Expand Down Expand Up @@ -93,8 +130,14 @@ function mockReadJsonWorkspace(
(originalReadJsonWorkspace) => async (path, host, options) => {
const modifiedOptions = {
...options,
allowedProjectExtensions,
allowedWorkspaceExtensions,
allowedProjectExtensions: allowedProjectExtensions as CheckHasKeys<
typeof allowedProjectExtensions,
keyof Omit<ProjectConfiguration, 'targets' | 'generators'>
>,
allowedWorkspaceExtensions: allowedWorkspaceExtensions as CheckHasKeys<
typeof allowedWorkspaceExtensions,
keyof NxJsonConfiguration
>,
};
try {
// Attempt angular CLI default behaviour
Expand Down
6 changes: 6 additions & 0 deletions packages/nx/src/config/nx-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,12 @@ export interface NxJsonConfiguration<T = '*' | string[]> {
*/
nxCloudAccessToken?: string;

/**
* Specifies the url pointing to an instance of nx cloud. Used for remote
* caching and displaying run links.
*/
nxCloudUrl?: string;

/**
* Specifies how many tasks can be run in parallel.
*/
Expand Down
2 changes: 2 additions & 0 deletions packages/nx/src/generators/utils/deprecated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export function updateWorkspaceConfiguration(
cacheDirectory,
parallel,
useDaemonProcess,
nxCloudUrl,
} = workspaceConfig;

const nxJson: Required<NxJsonConfiguration> = {
Expand All @@ -66,6 +67,7 @@ export function updateWorkspaceConfiguration(
cacheDirectory,
parallel,
useDaemonProcess,
nxCloudUrl,
};

updateNxJson(tree, nxJson);
Expand Down
9 changes: 8 additions & 1 deletion packages/nx/src/tasks-runner/run-command.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,21 @@ describe('getRunner', () => {
it('uses nx-cloud when no tasksRunnerOptions are present and accessToken is specified', () => {
jest.mock('nx-cloud', () => mockRunner);

const { tasksRunner } = getRunner(
const { tasksRunner, runnerOptions } = getRunner(
{},
{
nxCloudAccessToken: 'XXXX-XXX-XXXX',
nxCloudUrl: 'https://my-nx-cloud.app',
}
);

expect(tasksRunner).toEqual(mockRunner);
expect(runnerOptions).toMatchInlineSnapshot(`
{
"accessToken": "XXXX-XXX-XXXX",
"url": "https://my-nx-cloud.app",
}
`);
});

it('reads options from base properties if no runner options provided', () => {
Expand Down
4 changes: 4 additions & 0 deletions packages/nx/src/tasks-runner/run-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,10 @@ function getRunnerOptions(
result.accessToken ??= nxJson.nxCloudAccessToken;
}

if (nxJson.nxCloudUrl && isCloudDefault) {
result.url ??= nxJson.nxCloudUrl;
}

if (nxJson.parallel) {
result.parallel ??= nxJson.parallel;
}
Expand Down

1 comment on commit e97bbc0

@vercel
Copy link

@vercel vercel bot commented on e97bbc0 Oct 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx-dev-nrwl.vercel.app
nx-dev-git-master-nrwl.vercel.app
nx.dev
nx-five.vercel.app

Please sign in to comment.