Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(core): add migration and tests
- Loading branch information
Showing
3 changed files
with
157 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
packages/workspace/src/migrations/update-13-7-3/add-project-json-to-implicit-deps.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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': '*', | ||
}, | ||
}); | ||
}); | ||
}); |
24 changes: 24 additions & 0 deletions
24
packages/workspace/src/migrations/update-13-7-3/add-project-json-to-implicit-deps.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |