fix(misc): fix moving projects with standalone configuration (#6521)

This commit is contained in:
Leosvel Pérez Espinosa 2021-07-29 19:15:02 +01:00 committed by GitHub
parent dc02ca90b1
commit 7d6837ce41
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 117 additions and 1 deletions

View File

@ -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`

View File

@ -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`

View File

@ -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`

View File

@ -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';

View File

@ -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<RawWorkspaceJsonConfiguration>(
host,
getWorkspacePath(host)
);
const projectConfig = rawWorkspace.projects?.[project];
return typeof projectConfig === 'string';
}
function getProjectConfiguration(
host: Tree,
projectName: string,

View File

@ -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();
});
});

View File

@ -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
);
}