Skip to content

Commit

Permalink
chore(core): add migration and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
meeroslav committed Feb 4, 2022
1 parent 8567ca4 commit 9971928
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 0 deletions.
6 changes: 6 additions & 0 deletions packages/workspace/migrations.json
Expand Up @@ -115,6 +115,12 @@
"description": "Remove old options that are no longer used",
"cli": "nx",
"implementation": "./src/migrations/update-13-6-0/remove-old-task-runner-options"
},
"13-7-3-add-project-json-to-implicit-deps": {
"version": "13.7.3",
"description": "Add project.json to nx implicitDependencies",
"cli": "nx",
"implementation": "./src/migrations/update-13-7-3/add-project-json-to-implicit-deps"
}
},
"packageJsonUpdates": {
Expand Down
@@ -0,0 +1,127 @@
import { NxJsonConfiguration, readJson, Tree, writeJson } from '@nrwl/devkit';
import { createTree } from '@nrwl/devkit/testing';
import addProjectJsonToImplicitDeps from './add-project-json-to-implicit-deps';

describe('addProjectJsonToImplicitDeps', () => {
let tree: Tree;

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

it("should not add implicit dependencies if extends preset json's", () => {
writeJson<NxJsonConfiguration>(tree, 'nx.json', {
npmScope: 'scope',
extends: '@nrwl/workspace/presets/core.json',
});
addProjectJsonToImplicitDeps(tree);
const result = readJson(tree, 'nx.json');
expect(result).toEqual({
npmScope: 'scope',
extends: '@nrwl/workspace/presets/core.json',
});
});

it('should add implicit dependencies if missing', () => {
writeJson<NxJsonConfiguration>(tree, 'nx.json', {
npmScope: 'scope',
});
addProjectJsonToImplicitDeps(tree);
const result = readJson(tree, 'nx.json');
expect(result).toEqual({
npmScope: 'scope',
implicitDependencies: {
'apps/**/project.json': '*',
'libs/**/project.json': '*',
},
});
});

it('should update implicit dependencies if available', () => {
writeJson<NxJsonConfiguration>(tree, 'nx.json', {
npmScope: 'scope',
implicitDependencies: {
'package.json': {
dependencies: '*',
devDependencies: '*',
},
'.eslintrc.json': '*',
},
});
addProjectJsonToImplicitDeps(tree);
const result = readJson(tree, 'nx.json');
expect(result).toEqual({
npmScope: 'scope',
implicitDependencies: {
'package.json': {
dependencies: '*',
devDependencies: '*',
},
'.eslintrc.json': '*',
'apps/**/project.json': '*',
'libs/**/project.json': '*',
},
});
});

it('should use workspaceLayout folder names if available', () => {
writeJson<NxJsonConfiguration>(tree, 'nx.json', {
npmScope: 'scope',
workspaceLayout: { appsDir: 'appz', libsDir: 'libz' },
});
addProjectJsonToImplicitDeps(tree);
const result = readJson(tree, 'nx.json');
expect(result).toEqual({
npmScope: 'scope',
workspaceLayout: { appsDir: 'appz', libsDir: 'libz' },
implicitDependencies: {
'appz/**/project.json': '*',
'libz/**/project.json': '*',
},
});
});

it('should create single entry is apps and libs folder are same', () => {
writeJson<NxJsonConfiguration>(tree, 'nx.json', {
npmScope: 'scope',
workspaceLayout: { appsDir: 'packages', libsDir: 'packages' },
});
addProjectJsonToImplicitDeps(tree);
const result = readJson(tree, 'nx.json');
expect(result).toEqual({
npmScope: 'scope',
workspaceLayout: { appsDir: 'packages', libsDir: 'packages' },
implicitDependencies: {
'packages/**/project.json': '*',
},
});
});

it('should not update implicit dependencies for apps or libs if already set', () => {
writeJson<NxJsonConfiguration>(tree, 'nx.json', {
npmScope: 'scope',
implicitDependencies: {
'package.json': {
dependencies: '*',
devDependencies: '*',
},
'.eslintrc.json': '*',
'apps/**/project.json': ['testing'],
},
});
addProjectJsonToImplicitDeps(tree);
const result = readJson(tree, 'nx.json');
expect(result).toEqual({
npmScope: 'scope',
implicitDependencies: {
'package.json': {
dependencies: '*',
devDependencies: '*',
},
'.eslintrc.json': '*',
'apps/**/project.json': ['testing'],
'libs/**/project.json': '*',
},
});
});
});
@@ -0,0 +1,24 @@
import {
readWorkspaceConfiguration,
Tree,
updateWorkspaceConfiguration,
} from '@nrwl/devkit';

export function addProjectJsonToImplicitDeps(host: Tree) {
const workspaceConfig = readWorkspaceConfiguration(host);
if (!workspaceConfig.extends) {
const appsDir = workspaceConfig.workspaceLayout?.appsDir || 'apps';
const libsDir = workspaceConfig.workspaceLayout?.libsDir || 'libs';
// make sure dependencies exist
workspaceConfig.implicitDependencies =
workspaceConfig.implicitDependencies || {};
// set implicit dependencies
workspaceConfig.implicitDependencies[`${appsDir}/**/project.json`] =
workspaceConfig.implicitDependencies[`${appsDir}/**/project.json`] || '*';
workspaceConfig.implicitDependencies[`${libsDir}/**/project.json`] =
workspaceConfig.implicitDependencies[`${libsDir}/**/project.json`] || '*';
}
updateWorkspaceConfiguration(host, workspaceConfig);
}

export default addProjectJsonToImplicitDeps;

0 comments on commit 9971928

Please sign in to comment.