Skip to content

Commit

Permalink
feat(core): migrate existing workspaces to use inputs configuration (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenPandaz authored Oct 5, 2022
1 parent ce632aa commit 49c5e50
Show file tree
Hide file tree
Showing 36 changed files with 1,851 additions and 19 deletions.
6 changes: 6 additions & 0 deletions packages/angular/migrations.json
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,12 @@
"version": "15.0.0-beta.0",
"description": "Update the usages of @nrwl/angular/testing to import jasmine-marbles symbols from jasmine-marbles itself.",
"factory": "./src/migrations/update-15-0-0/switch-to-jasmine-marbles"
},
"add-karma-inputs": {
"cli": "nx",
"version": "15.0.0-beta.1",
"description": "Stop hashing karma spec files and config files for build targets and dependent tasks",
"factory": "./src/migrations/update-15-0-0/add-karma-inputs"
}
},
"packageJsonUpdates": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing';
import {
Tree,
addProjectConfiguration,
readWorkspaceConfiguration,
updateWorkspaceConfiguration,
} from '@nrwl/devkit';
import addKarmaInputs from './add-karma-inputs';

describe('15.0.0 migration (add-karma-inputs)', () => {
let tree: Tree;

beforeEach(() => {
tree = createTreeWithEmptyWorkspace();
});

it('should add inputs configuration for karma targets', async () => {
updateWorkspaceConfiguration(tree, {
version: 2,
namedInputs: {
default: ['{projectRoot}/**/*', 'sharedGlobals'],
sharedGlobals: [],
production: ['default'],
},
});
addProjectConfiguration(tree, 'proj', {
root: 'proj',
targets: {
test: {
executor: '@angular-devkit/build-angular:karma',
options: {},
},
test2: {
executor: '@angular-devkit/build-angular:karma',
options: {},
},
notTest: {
executor: 'nx:run-commands',
},
},
});
tree.write('karma.conf.js', '');

await addKarmaInputs(tree);

const updated = readWorkspaceConfiguration(tree);
expect(updated).toMatchInlineSnapshot(`
Object {
"namedInputs": Object {
"default": Array [
"{projectRoot}/**/*",
"sharedGlobals",
],
"production": Array [
"default",
"!{projectRoot}/**/*.spec.[jt]s",
"!{projectRoot}/tsconfig.spec.json",
"!{projectRoot}/karma.conf.js",
],
"sharedGlobals": Array [],
},
"targetDefaults": Object {
"test": Object {
"inputs": Array [
"default",
"^production",
"{workspaceRoot}/karma.conf.js",
],
},
"test2": Object {
"inputs": Array [
"default",
"^production",
"{workspaceRoot}/karma.conf.js",
],
},
},
"version": 2,
}
`);
});
});
60 changes: 60 additions & 0 deletions packages/angular/src/migrations/update-15-0-0/add-karma-inputs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import {
formatFiles,
readWorkspaceConfiguration,
Tree,
updateWorkspaceConfiguration,
} from '@nrwl/devkit';
import { forEachExecutorOptions } from '@nrwl/workspace/src/utilities/executor-options-utils';

export default async function (tree: Tree) {
const workspaceConfiguration = readWorkspaceConfiguration(tree);

const karmaTargets = getKarmaTargetNames(tree);
const hasProductionFileset = !!workspaceConfiguration.namedInputs?.production;

if (karmaTargets.size > 0 && hasProductionFileset) {
const productionFileset = new Set(
workspaceConfiguration.namedInputs.production
);
for (const exclusion of [
'!{projectRoot}/**/*.spec.[jt]s',
'!{projectRoot}/tsconfig.spec.json',
'!{projectRoot}/karma.conf.js',
]) {
productionFileset.add(exclusion);
}
workspaceConfiguration.namedInputs.production =
Array.from(productionFileset);
}

for (const targetName of karmaTargets) {
workspaceConfiguration.targetDefaults ??= {};
const jestTargetDefaults = (workspaceConfiguration.targetDefaults[
targetName
] ??= {});

jestTargetDefaults.inputs ??= [
'default',
hasProductionFileset ? '^production' : '^default',
...(tree.exists('karma.conf.js')
? ['{workspaceRoot}/karma.conf.js']
: []),
];
}

updateWorkspaceConfiguration(tree, workspaceConfiguration);

await formatFiles(tree);
}

function getKarmaTargetNames(tree: Tree) {
const karmaTargetNames = new Set<string>();
forEachExecutorOptions(
tree,
'@angular-devkit/build-angular:karma',
(_, __, target) => {
karmaTargetNames.add(target);
}
);
return karmaTargetNames;
}
6 changes: 6 additions & 0 deletions packages/cypress/migrations.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@
"version": "14.7.0-beta.0",
"description": "Update Cypress if using v10 to support latest component testing features",
"factory": "./src/migrations/update-14-7-0/update-cypress-version-if-10"
},
"add-cypress-inputs": {
"cli": "nx",
"version": "15.0.0-beta.0",
"description": "Stop hashing cypress spec files and config files for build targets and dependent tasks",
"factory": "./src/migrations/update-15-0-0/add-cypress-inputs"
}
},
"packageJsonUpdates": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing';
import {
Tree,
addProjectConfiguration,
readWorkspaceConfiguration,
updateWorkspaceConfiguration,
} from '@nrwl/devkit';
import addCypressInputs from './add-cypress-inputs';

