From c0ec05592bc5f242d5fcdf0fdf37a57cb4a5e296 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CJamesHenry=E2=80=9D?= Date: Thu, 27 Apr 2023 17:53:41 +0100 Subject: [PATCH] fix(core): do not strip additional angular.json properties --- docs/generated/devkit/nrwl_devkit.md | 2 +- .../packages/devkit/documents/nrwl_devkit.md | 2 +- packages/nx/src/adapter/angular-json.ts | 2 +- packages/nx/src/adapter/ngcli-adapter.ts | 14 +++++++++++--- packages/nx/src/utils/workspace-root.ts | 6 +++++- 5 files changed, 19 insertions(+), 7 deletions(-) diff --git a/docs/generated/devkit/nrwl_devkit.md b/docs/generated/devkit/nrwl_devkit.md index 3611c5e69b3d8..ce0df73d52ec0 100644 --- a/docs/generated/devkit/nrwl_devkit.md +++ b/docs/generated/devkit/nrwl_devkit.md @@ -832,7 +832,7 @@ Path to the directory where Nx stores its cache and daemon-related files. ### workspaceRoot -• `Const` **workspaceRoot**: `string` +• **workspaceRoot**: `string` The root of the workspace diff --git a/docs/generated/packages/devkit/documents/nrwl_devkit.md b/docs/generated/packages/devkit/documents/nrwl_devkit.md index 3611c5e69b3d8..ce0df73d52ec0 100644 --- a/docs/generated/packages/devkit/documents/nrwl_devkit.md +++ b/docs/generated/packages/devkit/documents/nrwl_devkit.md @@ -832,7 +832,7 @@ Path to the directory where Nx stores its cache and daemon-related files. ### workspaceRoot -• `Const` **workspaceRoot**: `string` +• **workspaceRoot**: `string` The root of the workspace diff --git a/packages/nx/src/adapter/angular-json.ts b/packages/nx/src/adapter/angular-json.ts index cf55622a9f8a4..ac470a472fb9e 100644 --- a/packages/nx/src/adapter/angular-json.ts +++ b/packages/nx/src/adapter/angular-json.ts @@ -26,7 +26,7 @@ export function shouldMergeAngularProjects( } } -function isAngularPluginInstalled() { +export function isAngularPluginInstalled() { try { require.resolve('@nx/angular'); return true; diff --git a/packages/nx/src/adapter/ngcli-adapter.ts b/packages/nx/src/adapter/ngcli-adapter.ts index 0c3c976ca847a..f201713bbccd0 100644 --- a/packages/nx/src/adapter/ngcli-adapter.ts +++ b/packages/nx/src/adapter/ngcli-adapter.ts @@ -41,7 +41,11 @@ import { parseJson } from '../utils/json'; import { NX_ERROR, NX_PREFIX } from '../utils/logger'; import { readModulePackageJson } from '../utils/package-json'; import { detectPackageManager } from '../utils/package-manager'; -import { toNewFormat, toOldFormat } from './angular-json'; +import { + isAngularPluginInstalled, + toNewFormat, + toOldFormat, +} from './angular-json'; import { normalizeExecutorSchema, Workspaces } from '../config/workspaces'; import { CustomHasher, @@ -899,7 +903,8 @@ export function wrapAngularDevkitSchematic( if (event.kind === 'error') { } else if (event.kind === 'update') { - if (eventPath === 'angular.json') { + // Apply special handling for the angular.json file, but only when in an Nx workspace + if (eventPath === 'angular.json' && isAngularPluginInstalled()) { saveProjectsConfigurationsInWrappedSchematic( host, event.content.toString() @@ -1002,7 +1007,10 @@ function saveProjectsConfigurationsInWrappedSchematic( ? Object.keys(existingAngularJson.projects) : []; - const newAngularJson = { projects: {} }; + const newAngularJson = existingAngularJson || {}; + + // Reset projects in order to rebuild them, but leave other properties untouched + newAngularJson.projects = {}; Object.keys(projects).forEach((projectName) => { if (projectsInAngularJson.includes(projectName)) { diff --git a/packages/nx/src/utils/workspace-root.ts b/packages/nx/src/utils/workspace-root.ts index 64c62d7d23631..7646a3af8e8b4 100644 --- a/packages/nx/src/utils/workspace-root.ts +++ b/packages/nx/src/utils/workspace-root.ts @@ -4,7 +4,11 @@ import { fileExists } from './fileutils'; /** * The root of the workspace */ -export const workspaceRoot = workspaceRootInner(process.cwd(), process.cwd()); +export let workspaceRoot = workspaceRootInner(process.cwd(), process.cwd()); + +export function setWorkspaceRoot(root: string): void { + workspaceRoot = root; +} export function workspaceRootInner( dir: string,