From e2d317d7978596b930c8e71d1628fa3ddd2ddb44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leosvel=20P=C3=A9rez=20Espinosa?= Date: Tue, 27 Jul 2021 12:19:33 +0100 Subject: [PATCH] fix(misc): fix moving projects with standalone configuration --- docs/angular/api-nx-devkit/index.md | 20 ++++++++++++ docs/node/api-nx-devkit/index.md | 20 ++++++++++++ docs/react/api-nx-devkit/index.md | 20 ++++++++++++ packages/devkit/index.ts | 1 + .../src/generators/project-configuration.ts | 16 ++++++++++ .../lib/move-project-configuration.spec.ts | 32 +++++++++++++++++++ .../move/lib/move-project-configuration.ts | 9 +++++- 7 files changed, 117 insertions(+), 1 deletion(-) diff --git a/docs/angular/api-nx-devkit/index.md b/docs/angular/api-nx-devkit/index.md index d385b18b99719..e2093463b36e0 100644 --- a/docs/angular/api-nx-devkit/index.md +++ b/docs/angular/api-nx-devkit/index.md @@ -72,6 +72,7 @@ - [getWorkspaceLayout](../../angular/nx-devkit/index#getworkspacelayout) - [getWorkspacePath](../../angular/nx-devkit/index#getworkspacepath) - [installPackagesTask](../../angular/nx-devkit/index#installpackagestask) +- [isStandaloneProject](../../angular/nx-devkit/index#isstandaloneproject) - [joinPathFragments](../../angular/nx-devkit/index#joinpathfragments) - [moveFilesToNewDirectory](../../angular/nx-devkit/index#movefilestonewdirectory) - [names](../../angular/nx-devkit/index#names) @@ -886,6 +887,25 @@ Runs `npm install` or `yarn install`. It will skip running the install if --- +### isStandaloneProject + +▸ **isStandaloneProject**(`host`, `project`): `boolean` + +Returns if a project has a standalone configuration (project.json). + +#### Parameters + +| Name | Type | Description | +| :-------- | :------------------------------------------- | :------------------- | +| `host` | [`Tree`](../../angular/nx-devkit/index#tree) | the file system tree | +| `project` | `string` | the project name | + +#### Returns + +`boolean` + +--- + ### joinPathFragments ▸ **joinPathFragments**(...`fragments`): `string` diff --git a/docs/node/api-nx-devkit/index.md b/docs/node/api-nx-devkit/index.md index 925472b94e551..1a87c8da94022 100644 --- a/docs/node/api-nx-devkit/index.md +++ b/docs/node/api-nx-devkit/index.md @@ -72,6 +72,7 @@ - [getWorkspaceLayout](../../node/nx-devkit/index#getworkspacelayout) - [getWorkspacePath](../../node/nx-devkit/index#getworkspacepath) - [installPackagesTask](../../node/nx-devkit/index#installpackagestask) +- [isStandaloneProject](../../node/nx-devkit/index#isstandaloneproject) - [joinPathFragments](../../node/nx-devkit/index#joinpathfragments) - [moveFilesToNewDirectory](../../node/nx-devkit/index#movefilestonewdirectory) - [names](../../node/nx-devkit/index#names) @@ -886,6 +887,25 @@ Runs `npm install` or `yarn install`. It will skip running the install if --- +### isStandaloneProject + +▸ **isStandaloneProject**(`host`, `project`): `boolean` + +Returns if a project has a standalone configuration (project.json). + +#### Parameters + +| Name | Type | Description | +| :-------- | :---------------------------------------- | :------------------- | +| `host` | [`Tree`](../../node/nx-devkit/index#tree) | the file system tree | +| `project` | `string` | the project name | + +#### Returns + +`boolean` + +--- + ### joinPathFragments ▸ **joinPathFragments**(...`fragments`): `string` diff --git a/docs/react/api-nx-devkit/index.md b/docs/react/api-nx-devkit/index.md index aa01900ae9e06..06a9277194de1 100644 --- a/docs/react/api-nx-devkit/index.md +++ b/docs/react/api-nx-devkit/index.md @@ -72,6 +72,7 @@ - [getWorkspaceLayout](../../react/nx-devkit/index#getworkspacelayout) - [getWorkspacePath](../../react/nx-devkit/index#getworkspacepath) - [installPackagesTask](../../react/nx-devkit/index#installpackagestask) +- [isStandaloneProject](../../react/nx-devkit/index#isstandaloneproject) - [joinPathFragments](../../react/nx-devkit/index#joinpathfragments) - [moveFilesToNewDirectory](../../react/nx-devkit/index#movefilestonewdirectory) - [names](../../react/nx-devkit/index#names) @@ -886,6 +887,25 @@ Runs `npm install` or `yarn install`. It will skip running the install if --- +### isStandaloneProject + +▸ **isStandaloneProject**(`host`, `project`): `boolean` + +Returns if a project has a standalone configuration (project.json). + +#### Parameters + +| Name | Type | Description | +| :-------- | :----------------------------------------- | :------------------- | +| `host` | [`Tree`](../../react/nx-devkit/index#tree) | the file system tree | +| `project` | `string` | the project name | + +#### Returns + +`boolean` + +--- + ### joinPathFragments ▸ **joinPathFragments**(...`fragments`): `string` diff --git a/packages/devkit/index.ts b/packages/devkit/index.ts index 2bae3d45e4b3d..c2e849886188c 100644 --- a/packages/devkit/index.ts +++ b/packages/devkit/index.ts @@ -41,6 +41,7 @@ export { readWorkspaceConfiguration, updateWorkspaceConfiguration, getProjects, + isStandaloneProject, } from './src/generators/project-configuration'; export { toJS } from './src/generators/to-js'; export { updateTsConfigsToJs } from './src/generators/update-ts-configs-to-js'; diff --git a/packages/devkit/src/generators/project-configuration.ts b/packages/devkit/src/generators/project-configuration.ts index bfa8358f88d8a..49bdbe140e952 100644 --- a/packages/devkit/src/generators/project-configuration.ts +++ b/packages/devkit/src/generators/project-configuration.ts @@ -205,6 +205,22 @@ export function readProjectConfiguration( return getProjectConfiguration(host, projectName, workspace, nxJson); } +/** + * Returns if a project has a standalone configuration (project.json). + * + * @param host - the file system tree + * @param project - the project name + */ +export function isStandaloneProject(host: Tree, project: string): boolean { + const rawWorkspace = readJson( + host, + getWorkspacePath(host) + ); + const projectConfig = rawWorkspace.projects?.[project]; + + return typeof projectConfig === 'string'; +} + function getProjectConfiguration( host: Tree, projectName: string, diff --git a/packages/workspace/src/generators/move/lib/move-project-configuration.spec.ts b/packages/workspace/src/generators/move/lib/move-project-configuration.spec.ts index 3234ed2df10c7..c9b42e2493b1d 100644 --- a/packages/workspace/src/generators/move/lib/move-project-configuration.spec.ts +++ b/packages/workspace/src/generators/move/lib/move-project-configuration.spec.ts @@ -188,4 +188,36 @@ describe('moveProjectConfiguration', () => { expect(actualProject.implicitDependencies).toEqual(['my-other-lib']); expect(readJson(tree, 'nx.json').projects['my-source']).not.toBeDefined(); }); + + it('should support moving a standalone project', () => { + const projectName = 'standalone'; + const newProjectName = 'parent-standalone'; + addProjectConfiguration( + tree, + projectName, + { + projectType: 'library', + root: 'libs/standalone', + targets: {}, + }, + true + ); + const moveSchema: NormalizedSchema = { + projectName: 'standalone', + destination: 'parent/standalone', + importPath: '@proj/parent-standalone', + newProjectName, + relativeToRootDestination: 'libs/parent/standalone', + updateImportPath: true, + }; + + moveProjectConfiguration(tree, moveSchema, projectConfig); + + expect(() => { + readProjectConfiguration(tree, projectName); + }).toThrow(); + const ws = readJson(tree, 'workspace.json'); + expect(typeof ws.projects[newProjectName]).toBe('string'); + expect(readProjectConfiguration(tree, newProjectName)).toBeDefined(); + }); }); diff --git a/packages/workspace/src/generators/move/lib/move-project-configuration.ts b/packages/workspace/src/generators/move/lib/move-project-configuration.ts index 7707b410aef58..cbd60bae1bc44 100644 --- a/packages/workspace/src/generators/move/lib/move-project-configuration.ts +++ b/packages/workspace/src/generators/move/lib/move-project-configuration.ts @@ -1,5 +1,6 @@ import { addProjectConfiguration, + isStandaloneProject, NxJsonProjectConfiguration, ProjectConfiguration, removeProjectConfiguration, @@ -12,6 +13,7 @@ export function moveProjectConfiguration( schema: NormalizedSchema, projectConfig: ProjectConfiguration & NxJsonProjectConfiguration ) { + const isStandalone = isStandaloneProject(tree, schema.projectName); const projectString = JSON.stringify(projectConfig); const newProjectString = projectString.replace( new RegExp(projectConfig.root, 'g'), @@ -25,5 +27,10 @@ export function moveProjectConfiguration( removeProjectConfiguration(tree, schema.projectName); // Create a new project with the root replaced - addProjectConfiguration(tree, schema.newProjectName, newProject); + addProjectConfiguration( + tree, + schema.newProjectName, + newProject, + isStandalone + ); }