-
Notifications
You must be signed in to change notification settings - Fork 2.9k
feat(tools): implement normalize-package-dependencies generator #28382
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Hotell
merged 10 commits into
microsoft:master
from
Hotell:hotell/deps/move-to-start-for-apps-deps
Jul 4, 2023
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
ff4c08d
feat(tools): scaffold normalize-package-dependencies generator
Hotell efcc184
feat(tools): implement normalize-package-dependencies generator
Hotell b672c71
fixup! feat(tools): implement normalize-package-dependencies generator
Hotell 563dffc
fixup! feat(tools): implement normalize-package-dependencies generator
Hotell 8d58e85
fixup! fixup! feat(tools): implement normalize-package-dependencies g…
Hotell da37bcb
feat(tools): properly resolve same npm scope packages that are instal…
Hotell 77281d2
fixup! feat(tools): properly resolve same npm scope packages that are…
Hotell 05d9adc
fixup! feat(tools): properly resolve same npm scope packages that are…
Hotell 87781d0
feat(tools): run normalize-package-deps on peerDeps as well
Hotell e23ee5e
refactor(tools): simplify filter processing
Hotell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,55 @@ | ||
| # normalize-package-dependencies | ||
|
|
||
| Workspace Generator for package.json dependencies normalization. | ||
|
|
||
| > it applies for all dependencies, devDependencies, peerDependencies | ||
|
|
||
| - update mode: updates projects package.json to use \* as version for inner monorepo dependencies | ||
| - verify mode: verifies projects package.json to use \* as version for inner monorepo dependencies | ||
|
|
||
| <!-- toc --> | ||
|
|
||
| - [Usage](#usage) | ||
| - [Examples](#examples) | ||
| - [Options](#options) | ||
| - [`verify`](#verify) | ||
| - [`projectType`](#projecttype) | ||
| - [`tag`](#tag) | ||
|
|
||
| <!-- tocstop --> | ||
|
|
||
| ## Usage | ||
|
|
||
| ```sh | ||
| yarn nx workspace-generator normalize-package-dependencies ... | ||
| ``` | ||
|
|
||
| Show what will be generated without writing to disk: | ||
|
|
||
| ```sh | ||
| yarn nx workspace-generator normalize-package-dependencies --dry-run | ||
| ``` | ||
|
|
||
| ### Examples | ||
|
|
||
| ```sh | ||
| yarn nx workspace-generator normalize-package-dependencies | ||
| ``` | ||
|
|
||
| ## Options | ||
|
|
||
| #### `verify` | ||
|
|
||
| Run generator in check(verification mode). Verify package.json dependencies for all projects or filtered projects (if filters are applied) | ||
|
|
||
| #### `projectType` | ||
|
|
||
| Type: `application | library | any` | ||
|
|
||
| Filter flag. Use to apply generator execution only on projects that contain provided `projectType` within their `project.json#projectType`. | ||
|
|
||
| #### `tag` | ||
|
|
||
| Type: `string` | ||
|
|
||
| Filter flag. Use to apply generator execution only on projects that contain provided tag within their `project.json#tags`. |
314 changes: 314 additions & 0 deletions
314
tools/generators/normalize-package-dependencies/index.spec.ts
This file contains hidden or 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,314 @@ | ||
| import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing'; | ||
| import { | ||
| Tree, | ||
| addProjectConfiguration, | ||
| writeJson, | ||
| getProjects, | ||
| readJson, | ||
| joinPathFragments, | ||
| ProjectGraph, | ||
| readProjectConfiguration, | ||
| updateJson, | ||
| } from '@nrwl/devkit'; | ||
| import * as chalk from 'chalk'; | ||
|
|
||
| import generator from './index'; | ||
| import { PackageJson } from '../../types'; | ||
| import { disableChalk } from '../../utils-testing'; | ||
|
|
||
| const graphMock: ProjectGraph = { | ||
| dependencies: {}, | ||
| nodes: {}, | ||
| externalNodes: {}, | ||
| }; | ||
|
|
||
| jest.mock('@nrwl/devkit', () => { | ||
| async function createProjectGraphAsyncMock(): Promise<ProjectGraph> { | ||
| return graphMock; | ||
| } | ||
|
|
||
| return { | ||
| ...jest.requireActual('@nrwl/devkit'), | ||
| createProjectGraphAsync: createProjectGraphAsyncMock, | ||
| }; | ||
| }); | ||
|
|
||
| describe('normalize-package-dependencies generator', () => { | ||
| let tree: Tree; | ||
| // eslint-disable-next-line @typescript-eslint/no-empty-function | ||
| const noop = () => {}; | ||
| disableChalk(chalk); | ||
|
|
||
| const logLogSpy = jest.spyOn(console, 'log').mockImplementation(noop); | ||
| const infoLogSpy = jest.spyOn(console, 'info').mockImplementation(noop); | ||
|
|
||
| beforeEach(() => { | ||
| tree = createTreeWithEmptyWorkspace(); | ||
| tree = createProject(tree, { | ||
| projectName: 'react-one', | ||
| deps: { dev: {}, prod: { react: '17.x.x' }, peer: {} }, | ||
| tags: ['platform:any'], | ||
| }); | ||
| tree = createProject(tree, { | ||
| projectName: 'react-two', | ||
| deps: { dev: {}, prod: { react: '17.x.x', '@proj/react-one': '^1.0.0' }, peer: {} }, | ||
| tags: ['platform:any', 'scope:two'], | ||
| }); | ||
| tree = createProject(tree, { | ||
| projectName: 'react-three', | ||
| deps: { dev: {}, prod: { react: '17.x.x', '@proj/react-two': '^1.0.0' }, peer: {} }, | ||
| tags: ['platform:any', 'scope:two'], | ||
| }); | ||
| tree = createProject(tree, { | ||
| projectName: 'react-app', | ||
| projectType: 'application', | ||
| deps: { dev: {}, prod: { react: '17.x.x', '@proj/react-one': '^1.0.0', '@proj/react-two': '^1.0.0' }, peer: {} }, | ||
| tags: ['platform:web', 'scope:two', 'type:app'], | ||
| }); | ||
| }); | ||
|
|
||
| it(`should update workspace dependencies to "*" for version`, async () => { | ||
| // this tests that dependency which has different major version (thus is installed from npm) will not be updated to '*' | ||
| addNonWorkspaceDependency(tree, '@proj/react-three', { pkgName: '@proj/react-one', pkgVersion: '^0.1.0' }); | ||
|
|
||
| await generator(tree, {}); | ||
|
|
||
| const { reactApp, reactOne, reactTwo, reactThree } = getPackageJsonForAllProjects(tree); | ||
|
|
||
| expect(reactOne.dependencies).toEqual({ react: '17.x.x' }); | ||
| expect(reactTwo.dependencies).toEqual({ | ||
| react: '17.x.x', | ||
| '@proj/react-one': '*', | ||
| }); | ||
| expect(reactThree.dependencies).toEqual({ | ||
| react: '17.x.x', | ||
| '@proj/react-one': '^0.1.0', | ||
| '@proj/react-two': '*', | ||
| }); | ||
| expect(reactApp.dependencies).toEqual({ | ||
| react: '17.x.x', | ||
| '@proj/react-one': '*', | ||
| '@proj/react-two': '*', | ||
| }); | ||
| }); | ||
|
|
||
| describe(`filters`, () => { | ||
| describe(`multiple active`, () => { | ||
| it(`should update workspace dependencies that match provided filter options`, async () => { | ||
| await generator(tree, { projectType: 'library', tag: 'type:app' }); | ||
|
|
||
| const { reactApp, reactOne, reactTwo } = getPackageJsonForAllProjects(tree); | ||
|
|
||
| expect(reactOne.dependencies).toEqual({ | ||
| react: '17.x.x', | ||
| }); | ||
| expect(reactTwo.dependencies).toEqual({ | ||
| react: '17.x.x', | ||
| '@proj/react-one': '^1.0.0', | ||
| }); | ||
| expect(reactApp.dependencies).toEqual({ | ||
| react: '17.x.x', | ||
| '@proj/react-one': '^1.0.0', | ||
| '@proj/react-two': '^1.0.0', | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe(`--tag`, () => { | ||
| it(`should update workspace dependencies that have specified tag`, async () => { | ||
| await generator(tree, { tag: 'scope:two' }); | ||
|
|
||
| const { reactApp, reactOne, reactTwo } = getPackageJsonForAllProjects(tree); | ||
|
|
||
| expect(reactOne.dependencies).toEqual({ | ||
| react: '17.x.x', | ||
| }); | ||
| expect(reactTwo.dependencies).toEqual({ | ||
| react: '17.x.x', | ||
| '@proj/react-one': '*', | ||
| }); | ||
| expect(reactApp.dependencies).toEqual({ | ||
| react: '17.x.x', | ||
| '@proj/react-one': '*', | ||
| '@proj/react-two': '*', | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe(`--projectType`, () => { | ||
| it(`should update workspace dependencies only for application`, async () => { | ||
| await generator(tree, { projectType: 'application' }); | ||
|
|
||
| const { reactApp, reactOne, reactTwo } = getPackageJsonForAllProjects(tree); | ||
|
|
||
| expect(reactOne.dependencies).toEqual({ | ||
| react: '17.x.x', | ||
| }); | ||
| expect(reactTwo.dependencies).toEqual({ | ||
| react: '17.x.x', | ||
| '@proj/react-one': '^1.0.0', | ||
| }); | ||
| expect(reactApp.dependencies).toEqual({ | ||
| react: '17.x.x', | ||
| '@proj/react-one': '*', | ||
| '@proj/react-two': '*', | ||
| }); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe(`--verify`, () => { | ||
| it(`should not update any package.json`, async () => { | ||
| try { | ||
| await generator(tree, { verify: true }); | ||
| } catch { | ||
| const { reactApp, reactOne, reactTwo } = getPackageJsonForAllProjects(tree); | ||
|
|
||
| expect(reactOne.dependencies).toEqual({ | ||
| react: '17.x.x', | ||
| }); | ||
| expect(reactTwo.dependencies).toEqual({ | ||
| react: '17.x.x', | ||
| '@proj/react-one': '^1.0.0', | ||
| }); | ||
| expect(reactApp.dependencies).toEqual({ | ||
| react: '17.x.x', | ||
| '@proj/react-one': '^1.0.0', | ||
| '@proj/react-two': '^1.0.0', | ||
| }); | ||
| } | ||
| }); | ||
|
|
||
| it(`should fail if there are any violations`, async () => { | ||
| await expect(generator(tree, { verify: true })).rejects.toThrowErrorMatchingInlineSnapshot( | ||
| `"package dependency violations found"`, | ||
| ); | ||
|
|
||
| expect(logLogSpy.mock.calls.flat()).toMatchInlineSnapshot(` | ||
| Array [ | ||
| "@proj/react-two has following dependency version issues:", | ||
| " - @proj/react-one", | ||
| "@proj/react-three has following dependency version issues:", | ||
| " - @proj/react-two", | ||
| "@proj/react-app has following dependency version issues:", | ||
| " - @proj/react-one", | ||
| " - @proj/react-two", | ||
| ] | ||
| `); | ||
| expect(infoLogSpy.mock.calls.flat()).toMatchInlineSnapshot(` | ||
| Array [ | ||
| "All these dependencies version should be specified as '*'", | ||
| "Fix this by running 'nx workspace-generator normalize-package-dependencies'", | ||
| ] | ||
| `); | ||
| }); | ||
|
|
||
| it(`should fail if there are any violations and filters are used`, async () => { | ||
| await expect( | ||
| generator(tree, { verify: true, projectType: 'application' }), | ||
| ).rejects.toThrowErrorMatchingInlineSnapshot(`"package dependency violations found"`); | ||
|
|
||
| expect(logLogSpy.mock.calls.flat()).toMatchInlineSnapshot(` | ||
| Array [ | ||
| "@proj/react-app has following dependency version issues:", | ||
| " - @proj/react-one", | ||
| " - @proj/react-two", | ||
| ] | ||
| `); | ||
| expect(infoLogSpy.mock.calls.flat()).toMatchInlineSnapshot(` | ||
| Array [ | ||
| "All these dependencies version should be specified as '*'", | ||
| "Fix this by running 'nx workspace-generator normalize-package-dependencies'", | ||
| ] | ||
| `); | ||
|
|
||
| jest.resetAllMocks(); | ||
|
|
||
| await expect(generator(tree, { verify: true, projectType: 'library', tag: 'type:app' })).resolves.toEqual( | ||
| undefined, | ||
| ); | ||
|
|
||
| expect(logLogSpy.mock.calls.flat()).toMatchInlineSnapshot(`Array []`); | ||
| expect(infoLogSpy.mock.calls.flat()).toMatchInlineSnapshot(`Array []`); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| function getPackageJsonForAllProjects(tree: Tree) { | ||
| const projects = getProjects(tree); | ||
|
|
||
| const reactOne = projects.get('@proj/react-one'); | ||
| const reactTwo = projects.get('@proj/react-two'); | ||
| const reactThree = projects.get('@proj/react-three'); | ||
| const reactApp = projects.get('@proj/react-app'); | ||
|
|
||
| return { | ||
| reactOne: readJson<PackageJson>(tree, joinPathFragments(reactOne!.root, 'package.json')), | ||
| reactTwo: readJson<PackageJson>(tree, joinPathFragments(reactTwo!.root, 'package.json')), | ||
| reactThree: readJson<PackageJson>(tree, joinPathFragments(reactThree!.root, 'package.json')), | ||
| reactApp: readJson<PackageJson>(tree, joinPathFragments(reactApp!.root, 'package.json')), | ||
| }; | ||
| } | ||
|
|
||
| function createProject( | ||
| tree: Tree, | ||
| options: { | ||
| projectName: string; | ||
| projectType?: 'application' | 'library'; | ||
| tags?: string[]; | ||
| deps: { prod: Record<string, string>; dev: Record<string, string>; peer: Record<string, string> }; | ||
| }, | ||
| ) { | ||
| const { projectName, deps, tags, projectType = 'library' } = options; | ||
| const packageName = `@proj/${projectName}`; | ||
|
|
||
| const rootPath = `packages/${projectName}`; | ||
|
|
||
| writeJson(tree, `packages/${projectName}/package.json`, { | ||
| name: packageName, | ||
| dependencies: { ...deps.prod }, | ||
| devDependencies: { ...deps.dev }, | ||
| peerDependencies: { ...deps.peer }, | ||
| }); | ||
|
|
||
| addProjectConfiguration(tree, packageName, { | ||
| root: rootPath, | ||
| projectType, | ||
| ...(tags ? { tags } : null), | ||
| }); | ||
|
|
||
| const depKeys = [...Object.keys(deps.prod), ...Object.keys(deps.dev), ...Object.keys(deps.peer)]; | ||
|
|
||
| graphMock.dependencies[packageName] = depKeys.map(value => { | ||
| return { source: packageName, target: value, type: 'static' }; | ||
| }); | ||
| graphMock.nodes[packageName] = { | ||
| name: packageName, | ||
| type: projectType === 'library' ? 'lib' : 'app', | ||
| data: { name: packageName, root: rootPath, files: [] }, | ||
| }; | ||
|
|
||
| return tree; | ||
| } | ||
|
|
||
| function addNonWorkspaceDependency( | ||
| tree: Tree, | ||
| projectName: string, | ||
| dependency: { pkgName: string; pkgVersion: string }, | ||
| ) { | ||
| const project = readProjectConfiguration(tree, projectName); | ||
| updateJson(tree, joinPathFragments(project.root, 'package.json'), json => { | ||
| json.dependencies = json.dependencies ?? {}; | ||
| json.dependencies[dependency.pkgName] = dependency.pkgVersion; | ||
|
|
||
| return json; | ||
| }); | ||
|
|
||
| graphMock.dependencies[projectName].push({ | ||
| source: projectName, | ||
| target: `npm:${dependency.pkgName}`, | ||
| type: 'static', | ||
| }); | ||
|
|
||
| return tree; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.