diff --git a/packages/workspace/src/schematics/move/lib/check-destination.ts b/packages/workspace/src/schematics/move/lib/check-destination.ts index e22184e0ab..f639d5f27e 100644 --- a/packages/workspace/src/schematics/move/lib/check-destination.ts +++ b/packages/workspace/src/schematics/move/lib/check-destination.ts @@ -26,7 +26,7 @@ export function checkDestination(schema: Schema): Rule { ); } - const destination = getDestination(schema, workspace); + const destination = getDestination(schema, workspace, tree); if (tree.getDir(destination).subfiles.length > 0) { throw new Error(`${INVALID_DESTINATION} - Path is not empty.`); diff --git a/packages/workspace/src/schematics/move/lib/move-project.ts b/packages/workspace/src/schematics/move/lib/move-project.ts index d111ddac0b..4babf5e887 100644 --- a/packages/workspace/src/schematics/move/lib/move-project.ts +++ b/packages/workspace/src/schematics/move/lib/move-project.ts @@ -16,7 +16,7 @@ export function moveProject(schema: Schema) { map((workspace) => { const project = workspace.projects.get(schema.projectName); - const destination = getDestination(schema, workspace); + const destination = getDestination(schema, workspace, tree); const dir = tree.getDir(project.root); dir.visit((file) => { const newPath = file.replace(project.root, destination); diff --git a/packages/workspace/src/schematics/move/lib/update-cypress-json.ts b/packages/workspace/src/schematics/move/lib/update-cypress-json.ts index a5756397e7..bc0ee760e2 100644 --- a/packages/workspace/src/schematics/move/lib/update-cypress-json.ts +++ b/packages/workspace/src/schematics/move/lib/update-cypress-json.ts @@ -24,7 +24,7 @@ export function updateCypressJson(schema: Schema): Rule { return from(getWorkspace(tree)).pipe( map((workspace) => { const project = workspace.projects.get(schema.projectName); - const destination = getDestination(schema, workspace); + const destination = getDestination(schema, workspace, tree); const cypressJsonPath = path.join(destination, 'cypress.json'); diff --git a/packages/workspace/src/schematics/move/lib/update-jest-config.ts b/packages/workspace/src/schematics/move/lib/update-jest-config.ts index 363f00d5f9..d31a7ce765 100644 --- a/packages/workspace/src/schematics/move/lib/update-jest-config.ts +++ b/packages/workspace/src/schematics/move/lib/update-jest-config.ts @@ -19,7 +19,7 @@ export function updateJestConfig(schema: Schema): Rule { return from(getWorkspace(tree)).pipe( map((workspace) => { const project = workspace.projects.get(schema.projectName); - const destination = getDestination(schema, workspace); + const destination = getDestination(schema, workspace, tree); const newProjectName = getNewProjectName(schema.destination); const jestConfigPath = path.join(destination, 'jest.config.js'); diff --git a/packages/workspace/src/schematics/move/lib/update-project-root-files.ts b/packages/workspace/src/schematics/move/lib/update-project-root-files.ts index 77bb45c028..a372e99a75 100644 --- a/packages/workspace/src/schematics/move/lib/update-project-root-files.ts +++ b/packages/workspace/src/schematics/move/lib/update-project-root-files.ts @@ -20,7 +20,7 @@ export function updateProjectRootFiles(schema: Schema): Rule { return from(getWorkspace(tree)).pipe( map((workspace) => { const project = workspace.projects.get(schema.projectName); - const destination = getDestination(schema, workspace); + const destination = getDestination(schema, workspace, tree); const newRelativeRoot = path .relative(path.join(appRootPath, destination), appRootPath) diff --git a/packages/workspace/src/schematics/move/lib/update-workspace.ts b/packages/workspace/src/schematics/move/lib/update-workspace.ts index 52ba38fa99..af2559fadb 100644 --- a/packages/workspace/src/schematics/move/lib/update-workspace.ts +++ b/packages/workspace/src/schematics/move/lib/update-workspace.ts @@ -1,3 +1,4 @@ +import { SchematicContext, Tree } from '@angular-devkit/schematics'; import { updateWorkspaceInTree } from '@nrwl/workspace'; import { Schema } from '../schema'; import { getDestination, getNewProjectName } from './utils'; @@ -12,38 +13,40 @@ import { getDestination, getNewProjectName } from './utils'; * @param schema The options provided to the schematic */ export function updateWorkspace(schema: Schema) { - return updateWorkspaceInTree((workspace) => { - const project = workspace.projects[schema.projectName]; - const newProjectName = getNewProjectName(schema.destination); + return (tree: Tree, _context: SchematicContext) => { + return updateWorkspaceInTree((workspace) => { + const project = workspace.projects[schema.projectName]; + const newProjectName = getNewProjectName(schema.destination); - // update root path refs in that project only - const oldProject = JSON.stringify(project); - const newProject = oldProject.replace( - new RegExp(project.root, 'g'), - getDestination(schema, workspace) - ); + // update root path refs in that project only + const oldProject = JSON.stringify(project); + const newProject = oldProject.replace( + new RegExp(project.root, 'g'), + getDestination(schema, workspace, tree) + ); - // rename - delete workspace.projects[schema.projectName]; - workspace.projects[newProjectName] = JSON.parse(newProject); + // rename + delete workspace.projects[schema.projectName]; + workspace.projects[newProjectName] = JSON.parse(newProject); - // update target refs - const strWorkspace = JSON.stringify(workspace); - workspace = JSON.parse( - strWorkspace.replace( - new RegExp(`${schema.projectName}:`, 'g'), - `${newProjectName}:` - ) - ); + // update target refs + const strWorkspace = JSON.stringify(workspace); + workspace = JSON.parse( + strWorkspace.replace( + new RegExp(`${schema.projectName}:`, 'g'), + `${newProjectName}:` + ) + ); - // update default project (if necessary) - if ( - workspace.defaultProject && - workspace.defaultProject === schema.projectName - ) { - workspace.defaultProject = newProjectName; - } + // update default project (if necessary) + if ( + workspace.defaultProject && + workspace.defaultProject === schema.projectName + ) { + workspace.defaultProject = newProjectName; + } - return workspace; - }); + return workspace; + }); + }; } diff --git a/packages/workspace/src/schematics/move/lib/utils.ts b/packages/workspace/src/schematics/move/lib/utils.ts index 79e77db398..32d261b397 100644 --- a/packages/workspace/src/schematics/move/lib/utils.ts +++ b/packages/workspace/src/schematics/move/lib/utils.ts @@ -1,7 +1,28 @@ import { WorkspaceDefinition } from '@angular-devkit/core/src/workspace'; +import { Tree } from '@angular-devkit/schematics'; +import { NxJson } from '@nrwl/workspace/src/core/shared-interfaces'; +import { readJsonInTree } from '@nrwl/workspace/src/utils/ast-utils'; import * as path from 'path'; import { Schema } from '../schema'; +/** + * This helper function retrieves the users workspace layout from + * `nx.json`. If the user does not have this property defined then + * we assume the default `apps/` and `libs/` layout. + * + * @param host The host tree + */ +export function getWorkspaceLayout( + host: Tree +): { appsDir?: string; libsDir?: string } { + const nxJson = readJsonInTree(host, 'nx.json'); + const workspaceLayout = nxJson.workspaceLayout + ? nxJson.workspaceLayout + : { appsDir: 'apps', libsDir: 'libs' }; + + return workspaceLayout; +} + /** * This helper function ensures that we don't move libs or apps * outside of the folders they should be in. @@ -14,7 +35,8 @@ import { Schema } from '../schema'; */ export function getDestination( schema: Schema, - workspace: WorkspaceDefinition | any + workspace: WorkspaceDefinition | any, + host: Tree ): string { const project = workspace.projects.get ? workspace.projects.get(schema.projectName) @@ -23,9 +45,11 @@ export function getDestination( ? project.extensions['projectType'] : project.projectType; - let rootFolder = 'libs'; + const workspaceLayout = getWorkspaceLayout(host); + + let rootFolder = workspaceLayout.libsDir; if (projectType === 'application') { - rootFolder = 'apps'; + rootFolder = workspaceLayout.appsDir; } return path.join(rootFolder, schema.destination).split(path.sep).join('/'); }