Skip to content

Commit 5fd0ad2

Browse files
leosvelperezFrozenPandaz
authored andcommitted
fix(misc): fix moving projects with standalone configuration (#6521)
1 parent 90c56aa commit 5fd0ad2

File tree

7 files changed

+117
-1
lines changed

7 files changed

+117
-1
lines changed

docs/angular/api-nx-devkit/index.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
- [getWorkspaceLayout](../../angular/nx-devkit/index#getworkspacelayout)
7373
- [getWorkspacePath](../../angular/nx-devkit/index#getworkspacepath)
7474
- [installPackagesTask](../../angular/nx-devkit/index#installpackagestask)
75+
- [isStandaloneProject](../../angular/nx-devkit/index#isstandaloneproject)
7576
- [joinPathFragments](../../angular/nx-devkit/index#joinpathfragments)
7677
- [moveFilesToNewDirectory](../../angular/nx-devkit/index#movefilestonewdirectory)
7778
- [names](../../angular/nx-devkit/index#names)
@@ -886,6 +887,25 @@ Runs `npm install` or `yarn install`. It will skip running the install if
886887

887888
---
888889

890+
### isStandaloneProject
891+
892+
**isStandaloneProject**(`host`, `project`): `boolean`
893+
894+
Returns if a project has a standalone configuration (project.json).
895+
896+
#### Parameters
897+
898+
| Name | Type | Description |
899+
| :-------- | :------------------------------------------- | :------------------- |
900+
| `host` | [`Tree`](../../angular/nx-devkit/index#tree) | the file system tree |
901+
| `project` | `string` | the project name |
902+
903+
#### Returns
904+
905+
`boolean`
906+
907+
---
908+
889909
### joinPathFragments
890910

891911
**joinPathFragments**(...`fragments`): `string`

docs/node/api-nx-devkit/index.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
- [getWorkspaceLayout](../../node/nx-devkit/index#getworkspacelayout)
7373
- [getWorkspacePath](../../node/nx-devkit/index#getworkspacepath)
7474
- [installPackagesTask](../../node/nx-devkit/index#installpackagestask)
75+
- [isStandaloneProject](../../node/nx-devkit/index#isstandaloneproject)
7576
- [joinPathFragments](../../node/nx-devkit/index#joinpathfragments)
7677
- [moveFilesToNewDirectory](../../node/nx-devkit/index#movefilestonewdirectory)
7778
- [names](../../node/nx-devkit/index#names)
@@ -886,6 +887,25 @@ Runs `npm install` or `yarn install`. It will skip running the install if
886887

887888
---
888889

890+
### isStandaloneProject
891+
892+
**isStandaloneProject**(`host`, `project`): `boolean`
893+
894+
Returns if a project has a standalone configuration (project.json).
895+
896+
#### Parameters
897+
898+
| Name | Type | Description |
899+
| :-------- | :---------------------------------------- | :------------------- |
900+
| `host` | [`Tree`](../../node/nx-devkit/index#tree) | the file system tree |
901+
| `project` | `string` | the project name |
902+
903+
#### Returns
904+
905+
`boolean`
906+
907+
---
908+
889909
### joinPathFragments
890910

891911
**joinPathFragments**(...`fragments`): `string`

docs/react/api-nx-devkit/index.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
- [getWorkspaceLayout](../../react/nx-devkit/index#getworkspacelayout)
7373
- [getWorkspacePath](../../react/nx-devkit/index#getworkspacepath)
7474
- [installPackagesTask](../../react/nx-devkit/index#installpackagestask)
75+
- [isStandaloneProject](../../react/nx-devkit/index#isstandaloneproject)
7576
- [joinPathFragments](../../react/nx-devkit/index#joinpathfragments)
7677
- [moveFilesToNewDirectory](../../react/nx-devkit/index#movefilestonewdirectory)
7778
- [names](../../react/nx-devkit/index#names)
@@ -886,6 +887,25 @@ Runs `npm install` or `yarn install`. It will skip running the install if
886887

887888
---
888889

890+
### isStandaloneProject
891+
892+
**isStandaloneProject**(`host`, `project`): `boolean`
893+
894+
Returns if a project has a standalone configuration (project.json).
895+
896+
#### Parameters
897+
898+
| Name | Type | Description |
899+
| :-------- | :----------------------------------------- | :------------------- |
900+
| `host` | [`Tree`](../../react/nx-devkit/index#tree) | the file system tree |
901+
| `project` | `string` | the project name |
902+
903+
#### Returns
904+
905+
`boolean`
906+
907+
---
908+
889909
### joinPathFragments
890910

891911
**joinPathFragments**(...`fragments`): `string`

packages/devkit/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export {
4141
readWorkspaceConfiguration,
4242
updateWorkspaceConfiguration,
4343
getProjects,
44+
isStandaloneProject,
4445
} from './src/generators/project-configuration';
4546
export { toJS } from './src/generators/to-js';
4647
export { updateTsConfigsToJs } from './src/generators/update-ts-configs-to-js';

packages/devkit/src/generators/project-configuration.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,22 @@ export function readProjectConfiguration(
205205
return getProjectConfiguration(host, projectName, workspace, nxJson);
206206
}
207207

208+
/**
209+
* Returns if a project has a standalone configuration (project.json).
210+
*
211+
* @param host - the file system tree
212+
* @param project - the project name
213+
*/
214+
export function isStandaloneProject(host: Tree, project: string): boolean {
215+
const rawWorkspace = readJson<RawWorkspaceJsonConfiguration>(
216+
host,
217+
getWorkspacePath(host)
218+
);
219+
const projectConfig = rawWorkspace.projects?.[project];
220+
221+
return typeof projectConfig === 'string';
222+
}
223+
208224
function getProjectConfiguration(
209225
host: Tree,
210226
projectName: string,

packages/workspace/src/generators/move/lib/move-project-configuration.spec.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,4 +188,36 @@ describe('moveProjectConfiguration', () => {
188188
expect(actualProject.implicitDependencies).toEqual(['my-other-lib']);
189189
expect(readJson(tree, 'nx.json').projects['my-source']).not.toBeDefined();
190190
});
191+
192+
it('should support moving a standalone project', () => {
193+
const projectName = 'standalone';
194+
const newProjectName = 'parent-standalone';
195+
addProjectConfiguration(
196+
tree,
197+
projectName,
198+
{
199+
projectType: 'library',
200+
root: 'libs/standalone',
201+
targets: {},
202+
},
203+
true
204+
);
205+
const moveSchema: NormalizedSchema = {
206+
projectName: 'standalone',
207+
destination: 'parent/standalone',
208+
importPath: '@proj/parent-standalone',
209+
newProjectName,
210+
relativeToRootDestination: 'libs/parent/standalone',
211+
updateImportPath: true,
212+
};
213+
214+
moveProjectConfiguration(tree, moveSchema, projectConfig);
215+
216+
expect(() => {
217+
readProjectConfiguration(tree, projectName);
218+
}).toThrow();
219+
const ws = readJson(tree, 'workspace.json');
220+
expect(typeof ws.projects[newProjectName]).toBe('string');
221+
expect(readProjectConfiguration(tree, newProjectName)).toBeDefined();
222+
});
191223
});

packages/workspace/src/generators/move/lib/move-project-configuration.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
addProjectConfiguration,
3+
isStandaloneProject,
34
NxJsonProjectConfiguration,
45
ProjectConfiguration,
56
removeProjectConfiguration,
@@ -12,6 +13,7 @@ export function moveProjectConfiguration(
1213
schema: NormalizedSchema,
1314
projectConfig: ProjectConfiguration & NxJsonProjectConfiguration
1415
) {
16+
const isStandalone = isStandaloneProject(tree, schema.projectName);
1517
const projectString = JSON.stringify(projectConfig);
1618
const newProjectString = projectString.replace(
1719
new RegExp(projectConfig.root, 'g'),
@@ -25,5 +27,10 @@ export function moveProjectConfiguration(
2527
removeProjectConfiguration(tree, schema.projectName);
2628

2729
// Create a new project with the root replaced
28-
addProjectConfiguration(tree, schema.newProjectName, newProject);
30+
addProjectConfiguration(
31+
tree,
32+
schema.newProjectName,
33+
newProject,
34+
isStandalone
35+
);
2936
}

0 commit comments

Comments
 (0)