From d7c234c25f83ef7a965fe94fe99c6e634c0530ff Mon Sep 17 00:00:00 2001 From: AgentEnder Date: Fri, 22 Sep 2023 23:21:13 -0400 Subject: [PATCH] chore(core): add config to ng cli adapter to prevent warnings from leaking --- packages/nx/src/adapter/compat.ts | 80 ++++++++++++++++++++++++++++--- 1 file changed, 74 insertions(+), 6 deletions(-) diff --git a/packages/nx/src/adapter/compat.ts b/packages/nx/src/adapter/compat.ts index 7853a9bd8f2567..a6e2d5891dd009 100644 --- a/packages/nx/src/adapter/compat.ts +++ b/packages/nx/src/adapter/compat.ts @@ -2,10 +2,14 @@ import { createProjectGraphAsync, readProjectsConfigurationFromProjectGraph, } from '../project-graph/project-graph'; -import { ProjectsConfigurations } from '../config/workspace-json-project-json'; +import { + ProjectConfiguration, + ProjectsConfigurations, +} from '../config/workspace-json-project-json'; import { readNxJson } from '../config/configuration'; import { NxJsonConfiguration } from '../config/nx-json'; import { toOldFormat } from './angular-json'; +import type { JsonWorkspaceOptions } from '@angular-devkit/core/src/workspace/json/reader'; /* eslint-disable */ const Module = require('module'); @@ -13,7 +17,19 @@ const originalRequire: NodeRequire = Module.prototype.require; let patched = false; +/** + * Additional properties that may appear in a project configuration + * without Angular CLI warning. If a property that is on nx config + * is not listed here, and angular sees it, it will visibly warn the user + * + * NOTE: there is no penalty if a property that angular CLI + * would already know about is also listed here, so better to have + * more than less. + */ const allowedProjectExtensions = [ + 'root', + 'sourceRoot', + 'projectType', 'tags', 'implicitDependencies', 'configFilePath', @@ -22,8 +38,17 @@ const allowedProjectExtensions = [ 'namedInputs', 'name', 'files', -]; +] as const; +/** + * Additional properties that may appear on the workspace configuration + * without Angular CLI warning. If a property that is on nx config + * is not listed here, and angular sees it, it will visibly warn the user + * + * NOTE: there is no penalty if a property that angular CLI + * would already know about is also listed here, so better to have + * more than less. + */ const allowedWorkspaceExtensions = [ 'implicitDependencies', 'affected', @@ -35,7 +60,46 @@ const allowedWorkspaceExtensions = [ 'files', 'generators', 'namedInputs', -]; + 'accessToken', + 'extends', + 'cli', + 'pluginsConfig', + 'defaultProject', + 'installation', + 'release', + 'parallel', + 'cacheDirectory', + 'useDaemonProcess', +] as const; + +// The below section looks a bit confusing but ensures that all +// properties on nx.json appear as members of the allowedWorkspaceExtensions +// array. +// +// These get compiled away since they are all just interfaces and types. +// They are in a scope s.t. they aren't used elsewhere. +{ + // This type throws an error if any item in `Keys` + // is not present in `Arr`. + interface CheckForMissing< + Arr extends readonly unknown[], + Keys extends Arr[number] + > {} + + type CheckWorkspaceKeys = CheckForMissing< + typeof allowedWorkspaceExtensions, + keyof NxJsonConfiguration + >; + type CheckProjectKeys = CheckForMissing< + typeof allowedProjectExtensions, + // We want a visible warning if targets or generators is passed + // to angular CLI, as that means our compat layer is not + // working properly. + keyof Omit + >; +} + +type Writable = { -readonly [P in keyof T]: T[P] }; if (!patched) { Module.prototype.require = function () { @@ -91,10 +155,14 @@ function mockReadJsonWorkspace( readJsonUtils, 'readJsonWorkspace', (originalReadJsonWorkspace) => async (path, host, options) => { - const modifiedOptions = { + const modifiedOptions: JsonWorkspaceOptions = { ...options, - allowedProjectExtensions, - allowedWorkspaceExtensions, + allowedProjectExtensions: allowedProjectExtensions as Writable< + typeof allowedProjectExtensions + >, + allowedWorkspaceExtensions: allowedWorkspaceExtensions as Writable< + typeof allowedWorkspaceExtensions + >, }; try { // Attempt angular CLI default behaviour