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 6dd0502 commit 7fc8c77
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 2 deletions.
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-0-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();
});
});
});
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
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 7fc8c77

Please sign in to comment.