Skip to content

Commit 6398a06

Browse files
authored
fix(core): move generator not updating paths correctly (#767)
Co-authored-by: Chris Leigh <chris.leigh@securitas.com>
1 parent 72b7c15 commit 6398a06

File tree

2 files changed

+56
-13
lines changed

2 files changed

+56
-13
lines changed

packages/core/src/generators/move/generator.spec.ts

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import {
55
addProjectConfiguration,
66
joinPathFragments,
77
names,
8+
offsetFromRoot,
9+
ProjectConfiguration,
810
} from '@nrwl/devkit';
911
import { uniq } from '@nrwl/nx-plugin/testing';
1012

@@ -29,8 +31,8 @@ describe('move generator', () => {
2931
expect(tree.exists(`apps/${destination}/readme.md`)).toBeTruthy();
3032
});
3133

32-
it('should move simple projects down a directory', async () => {
33-
const { project } = makeSimpleProject(tree, 'app', 'apps/libs/test');
34+
it('should move simple projects up a directory', async () => {
35+
const { project } = makeSimpleProject(tree, 'app', 'test');
3436
const destination = uniq('app');
3537
await generator(tree, { projectName: project, destination });
3638
const config = readProjectConfiguration(tree, destination);
@@ -39,8 +41,8 @@ describe('move generator', () => {
3941
expect(tree.exists(`apps/libs/test/readme.md`)).toBeFalsy();
4042
});
4143

42-
it('should move simple projects up a directory', async () => {
43-
const { project } = makeSimpleProject(tree, 'app', 'apps/test');
44+
it('should move simple projects down a directory', async () => {
45+
const { project } = makeSimpleProject(tree, 'app', 'test');
4446
const destination = joinPathFragments('test', 'nested', uniq('app'));
4547
await generator(tree, { projectName: project, destination });
4648
const config = readProjectConfiguration(
@@ -53,15 +55,17 @@ describe('move generator', () => {
5355
});
5456

5557
it('should update references in .csproj files', async () => {
56-
const { project, root } = makeSimpleProject(tree, 'app', 'apps/test');
58+
const { project, root } = makeSimpleProject(tree, 'app', 'test');
5759
const csProjPath = 'apps/other/Other.csproj';
5860
tree.write(
5961
csProjPath,
6062
`<Project Sdk="Microsoft.NET.Sdk">
6163
<ItemGroup>
62-
<ProjectReference Include="../test/${names(project).className}.csproj" />
64+
<ProjectReference Include="../test/${project}/${
65+
names(project).className
66+
}.csproj" />
6367
</ItemGroup>
64-
68+
6569
<PropertyGroup>
6670
<TargetFramework>net6.0</TargetFramework>
6771
<RootNamespace>test_lib2</RootNamespace>
@@ -86,15 +90,38 @@ describe('move generator', () => {
8690
expect(updatedCsProj).not.toContain(project);
8791
expect(updatedCsProj).toContain(basename(destination));
8892
});
93+
94+
it('should update paths in project configuration', async () => {
95+
const { project, root: source } = makeSimpleProject(tree, 'app', 'a/b');
96+
const destination = joinPathFragments('a', 'b', 'c', uniq('app'));
97+
await generator(tree, { projectName: project, destination });
98+
const config: ProjectConfiguration & { $schema?: string } =
99+
readProjectConfiguration(tree, destination.replace(/[\\|/]/g, '-'));
100+
expect(config).toBeDefined();
101+
expect(JSON.stringify(config)).not.toContain(source);
102+
expect(JSON.stringify(config)).toContain(destination);
103+
expect(config.root.endsWith(destination)).toBeTruthy();
104+
expect(config.sourceRoot?.startsWith(config.root)).toBeTruthy();
105+
const relativeToRoot = offsetFromRoot(config.root);
106+
expect(config.$schema).toMatch(
107+
new RegExp(`^${joinPathFragments(relativeToRoot, 'node_modules')}.*`),
108+
);
109+
});
89110
});
90111

91112
function makeSimpleProject(tree: Tree, type: 'app' | 'lib', path?: string) {
92113
const project = uniq(type);
93-
const root = path ? path.replaceAll('{n}', project) : `${type}s/${project}`;
114+
const root = joinPathFragments(`${type}s`, path ?? '', project);
94115
addProjectConfiguration(tree, project, {
95-
root: root,
116+
root,
117+
sourceRoot: joinPathFragments(root, 'src'),
96118
projectType: type === 'app' ? 'application' : 'library',
97-
targets: { 'my-target': { executor: 'nx:noop' } },
119+
targets: {
120+
'my-target': {
121+
executor: 'nx:noop',
122+
outputs: [`{workspaceRoot}/dist/${root}`],
123+
},
124+
},
98125
});
99126
tree.write(joinPathFragments(root, 'readme.md'), 'contents');
100127
return { project, root };

packages/core/src/generators/move/generator.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
joinPathFragments,
66
names,
77
normalizePath,
8+
offsetFromRoot,
89
ProjectConfiguration,
910
readProjectConfiguration,
1011
removeProjectConfiguration,
@@ -70,17 +71,32 @@ export default async function (tree: Tree, options: MoveGeneratorSchema) {
7071
addProjectConfiguration(
7172
tree,
7273
options.projectName,
73-
transformConfiguration(config, normalizedOptions),
74+
transformConfiguration(tree, config, normalizedOptions),
7475
);
7576
updateXmlReferences(tree, normalizedOptions);
7677
await formatFiles(tree);
7778
}
7879

7980
function transformConfiguration(
81+
tree: Tree,
8082
config: ProjectConfiguration,
8183
options: NormalizedSchema,
8284
) {
83-
return updateReferencesInObject(config, options);
85+
const copy = updateReferencesInObject(config, options);
86+
updateSchemaPath(tree, copy, config.root);
87+
return copy;
88+
}
89+
90+
function updateSchemaPath(
91+
tree: Tree,
92+
config: ProjectConfiguration & { $schema?: string },
93+
projectRoot: string,
94+
) {
95+
const relativeToRoot = offsetFromRoot(projectRoot);
96+
config.$schema = config.$schema?.replace(
97+
/^.*\/node_modules/,
98+
joinPathFragments(relativeToRoot, 'node_modules'),
99+
);
84100
}
85101

86102
function updateReferencesInObject<
@@ -94,7 +110,7 @@ function updateReferencesInObject<
94110
for (const key in object) {
95111
if (typeof object[key] === 'string') {
96112
newValue[key] = (object[key] as string).replace(
97-
options.currentProject,
113+
options.currentRoot,
98114
options.destinationRoot,
99115
);
100116
} else if (typeof object[key] === 'object') {

0 commit comments

Comments
 (0)