Skip to content

Commit

Permalink
chore(core): add config to ng cli adapter to prevent warnings from le…
Browse files Browse the repository at this point in the history
…aking
  • Loading branch information
AgentEnder committed Sep 23, 2023
1 parent 65199bc commit d7c234c
Showing 1 changed file with 74 additions and 6 deletions.
80 changes: 74 additions & 6 deletions packages/nx/src/adapter/compat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,34 @@ 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');
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',
Expand All @@ -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',
Expand All @@ -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<ProjectConfiguration, 'targets' | 'generators'>
>;
}

type Writable<T> = { -readonly [P in keyof T]: T[P] };

if (!patched) {
Module.prototype.require = function () {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit d7c234c

Please sign in to comment.