feat(core): flatten default base config to base (#19964)

This commit is contained in:
Craigory Coppola 2024-02-22 16:50:25 -05:00 committed by GitHub
parent 17e2248098
commit 95b77f8fb0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
18 changed files with 160 additions and 32 deletions

View File

@ -1,5 +1,9 @@
# Interface: NxAffectedConfig
**`Deprecated`**
Use [NxJsonConfiguration#defaultBase](../../devkit/documents/NxJsonConfiguration#defaultbase) instead
## Table of contents
### Properties

View File

@ -21,6 +21,7 @@ Nx.json configuration
- [affected](../../devkit/documents/NxJsonConfiguration#affected): NxAffectedConfig
- [cacheDirectory](../../devkit/documents/NxJsonConfiguration#cachedirectory): string
- [cli](../../devkit/documents/NxJsonConfiguration#cli): Object
- [defaultBase](../../devkit/documents/NxJsonConfiguration#defaultbase): string
- [defaultProject](../../devkit/documents/NxJsonConfiguration#defaultproject): string
- [extends](../../devkit/documents/NxJsonConfiguration#extends): string
- [generators](../../devkit/documents/NxJsonConfiguration#generators): Object
@ -47,6 +48,10 @@ Nx.json configuration
Default options for `nx affected`
**`Deprecated`**
use [defaultBase](../../devkit/documents/NxJsonConfiguration#defaultbase) instead. For more information see https://nx.dev/deprecated/affected-config#affected-config
---
### cacheDirectory
@ -72,6 +77,14 @@ Default generator collection. It is used when no collection is provided.
---
### defaultBase
`Optional` **defaultBase**: `string`
Default value for --base used by `nx affected` and `nx format`.
---
### defaultProject
`Optional` **defaultProject**: `string`

View File

@ -19,6 +19,7 @@ use ProjectsConfigurations or NxJsonConfiguration
- [affected](../../devkit/documents/Workspace#affected): NxAffectedConfig
- [cacheDirectory](../../devkit/documents/Workspace#cachedirectory): string
- [cli](../../devkit/documents/Workspace#cli): Object
- [defaultBase](../../devkit/documents/Workspace#defaultbase): string
- [defaultProject](../../devkit/documents/Workspace#defaultproject): string
- [extends](../../devkit/documents/Workspace#extends): string
- [generators](../../devkit/documents/Workspace#generators): Object
@ -47,6 +48,10 @@ use ProjectsConfigurations or NxJsonConfiguration
Default options for `nx affected`
**`Deprecated`**
use [defaultBase](../../devkit/documents/NxJsonConfiguration#defaultbase) instead. For more information see https://nx.dev/deprecated/affected-config#affected-config
#### Inherited from
[NxJsonConfiguration](../../devkit/documents/NxJsonConfiguration).[affected](../../devkit/documents/NxJsonConfiguration#affected)
@ -84,6 +89,18 @@ Default generator collection. It is used when no collection is provided.
---
### defaultBase
`Optional` **defaultBase**: `string`
Default value for --base used by `nx affected` and `nx format`.
#### Inherited from
[NxJsonConfiguration](../../devkit/documents/NxJsonConfiguration).[defaultBase](../../devkit/documents/NxJsonConfiguration#defaultbase)
---
### defaultProject
`Optional` **defaultProject**: `string`

View File

@ -16,9 +16,7 @@ The following is an expanded example showing all options. Your `nx.json` will li
],
"parallel": 4,
"cacheDirectory": "tmp/my-nx-cache",
"affected": {
"defaultBase": "main"
},
"defaultBase": "main",
"namedInputs": {
"default": ["{projectRoot}/**/*"],
"production": ["!{projectRoot}/**/*.spec.tsx"]
@ -123,9 +121,9 @@ nx run-many -t build --runner=another
The official types of `runner` supported by Nx are `"nx/tasks-runners/default"` and `"nx-cloud"`.
## Affected
## Default Base
Tells Nx which branch and HEAD to use when calculating affected projects.
Tells Nx which base branch to use when calculating affected projects.
- `defaultBase` defines the default base branch, defaults to `main`.

View File

@ -143,9 +143,7 @@ describe('convert Angular CLI workspace to an Nx workspace', () => {
// check nx.json
const nxJson = readJson('nx.json');
expect(nxJson).toEqual({
affected: {
defaultBase: 'main',
},
defaultBase: 'main',
namedInputs: {
default: ['{projectRoot}/**/*', 'sharedGlobals'],
production: [

View File

@ -80,9 +80,7 @@ exports[`workspace move to nx layout should create a root eslint config 1`] = `
exports[`workspace move to nx layout should create nx.json 1`] = `
{
"affected": {
"defaultBase": "main",
},
"defaultBase": "main",
"defaultProject": "myApp",
"namedInputs": {
"default": [

View File

@ -50,9 +50,7 @@ export function createNxJson(
const targets = getWorkspaceCommonTargets(tree);
writeJson<NxJsonConfiguration>(tree, 'nx.json', {
affected: {
defaultBase: options.defaultBase ?? deduceDefaultBase(),
},
defaultBase: options.defaultBase ?? deduceDefaultBase(),
namedInputs: {
sharedGlobals: [],
default: ['{projectRoot}/**/*', 'sharedGlobals'],

View File

@ -89,6 +89,11 @@
"description": "Updates .env to disabled adding plugins when generating projects in an existing Nx workspace",
"implementation": "./src/migrations/update-18-0-0/disable-crystal-for-existing-workspaces",
"x-repair-skip": true
},
"move-default-base-to-nx-json-root": {
"version": "18.1.0-beta.3",
"description": "Moves affected.defaultBase to defaultBase in `nx.json`",
"implementation": "./src/migrations/update-17-2-0/move-default-base"
}
}
}

View File

@ -18,7 +18,12 @@
"description": "Default based branch used by affected commands."
}
},
"additionalProperties": false
"additionalProperties": false,
"deprecated": "Use `defaultBase` instead. Support for setting `defaultBase` in `affected` will be removed in Nx 19."
},
"defaultBase": {
"type": "string",
"description": "Default --base used by affected logic."
},
"tasksRunnerOptions": {
"type": "object",

View File

@ -55,6 +55,7 @@ export const allowedProjectExtensions = [
export const allowedWorkspaceExtensions = [
'implicitDependencies',
'affected',
'defaultBase',
'tasksRunnerOptions',
'workspaceLayout',
'plugins',

View File

@ -58,8 +58,7 @@ export function createNxJsonFile(
delete nxJson.targetDefaults;
}
nxJson.affected ??= {};
nxJson.affected.defaultBase ??= deduceDefaultBase();
nxJson.defaultBase ??= deduceDefaultBase();
writeJsonFile(nxJsonPath, nxJson);
}

View File

@ -19,6 +19,9 @@ export interface ImplicitJsonSubsetDependency<T = '*' | string[]> {
[key: string]: T | ImplicitJsonSubsetDependency<T>;
}
/**
* @deprecated Use {@link NxJsonConfiguration#defaultBase } instead
*/
export interface NxAffectedConfig {
/**
* Default based branch used by affected commands.
@ -272,8 +275,15 @@ export interface NxJsonConfiguration<T = '*' | string[]> {
targetDefaults?: TargetDefaults;
/**
* Default options for `nx affected`
* @deprecated use {@link defaultBase} instead. For more information see https://nx.dev/deprecated/affected-config#affected-config
*/
affected?: NxAffectedConfig;
/**
* Default value for --base used by `nx affected` and `nx format`.
*/
defaultBase?: string;
/**
* Where new apps + libs should be placed
*/

View File

@ -0,0 +1,47 @@
import { createTreeWithEmptyWorkspace } from '../../generators/testing-utils/create-tree-with-empty-workspace';
import { Tree } from '../../generators/tree';
import { readNxJson, updateNxJson } from '../../generators/utils/nx-json';
import update from './move-default-base';
describe('update-17.1.0 migration', () => {
let tree: Tree;
beforeEach(() => {
tree = createTreeWithEmptyWorkspace({ layout: 'apps-libs' });
});
it("shouldn't do anything if affected.defaultBase is not set", () => {
tree.write('nx.json', JSON.stringify({}));
update(tree);
expect(readNxJson(tree)).toEqual({});
});
it("shouldn't remove affected if other keys present", () => {
updateNxJson(tree, {
affected: {
defaultBase: 'master',
otherKey: 'otherValue',
} as any,
});
update(tree);
expect(readNxJson(tree)).toEqual({
affected: {
otherKey: 'otherValue',
},
defaultBase: 'master',
});
});
it('should remove affected if no other keys present', () => {
updateNxJson(tree, {
affected: {
defaultBase: 'master',
} as any,
});
update(tree);
expect(readNxJson(tree)).toEqual({
defaultBase: 'master',
});
});
});

View File

@ -0,0 +1,21 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import { readNxJson, updateNxJson } from '../../generators/utils/nx-json';
import { Tree } from '../../generators/tree';
import { NxJsonConfiguration } from '../../config/nx-json';
/**
* Updates existing workspaces to move nx.json's affected.defaultBase to nx.json's base.
*/
export default function update(host: Tree) {
const nxJson = readNxJson(host) as NxJsonConfiguration & {
affected: { defaultBase?: string };
};
if (nxJson?.affected?.defaultBase) {
nxJson.defaultBase = nxJson.affected.defaultBase;
delete nxJson.affected.defaultBase;
if (Object.keys(nxJson.affected).length === 0) {
delete nxJson.affected;
}
updateNxJson(host, nxJson);
}
}

View File

@ -142,7 +142,8 @@ export function splitArgsIntoNxArgsAndOverrides(
}
if (!nxArgs.base) {
nxArgs.base = nxJson.affected?.defaultBase || 'main';
nxArgs.base =
nxJson.defaultBase ?? nxJson.affected?.defaultBase ?? 'main';
// No user-provided arguments to set the affected criteria, so inform the user of the defaults being used
if (

View File

@ -2,6 +2,7 @@ import {
NxJsonConfiguration,
PackageManager,
readJson,
readNxJson,
Tree,
updateJson,
writeJson,
@ -114,6 +115,21 @@ describe('CI Workflow generator', () => {
);
});
it('should prefix nx.json base with origin/ if ci is bitbucket-pipelines', async () => {
setNxCloud(tree);
const nxJson = readNxJson(tree);
nxJson.defaultBase = 'my-branch';
writeJson(tree, 'nx.json', nxJson);
await ciWorkflowGenerator(tree, {
ci: 'bitbucket-pipelines',
name: 'CI',
});
expect(readNxJson(tree).defaultBase).toEqual('origin/my-branch');
});
it('should generate gitlab config', async () => {
setNxCloud(tree);
await ciWorkflowGenerator(tree, { ci: 'gitlab', name: 'CI' });

View File

@ -85,17 +85,16 @@ function normalizeOptions(options: Schema, tree: Tree): Substitutes {
}
function defaultBranchNeedsOriginPrefix(nxJson: NxJsonConfiguration): boolean {
return !nxJson.affected?.defaultBase?.startsWith('origin/');
const base = nxJson.defaultBase ?? nxJson.affected?.defaultBase;
return !base?.startsWith('origin/');
}
function appendOriginPrefix(nxJson: NxJsonConfiguration): NxJsonConfiguration {
return {
...nxJson,
affected: {
...(nxJson.affected ?? {}),
defaultBase: nxJson.affected?.defaultBase
? `origin/${nxJson.affected.defaultBase}`
: 'origin/main',
},
};
if (nxJson?.affected?.defaultBase) {
nxJson.affected.defaultBase = `origin/${nxJson.affected.defaultBase}`;
}
if (nxJson.defaultBase || !nxJson.affected) {
nxJson.defaultBase = `origin/${nxJson.defaultBase ?? deduceDefaultBase()}`;
}
return nxJson;
}

View File

@ -64,9 +64,7 @@ function createNxJson(
) {
const nxJson: NxJsonConfiguration & { $schema: string } = {
$schema: './node_modules/nx/schemas/nx-schema.json',
affected: {
defaultBase,
},
defaultBase,
targetDefaults:
process.env.NX_ADD_PLUGINS === 'false'
? {
@ -82,7 +80,7 @@ function createNxJson(
};
if (defaultBase === 'main') {
delete nxJson.affected;
delete nxJson.defaultBase;
}
if (preset !== Preset.NPM) {
nxJson.namedInputs = {