Skip to content

Commit

Permalink
fix(core): add migrations to remove root
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenPandaz committed Apr 27, 2022
1 parent b1594dc commit f2c5396
Show file tree
Hide file tree
Showing 13 changed files with 386 additions and 76 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`workspace --preserve-angular-cli-layout should create nx.json 1`] = `
Object {
"affected": Object {
"defaultBase": "main",
},
"implicitDependencies": Object {
".eslintrc.json": "*",
"package.json": Object {
"dependencies": "*",
"devDependencies": "*",
},
},
"npmScope": "my-scope",
"targetDependencies": Object {
"build": Array [
Object {
"projects": "dependencies",
"target": "build",
},
],
},
"tasksRunnerOptions": Object {
"default": Object {
"options": Object {
"cacheableOperations": Array [
"build",
"lint",
"test",
"e2e",
],
},
"runner": "nx/tasks-runners/default",
},
},
"workspaceLayout": Object {
"appsDir": "",
"libsDir": "",
},
}
`;

exports[`workspace --preserve-angular-cli-layout should support multiple projects 1`] = `
Object {
"affected": Object {
"defaultBase": "main",
},
"implicitDependencies": Object {
".eslintrc.json": "*",
"package.json": Object {
"dependencies": "*",
"devDependencies": "*",
},
},
"npmScope": "my-scope",
"targetDependencies": Object {
"build": Array [
Object {
"projects": "dependencies",
"target": "build",
},
],
},
"tasksRunnerOptions": Object {
"default": Object {
"options": Object {
"cacheableOperations": Array [
"build",
"lint",
"test",
"e2e",
],
},
"runner": "nx/tasks-runners/default",
},
},
"workspaceLayout": Object {
"appsDir": "projects",
"libsDir": "projects",
},
}
`;

exports[`workspace move to nx layout should create a root eslint config 1`] = `
Object {
"ignorePatterns": Array [
"**/*",
],
"overrides": Array [
Object {
"extends": Array [
"plugin:@angular-eslint/recommended",
"plugin:@angular-eslint/template/process-inline-templates",
],
"files": Array [
"*.ts",
],
"parserOptions": Object {
"createDefaultProgram": true,
},
"rules": Object {
"@angular-eslint/component-selector": Array [
"error",
Object {
"prefix": "app",
"style": "kebab-case",
"type": "element",
},
],
"@angular-eslint/directive-selector": Array [
"error",
Object {
"prefix": "app",
"style": "camelCase",
"type": "attribute",
},
],
},
},
Object {
"extends": Array [
"plugin:@angular-eslint/template/recommended",
],
"files": Array [
"*.html",
],
"rules": Object {},
},
Object {
"files": Array [
"*.ts",
"*.tsx",
"*.js",
"*.jsx",
],
"rules": Object {
"@nrwl/nx/enforce-module-boundaries": Array [
"error",
Object {
"allow": Array [],
"depConstraints": Array [
Object {
"onlyDependOnLibsWithTags": Array [
"*",
],
"sourceTag": "*",
},
],
"enforceBuildableLibDependency": true,
},
],
},
},
],
"plugins": Array [
"@nrwl/nx",
],
"root": true,
}
`;

exports[`workspace move to nx layout should create nx.json 1`] = `
Object {
"affected": Object {
"defaultBase": "main",
},
"cli": Object {
"defaultCollection": "@nrwl/angular",
},
"defaultProject": "myApp",
"implicitDependencies": Object {
".eslintrc.json": "*",
"package.json": Object {
"dependencies": "*",
"devDependencies": "*",
},
},
"npmScope": "my-org",
"targetDependencies": Object {
"build": Array [
Object {
"projects": "dependencies",
"target": "build",
},
],
},
"tasksRunnerOptions": Object {
"default": Object {
"options": Object {
"cacheableOperations": Array [
"build",
"lint",
"test",
"e2e",
],
},
"runner": "nx/tasks-runners/default",
},
},
}
`;

exports[`workspace move to nx layout should update tsconfig.base.json if present 1`] = `
Object {
"compilerOptions": Object {
"baseUrl": ".",
"paths": Object {},
"rootDir": ".",
},
"exclude": Array [
"node_modules",
"tmp",
],
}
`;

exports[`workspace move to nx layout should work if angular-cli workspace had tsconfig.base.json 1`] = `
Object {
"compilerOptions": Object {
"baseUrl": ".",
"paths": Object {},
"rootDir": ".",
},
"exclude": Array [
"node_modules",
"tmp",
],
}
`;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createTree } from '../tests/create-tree';
import { createTree } from 'nx/src/generators/testing-utils/create-tree';
import type { Tree } from 'nx/src/generators/tree';
import { visitNotIgnoredFiles } from './visit-not-ignored-files';