describe('15.0.0 migration (add-cypress-inputs)', () => {
let tree: Tree;

beforeEach(() => {
tree = createTreeWithEmptyWorkspace();
});

it('should add inputs configuration for cypress targets', async () => {
updateWorkspaceConfiguration(tree, {
version: 2,
namedInputs: {
default: ['{projectRoot}/**/*', 'sharedGlobals'],
sharedGlobals: [],
production: ['default'],
},
});
addProjectConfiguration(tree, 'proj', {
root: 'proj',
targets: {
e2e: {
executor: '@nrwl/cypress:cypress',
options: {},
},
e2e2: {
executor: '@nrwl/cypress:cypress',
options: {},
},
notTest: {
executor: 'nx:run-commands',
},
},
});
tree.write('jest.preset.js', '');

await addCypressInputs(tree);

const updated = readWorkspaceConfiguration(tree);
expect(updated).toMatchInlineSnapshot(`
Object {
"namedInputs": Object {
"default": Array [
"{projectRoot}/**/*",
"sharedGlobals",
],
"production": Array [
"default",
],
"sharedGlobals": Array [],
},
"targetDefaults": Object {
"e2e": Object {
"inputs": Array [
"default",
"^production",
],
},
"e2e2": Object {
"inputs": Array [
"default",
"^production",
],
},
},
"version": 2,
}
`);
});

it('should inputs configuration for cypress component testing targets', async () => {
updateWorkspaceConfiguration(tree, {
version: 2,
namedInputs: {
default: ['{projectRoot}/**/*', 'sharedGlobals'],
sharedGlobals: [],
production: ['default'],
},
});
addProjectConfiguration(tree, 'proj', {
root: 'proj',
targets: {
e2e: {
executor: '@nrwl/cypress:cypress',
options: {
testingType: 'component',
},
},
e2e2: {
executor: '@nrwl/cypress:cypress',
options: {
testingType: 'component',
},
},
notTest: {
executor: 'nx:run-commands',
},
},
});
tree.write('jest.preset.js', '');

await addCypressInputs(tree);

const updated = readWorkspaceConfiguration(tree);
expect(updated).toMatchInlineSnapshot(`
Object {
"namedInputs": Object {
"default": Array [
"{projectRoot}/**/*",
"sharedGlobals",
],
"production": Array [
"default",
"!{projectRoot}/cypress/**/*",
"!{projectRoot}/**/*.cy.[jt]s?(x)",
"!{projectRoot}/cypress.config.[jt]s",
],
"sharedGlobals": Array [],
},
"targetDefaults": Object {
"e2e": Object {
"inputs": Array [
"default",
"^production",
],
},
"e2e2": Object {
"inputs": Array [
"default",
"^production",
],
},
},
"version": 2,
}
`);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import {
formatFiles,
readWorkspaceConfiguration,
Tree,
updateWorkspaceConfiguration,
} from '@nrwl/devkit';
import { forEachExecutorOptions } from '@nrwl/workspace/src/utilities/executor-options-utils';
import { CypressExecutorOptions } from '@nrwl/cypress/src/executors/cypress/cypress.impl';

export default async function (tree: Tree) {
const workspaceConfiguration = readWorkspaceConfiguration(tree);

const { cypressTargets, hasComponentTesting } = getCypressTargetNames(tree);
const hasProductionFileset = !!workspaceConfiguration.namedInputs?.production;

if (hasComponentTesting && hasProductionFileset && cypressTargets.size > 0) {
const productionFileset = new Set(
workspaceConfiguration.namedInputs.production
);
for (const exclusion of [
'!{projectRoot}/cypress/**/*',
'!{projectRoot}/**/*.cy.[jt]s?(x)',
'!{projectRoot}/cypress.config.[jt]s',
]) {
productionFileset.add(exclusion);
}
workspaceConfiguration.namedInputs.production =
Array.from(productionFileset);
}

for (const targetName of cypressTargets) {
workspaceConfiguration.targetDefaults ??= {};
const cypressTargetDefaults = (workspaceConfiguration.targetDefaults[
targetName
] ??= {});

cypressTargetDefaults.inputs ??= [
'default',
hasProductionFileset ? '^production' : '^default',
];
}

updateWorkspaceConfiguration(tree, workspaceConfiguration);

await formatFiles(tree);
}

function getCypressTargetNames(tree: Tree) {
const cypressTargets = new Set<string>();
let hasComponentTesting = false;
forEachExecutorOptions<CypressExecutorOptions>(
tree,
'@nrwl/cypress:cypress',
(options, __, target) => {
cypressTargets.add(target);
if (options.testingType === 'component') {
hasComponentTesting = true;
}
}
);
return { cypressTargets, hasComponentTesting };
}
8 changes: 7 additions & 1 deletion packages/jest/migrations.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,14 @@
"update-tests-jest-28": {
"version": "14.6.0-beta.0",
"cli": "nx",
"description": "Update jest test files to support jest 28 changes (https://jestjs.io/docs/upgrading-to-jest28)",
"description": "Update jest test files to support jest 28 changes (https://jestjs.io/docs/upgrading-to-jest28)",
"factory": "./src/migrations/update-14-6-0/update-tests-jest-28"
},
"add-jest-inputs": {
"version": "15.0.0-beta.0",
"cli": "nx",
"description": "Stop hashing jest spec files and config files for build targets and dependent tasks",
"factory": "./src/migrations/update-15-0-0/add-jest-inputs"
}
},
"packageJsonUpdates": {
Expand Down
Loading

1 comment on commit 49c5e50

@vercel
Copy link

@vercel vercel bot commented on 49c5e50 Oct 5, 2022

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-git-master-nrwl.vercel.app
nx-five.vercel.app
nx-dev-nrwl.vercel.app
nx.dev

Please sign in to comment.