Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 8f62e8d

Browse files
devinshoemakerFrozenPandaz
authored andcommittedAug 18, 2020
fix(core): honor workspace layout with workspace move schematic
1 parent 237d508 commit 8f62e8d

File tree

7 files changed

+64
-37
lines changed

7 files changed

+64
-37
lines changed
 

Diff for: ‎packages/workspace/src/schematics/move/lib/check-destination.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export function checkDestination(schema: Schema): Rule {
2626
);
2727
}
2828

29-
const destination = getDestination(schema, workspace);
29+
const destination = getDestination(schema, workspace, tree);
3030

3131
if (tree.getDir(destination).subfiles.length > 0) {
3232
throw new Error(`${INVALID_DESTINATION} - Path is not empty.`);

Diff for: ‎packages/workspace/src/schematics/move/lib/move-project.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export function moveProject(schema: Schema) {
1616
map((workspace) => {
1717
const project = workspace.projects.get(schema.projectName);
1818

19-
const destination = getDestination(schema, workspace);
19+
const destination = getDestination(schema, workspace, tree);
2020
const dir = tree.getDir(project.root);
2121
dir.visit((file) => {
2222
const newPath = file.replace(project.root, destination);

Diff for: ‎packages/workspace/src/schematics/move/lib/update-cypress-json.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export function updateCypressJson(schema: Schema): Rule {
2424
return from(getWorkspace(tree)).pipe(
2525
map((workspace) => {
2626
const project = workspace.projects.get(schema.projectName);
27-
const destination = getDestination(schema, workspace);
27+
const destination = getDestination(schema, workspace, tree);
2828

2929
const cypressJsonPath = path.join(destination, 'cypress.json');
3030

Diff for: ‎packages/workspace/src/schematics/move/lib/update-jest-config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export function updateJestConfig(schema: Schema): Rule {
1919
return from(getWorkspace(tree)).pipe(
2020
map((workspace) => {
2121
const project = workspace.projects.get(schema.projectName);
22-
const destination = getDestination(schema, workspace);
22+
const destination = getDestination(schema, workspace, tree);
2323
const newProjectName = getNewProjectName(schema.destination);
2424

2525
const jestConfigPath = path.join(destination, 'jest.config.js');

Diff for: ‎packages/workspace/src/schematics/move/lib/update-project-root-files.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export function updateProjectRootFiles(schema: Schema): Rule {
2020
return from(getWorkspace(tree)).pipe(
2121
map((workspace) => {
2222
const project = workspace.projects.get(schema.projectName);
23-
const destination = getDestination(schema, workspace);
23+
const destination = getDestination(schema, workspace, tree);
2424

2525
const newRelativeRoot = path
2626
.relative(path.join(appRootPath, destination), appRootPath)
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { SchematicContext, Tree } from '@angular-devkit/schematics';
12
import { updateWorkspaceInTree } from '@nrwl/workspace';
23
import { Schema } from '../schema';
34
import { getDestination, getNewProjectName } from './utils';
@@ -12,38 +13,40 @@ import { getDestination, getNewProjectName } from './utils';
1213
* @param schema The options provided to the schematic
1314
*/
1415
export function updateWorkspace(schema: Schema) {
15-
return updateWorkspaceInTree((workspace) => {
16-
const project = workspace.projects[schema.projectName];
17-
const newProjectName = getNewProjectName(schema.destination);
16+
return (tree: Tree, _context: SchematicContext) => {
17+
return updateWorkspaceInTree((workspace) => {
18+
const project = workspace.projects[schema.projectName];
19+
const newProjectName = getNewProjectName(schema.destination);
1820

19-
// update root path refs in that project only
20-
const oldProject = JSON.stringify(project);
21-
const newProject = oldProject.replace(
22-
new RegExp(project.root, 'g'),
23-
getDestination(schema, workspace)
24-
);
21+
// update root path refs in that project only
22+
const oldProject = JSON.stringify(project);
23+
const newProject = oldProject.replace(
24+
new RegExp(project.root, 'g'),
25+
getDestination(schema, workspace, tree)
26+
);
2527

26-
// rename
27-
delete workspace.projects[schema.projectName];
28-
workspace.projects[newProjectName] = JSON.parse(newProject);
28+
// rename
29+
delete workspace.projects[schema.projectName];
30+
workspace.projects[newProjectName] = JSON.parse(newProject);
2931

30-
// update target refs
31-
const strWorkspace = JSON.stringify(workspace);
32-
workspace = JSON.parse(
33-
strWorkspace.replace(
34-
new RegExp(`${schema.projectName}:`, 'g'),
35-
`${newProjectName}:`
36-
)
37-
);
32+
// update target refs
33+
const strWorkspace = JSON.stringify(workspace);
34+
workspace = JSON.parse(
35+
strWorkspace.replace(
36+
new RegExp(`${schema.projectName}:`, 'g'),
37+
`${newProjectName}:`
38+
)
39+
);
3840

39-
// update default project (if necessary)
40-
if (
41-
workspace.defaultProject &&
42-
workspace.defaultProject === schema.projectName
43-
) {
44-
workspace.defaultProject = newProjectName;
45-
}
41+
// update default project (if necessary)
42+
if (
43+
workspace.defaultProject &&
44+
workspace.defaultProject === schema.projectName
45+
) {
46+
workspace.defaultProject = newProjectName;
47+
}
4648

47-
return workspace;
48-
});
49+
return workspace;
50+
});
51+
};
4952
}

Diff for: ‎packages/workspace/src/schematics/move/lib/utils.ts

+27-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,28 @@
11
import { WorkspaceDefinition } from '@angular-devkit/core/src/workspace';
2+
import { Tree } from '@angular-devkit/schematics';
3+
import { NxJson } from '@nrwl/workspace/src/core/shared-interfaces';
4+
import { readJsonInTree } from '@nrwl/workspace/src/utils/ast-utils';
25
import * as path from 'path';
36
import { Schema } from '../schema';
47

8+
/**
9+
* This helper function retrieves the users workspace layout from
10+
* `nx.json`. If the user does not have this property defined then
11+
* we assume the default `apps/` and `libs/` layout.
12+
*
13+
* @param host The host tree
14+
*/
15+
export function getWorkspaceLayout(
16+
host: Tree
17+
): { appsDir?: string; libsDir?: string } {
18+
const nxJson = readJsonInTree<NxJson>(host, 'nx.json');
19+
const workspaceLayout = nxJson.workspaceLayout
20+
? nxJson.workspaceLayout
21+
: { appsDir: 'apps', libsDir: 'libs' };
22+
23+
return workspaceLayout;
24+
}
25+
526
/**
627
* This helper function ensures that we don't move libs or apps
728
* outside of the folders they should be in.
@@ -14,7 +35,8 @@ import { Schema } from '../schema';
1435
*/
1536
export function getDestination(
1637
schema: Schema,
17-
workspace: WorkspaceDefinition | any
38+
workspace: WorkspaceDefinition | any,
39+
host: Tree
1840
): string {
1941
const project = workspace.projects.get
2042
? workspace.projects.get(schema.projectName)
@@ -23,9 +45,11 @@ export function getDestination(
2345
? project.extensions['projectType']
2446
: project.projectType;
2547

26-
let rootFolder = 'libs';
48+
const workspaceLayout = getWorkspaceLayout(host);
49+
50+
let rootFolder = workspaceLayout.libsDir;
2751
if (projectType === 'application') {
28-
rootFolder = 'apps';
52+
rootFolder = workspaceLayout.appsDir;
2953
}
3054
return path.join(rootFolder, schema.destination).split(path.sep).join('/');
3155
}

0 commit comments

Comments
 (0)
Please sign in to comment.