Expand Down
4 changes: 2 additions & 2 deletions packages/devkit/src/utils/package-json.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Tree } from 'nx/src/generators/tree';
import { readJson, writeJson } from './json';
import { readJson, writeJson } from 'nx/src/generators/utils/json';
import { addDependenciesToPackageJson } from './package-json';
import { createTree } from '../tests/create-tree';
import { createTree } from 'nx/src/generators/testing-utils/create-tree';

describe('addDependenciesToPackageJson', () => {
let tree: Tree;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const mockResolveConfig = jest.fn(() =>
);

import { Tree } from '@nrwl/devkit';
import { createTreeWithEmptyWorkspace } from 'packages/devkit/src/tests/create-tree-with-empty-workspace';
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing';
import update from './update-base-jest-config';

describe('update 12.6.0', () => {
Expand Down
9 changes: 8 additions & 1 deletion packages/nx/migrations.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
{
"migrations": {}
"generators": {
"14-0-4-remove-root": {
"cli": "nx",
"version": "14.0.4",
"description": "Remove root property from project.json files",
"factory": "./src/migrations/update-14-0-4/remove-roots"
}
}
}
12 changes: 12 additions & 0 deletions packages/nx/migrations.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import path = require('path');
import json = require('./migrations.json');

describe('Node migrations', () => {
it('should have valid paths', () => {
Object.values(json.generators).forEach((m) => {
expect(() =>
require.resolve(path.join(__dirname, `${m.factory}.ts`))
).not.toThrow();
});
});
});
8 changes: 4 additions & 4 deletions packages/nx/src/config/workspaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,11 +319,11 @@ function findFullGeneratorName(
export function reformattedWorkspaceJsonOrNull(w: any) {
const workspaceJson =
w.version === 2 ? toNewFormatOrNull(w) : toOldFormatOrNull(w);
if (workspaceJson?.projects) {
workspaceJson.projects = sortObjectByKeys(workspaceJson.projects);
}

return {
...workspaceJson,
projects: sortObjectByKeys(workspaceJson.projects),
};
return workspaceJson;
}

export function toNewFormat(w: any): WorkspaceJsonConfiguration {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type * as Prettier from 'prettier';
* Formats all the created or updated files using Prettier
* @param tree - the file system tree
*/
export async function formatChangedWithPrettierIfAvailable(
export async function formatChangedFilesWithPrettierIfAvailable(
tree: Tree
): Promise<void> {
let prettier: typeof Prettier;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import { readJson, updateJson } from '../utils/json';
import {
addProjectConfiguration,
getProjects,
getWorkspacePath,
readProjectConfiguration,
readWorkspaceConfiguration,
removeProjectConfiguration,
updateProjectConfiguration,
updateWorkspaceConfiguration,
WorkspaceConfiguration,
} from './project-configuration';
import { getWorkspacePath } from '../utils/get-workspace-layout';

type ProjectConfigurationV1 = Pick<
ProjectConfiguration,
Expand Down
48 changes: 48 additions & 0 deletions packages/nx/src/migrations/update-14-0-4/remove-roots.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { Tree } from '../../generators/tree';
import { createTreeWithEmptyWorkspace } from '../../generators/testing-utils/create-tree-with-empty-workspace';
import { addProjectConfiguration } from '../../generators/utils/project-configuration';
import { readJson, updateJson } from '../../generators/utils/json';
import removeRoots from './remove-roots';

describe('remove-roots >', () => {
let tree: Tree;

describe('projects with project.json configs', () => {
beforeEach(() => {
tree = createTreeWithEmptyWorkspace(2);
});

it('should remove the root property', async () => {
addProjectConfiguration(tree, 'proj1', {
root: 'proj1',
});

updateJson(tree, 'proj1/project.json', (config) => ({
...config,
root: 'proj1',
}));

await removeRoots(tree);

expect(readJson(tree, 'proj1/project.json').root).toBeUndefined();
});
});

describe('projects with project.json configs', () => {
beforeEach(() => {
tree = createTreeWithEmptyWorkspace(1);
});

it('should remove the root property', async () => {
addProjectConfiguration(tree, 'proj1', {
root: 'proj1',
});

await removeRoots(tree);

expect(readJson(tree, 'workspace.json').projects.proj1.root).toEqual(
'proj1'
);
});
});
});
15 changes: 15 additions & 0 deletions packages/nx/src/migrations/update-14-0-4/remove-roots.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Tree } from '../../generators/tree';
import {
getProjects,
updateProjectConfiguration,
} from '../../generators/utils/project-configuration';
import { formatChangedFilesWithPrettierIfAvailable } from '../../generators/internal-utils/format-changed-files-with-prettier-if-available';

export default async function (tree: Tree) {
// This looks like it does nothing, but this will actually effectively migrate over all the configs that need to be moved over, but won't touch configs that don't need to be moved
for (const [projName, projConfig] of getProjects(tree)) {
updateProjectConfiguration(tree, projName, projConfig);
}

await formatChangedFilesWithPrettierIfAvailable(tree);
}

0 comments on commit f2c5396

Please sign in to comment.