nx/packages/angular/src/migrations/update-12-9-0/update-invalid-import-paths.ts
Craigory Coppola 79cf69b4e4
feat(core): consolidate nx.json and workspace.json (#6642)
* feat(core): consolidate settings between workspace.json + nx.json

workspace.json (and linked project.json files) now contain all project specific settings.
nx.json now contains all settings that affect the whole workspace.

* chore(core): fix angular unit tests w/ new config

* chore(core): fix failing tests

* chore(core): fix formatting

* chore(core): fix more tests

* chore(core): normalize-nx-json feedback

* chore(core): Fix more unit tests

* chore(core): fix remaining tests in workspace

* chore(linter): fix remaining linter tests

* chore(core): fix remaining spec + build issues

* chore(core): formatting fixes

* feat(core): migration script to move config options to new locations

* chore(core): fix e2e tests

* chore(core): run format

* chore(react-native): fix failing tests

Signed-off-by: AgentEnder <craigorycoppola@gmail.com>

* feat(core): move properties to new location during format step

Signed-off-by: AgentEnder <craigorycoppola@gmail.com>

* feat(core): initial pass on ngcli-adapter for property consolidation

Signed-off-by: AgentEnder <craigorycoppola@gmail.com>

* chore(misc): fix tests

Signed-off-by: AgentEnder <craigorycoppola@gmail.com>

* docs(core): update docs with changes

* chore(misc): fix tests

* chore(core): code review changes

updateWorkspaceJson -> updateWorkspace, no longer uses updater function

Signed-off-by: AgentEnder <craigorycoppola@gmail.com>

* chore(core): fix bug in ngcli impl

* fix(core): fix bug in ngcli-adapter

Signed-off-by: AgentEnder <craigorycoppola@gmail.com>

* fix(core): fix ngcli-adapter

Signed-off-by: AgentEnder <craigorycoppola@gmail.com>

* chore(core): fix workspace e2e

* chore(core): fix nx-plugin e2e

* fix(core): move defaultProject to nx.json

* chore(core): fix broken workspace test

* chore(core): formatting

* chore(core): fix workspace format

* chore(core): add nxJson to ExecutorContext

Signed-off-by: AgentEnder <craigorycoppola@gmail.com>

* chore(core): remove all references ot `NxProjectConfiguration` from our code

* chore(core): Review Changes

* fix(core): update new config locations v13 migration
2021-10-14 10:42:47 -04:00

162 lines
4.2 KiB
TypeScript

import type { ProjectConfiguration, Tree } from '@nrwl/devkit';
import {
formatFiles,
getProjects,
readJson,
joinPathFragments,
updateJson,
logger,
} from '@nrwl/devkit';
type AffectedLib = ProjectConfiguration;
type InvalidLibs = {
buildableLibs: AffectedLib[];
publishableLibs: AffectedLib[];
};
export default async function (tree: Tree) {
const tsconfigPath = getBaseTsConfigPath(tree);
if (!tree.exists(tsconfigPath)) {
logger.error(
'Could not find `tsconfig.base.json` or `tsconfig.json` at the root of the workspace. Skipping this migration...'
);
return;
}
const possibleAffectedLibs = findBuildableAndPublishableLibs(tree);
const invalidLibs = findInvalidLibs(tree, possibleAffectedLibs, tsconfigPath);
fixLibs(tree, invalidLibs, tsconfigPath);
await formatFiles(tree);
}
export function findBuildableAndPublishableLibs(tree: Tree): InvalidLibs {
const projects = getProjects(tree);
const buildableLibs: AffectedLib[] = [];
const publishableLibs: AffectedLib[] = [];
for (const [name, project] of projects) {
for (const target of Object.values(project.targets || {})) {
if (target.executor === '@nrwl/angular:package') {
publishableLibs.push(project);
} else if (target.executor === '@nrwl/angular:ng-packagr-lite') {
buildableLibs.push(project);
}
}
}
return { buildableLibs, publishableLibs };
}
export function findInvalidLibs(
tree: Tree,
libs: InvalidLibs,
tsconfigPath: string
): InvalidLibs {
const { compilerOptions } = readJson(tree, tsconfigPath);
const { paths: tsConfigPaths } = compilerOptions;
const invalidBuildableLibs = libs.buildableLibs.filter((lib) =>
checkInvalidLib(tree, lib, tsConfigPaths)
);
const invalidPublishableLibs = libs.publishableLibs.filter((lib) =>
checkInvalidLib(tree, lib, tsConfigPaths)
);
return {
buildableLibs: invalidBuildableLibs,
publishableLibs: invalidPublishableLibs,
};
}
function checkInvalidLib(
tree: Tree,
lib: AffectedLib,
tsConfigPaths: Record<string, string>
) {
const { name } = readJson(tree, joinPathFragments(lib.root, 'package.json'));
return !tsConfigPaths[name];
}
function fixLibs(
tree: Tree,
{ buildableLibs, publishableLibs }: InvalidLibs,
tsconfigFilePath: string
) {
const { compilerOptions } = readJson(tree, tsconfigFilePath);
const { paths: tsConfigPaths } = compilerOptions;
buildableLibs.map((lib) => fixBuildableLib(tree, lib, tsConfigPaths));
publishableLibs.map((lib) =>
fixPublishableLib(tree, lib, tsConfigPaths, tsconfigFilePath)
);
}
function fixBuildableLib(
tree: Tree,
lib: AffectedLib,
tsConfigPaths: Record<string, string>
) {
const srcRoot = joinPathFragments(lib.sourceRoot, 'index.ts');
for (const [validPackageName, tsLibSrcRoot] of Object.entries(
tsConfigPaths
)) {
if (tsLibSrcRoot[0] === srcRoot) {
updateJson(
tree,
joinPathFragments(lib.root, 'package.json'),
(pkgJson) => {
pkgJson.name = validPackageName;
return pkgJson;
}
);
break;
}
}
}
function fixPublishableLib(
tree: Tree,
lib: AffectedLib,
tsConfigPaths: Record<string, string>,
tsconfigFilePath: string
) {
const srcRoot = joinPathFragments(lib.sourceRoot, 'index.ts');
const { name: pkgName } = readJson(
tree,
joinPathFragments(lib.root, 'package.json')
);
const pkgNameParts = pkgName.split('/');
if (Array.isArray(pkgNameParts) && pkgNameParts.length > 2) {
logger.warn(
`Your publishable package ${pkgName} is an invalid NPM Package name. Please ensure it only contains one '/'.`
);
logger.warn(
`The affected package.json is at '${joinPathFragments(
lib.root,
'package.json'
)}'`
);
}
for (const [invalidPathKey, tsLibSrcRoot] of Object.entries(tsConfigPaths)) {
if (tsLibSrcRoot[0] === srcRoot) {
updateJson(tree, tsconfigFilePath, (tsconfig) => {
tsconfig.compilerOptions.paths[invalidPathKey] = undefined;
tsconfig.compilerOptions.paths[pkgName] = tsLibSrcRoot;
return tsconfig;
});
break;
}
}
}
function getBaseTsConfigPath(tree: Tree) {
return tree.exists('tsconfig.base.json')
? 'tsconfig.base.json'
: 'tsconfig.json';
}