Skip to content

Commit

Permalink
feat(core): flatten default base config to base
Browse files Browse the repository at this point in the history
  • Loading branch information
AgentEnder committed Oct 31, 2023
1 parent 8ec589d commit 478e5e6
Show file tree
Hide file tree
Showing 17 changed files with 135 additions and 29 deletions.
9 changes: 9 additions & 0 deletions docs/generated/devkit/NxJsonConfiguration.md
Expand Up @@ -19,6 +19,7 @@ Nx.json configuration
### Properties

- [affected](../../devkit/documents/NxJsonConfiguration#affected): NxAffectedConfig
- [base](../../devkit/documents/NxJsonConfiguration#base): string
- [cacheDirectory](../../devkit/documents/NxJsonConfiguration#cachedirectory): string
- [cli](../../devkit/documents/NxJsonConfiguration#cli): Object
- [defaultProject](../../devkit/documents/NxJsonConfiguration#defaultproject): string
Expand Down Expand Up @@ -49,6 +50,14 @@ Default options for `nx affected`

---

### base

`Optional` **base**: `string`

Default value for --base used by `nx affected` and `nx format`.

---

### cacheDirectory

`Optional` **cacheDirectory**: `string`
Expand Down
13 changes: 13 additions & 0 deletions docs/generated/devkit/Workspace.md
Expand Up @@ -17,6 +17,7 @@ use ProjectsConfigurations or NxJsonConfiguration
### Properties

- [affected](../../devkit/documents/Workspace#affected): NxAffectedConfig
- [base](../../devkit/documents/Workspace#base): string
- [cacheDirectory](../../devkit/documents/Workspace#cachedirectory): string
- [cli](../../devkit/documents/Workspace#cli): Object
- [defaultProject](../../devkit/documents/Workspace#defaultproject): string
Expand Down Expand Up @@ -53,6 +54,18 @@ Default options for `nx affected`

---

### base

`Optional` **base**: `string`

Default value for --base used by `nx affected` and `nx format`.

#### Inherited from

[NxJsonConfiguration](../../devkit/documents/NxJsonConfiguration).[base](../../devkit/documents/NxJsonConfiguration#base)

---

### cacheDirectory

`Optional` **cacheDirectory**: `string`
Expand Down
6 changes: 2 additions & 4 deletions docs/shared/reference/nx-json.md
Expand Up @@ -47,11 +47,9 @@ In Nx 16, if the `npmScope` property is present, it will be used as a prefix. If

In Nx 17, the `npmScope` property is ignored.

### Affected
### Base

Tells Nx which branch and HEAD to use when calculating affected projects.

- `defaultBase` defines the default base branch, defaulted to `main`.
Tells Nx which branch and HEAD to use when calculating affected projects. Defaults to `main`.

### inputs & namedInputs

Expand Down
4 changes: 1 addition & 3 deletions e2e/angular-core/src/ng-add.test.ts
Expand Up @@ -141,9 +141,7 @@ describe('convert Angular CLI workspace to an Nx workspace', () => {
// check nx.json
const nxJson = readJson('nx.json');
expect(nxJson).toEqual({
affected: {
defaultBase: 'main',
},
base: 'main',
namedInputs: {
default: ['{projectRoot}/**/*', 'sharedGlobals'],
production: [
Expand Down
Expand Up @@ -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",
},
"base": "main",
"defaultProject": "myApp",
"namedInputs": {
"default": [
Expand Down
Expand Up @@ -49,9 +49,7 @@ export function createNxJson(
const targets = getWorkspaceCommonTargets(tree);

writeJson<NxJsonConfiguration>(tree, 'nx.json', {
affected: {
defaultBase: options.defaultBase ?? deduceDefaultBase(),
},
base: options.defaultBase ?? deduceDefaultBase(),
namedInputs: {
sharedGlobals: [],
default: ['{projectRoot}/**/*', 'sharedGlobals'],
Expand Down
5 changes: 5 additions & 0 deletions packages/nx/migrations.json
Expand Up @@ -76,6 +76,11 @@
"version": "17.0.0-rc.1",
"description": "Migration for v17.0.0-rc.1",
"implementation": "./src/migrations/update-17-0-0/rm-default-collection-npm-scope"
},
"update-17.1.0": {
"version": "17.1.0",
"description": "Moves affected.defaultBase to base in `nx.json`",
"implementation": "./src/migrations/update-17-1-0/move-default-base"
}
}
}
7 changes: 6 additions & 1 deletion packages/nx/schemas/nx-schema.json
Expand Up @@ -18,7 +18,12 @@
"description": "Default based branch used by affected commands."
}
},
"additionalProperties": false
"additionalProperties": false,
"deprecated": "Use `base` instead."
},
"base": {
"type": "string",
"description": "Default --base used by affected logic."
},
"tasksRunnerOptions": {
"type": "object",
Expand Down
1 change: 1 addition & 0 deletions packages/nx/src/adapter/compat.ts
Expand Up @@ -53,6 +53,7 @@ export const allowedProjectExtensions = [
export const allowedWorkspaceExtensions = [
'implicitDependencies',
'affected',
'base',
'tasksRunnerOptions',
'workspaceLayout',
'plugins',
Expand Down
3 changes: 1 addition & 2 deletions packages/nx/src/command-line/init/implementation/utils.ts
Expand Up @@ -76,8 +76,7 @@ export function createNxJsonFile(
nxJson.targetDefaults[target].cache ??= true;
}

nxJson.affected ??= {};
nxJson.affected.defaultBase ??= deduceDefaultBase();
nxJson.base ??= deduceDefaultBase();
writeJsonFile(nxJsonPath, nxJson);
}

Expand Down
6 changes: 6 additions & 0 deletions packages/nx/src/config/nx-json.ts
Expand Up @@ -183,6 +183,12 @@ export interface NxJsonConfiguration<T = '*' | string[]> {
* Default options for `nx affected`
*/
affected?: NxAffectedConfig;

/**
* Default value for --base used by `nx affected` and `nx format`.
*/
base?: string;

/**
* Where new apps + libs should be placed
*/
Expand Down
46 changes: 46 additions & 0 deletions packages/nx/src/migrations/update-17-1-0/move-default-base.spec.ts
@@ -0,0 +1,46 @@
import { createTreeWithEmptyWorkspace } from 'nx/src/generators/testing-utils/create-tree-with-empty-workspace';
import update from './move-default-base';
import { Tree } from 'nx/src/generators/tree';
import { readNxJson, updateNxJson } from 'nx/src/generators/utils/nx-json';

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',
},
base: 'master',
});
});

it('should remove affected if no other keys present', () => {
updateNxJson(tree, {
affected: {
defaultBase: 'master',
} as any,
});
update(tree);
expect(readNxJson(tree)).toEqual({
base: 'master',
});
});
});
18 changes: 18 additions & 0 deletions packages/nx/src/migrations/update-17-1-0/move-default-base.ts
@@ -0,0 +1,18 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import { readNxJson, updateNxJson } from '../../generators/utils/nx-json';
import { Tree } from '../../generators/tree';

/**
* 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);
if (nxJson.affected?.defaultBase) {
nxJson.base = nxJson.affected.defaultBase;
delete nxJson.affected.defaultBase;
if (Object.keys(nxJson.affected).length === 0) {
delete nxJson.affected;
}
updateNxJson(host, nxJson);
}
}
2 changes: 1 addition & 1 deletion packages/nx/src/utils/command-line-utils.ts
Expand Up @@ -142,7 +142,7 @@ export function splitArgsIntoNxArgsAndOverrides(
}

if (!nxArgs.base) {
nxArgs.base = nxJson.affected?.defaultBase || 'main';
nxArgs.base = nxJson.base ?? nxJson.affected?.defaultBase ?? 'main';

// No user-provided arguments to set the affected criteria, so inform the user of the defaults being used
if (
Expand Down
15 changes: 15 additions & 0 deletions packages/workspace/src/generators/ci-workflow/ci-workflow.spec.ts
Expand Up @@ -88,6 +88,21 @@ describe('CI Workflow generator', () => {
);
});

it('should prefix nx.json base with origin/ if ci is bitbucket-pipelines', async () => {
setNxCloud(tree);

const nxJson = readJson(tree, 'nx.json');
nxJson.base = 'my-branch';
writeJson(tree, 'nx.json', nxJson);

await ciWorkflowGenerator(tree, {
ci: 'bitbucket-pipelines',
name: 'CI',
});

expect(readJson(tree, 'nx.json').base).toEqual('origin/my-branch');
});

it('should generate gitlab config', async () => {
setNxCloud(tree);
await ciWorkflowGenerator(tree, { ci: 'gitlab', name: 'CI' });
Expand Down
16 changes: 7 additions & 9 deletions packages/workspace/src/generators/ci-workflow/ci-workflow.ts
Expand Up @@ -68,13 +68,11 @@ function defaultBranchNeedsOriginPrefix(nxJson: NxJsonConfiguration): boolean {
}

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.base || !nxJson.affected) {
nxJson.base = `origin/${nxJson.base ?? deduceDefaultBase()}`;
}
return nxJson;
}
Expand Up @@ -64,9 +64,7 @@ function createNxJson(
) {
const nxJson: NxJsonConfiguration & { $schema: string } = {
$schema: './node_modules/nx/schemas/nx-schema.json',
affected: {
defaultBase,
},
base: defaultBase,
targetDefaults: {
build: {
cache: true,
Expand All @@ -86,6 +84,7 @@ function createNxJson(

if (defaultBase === 'main') {
delete nxJson.affected;
delete nxJson.base;
}
if (preset !== Preset.NPM) {
nxJson.namedInputs = {
Expand Down

0 comments on commit 478e5e6

Please sign in to comment.