From a10cf176d5354bdb7616b9e2cfb565f3e8dd9607 Mon Sep 17 00:00:00 2001 From: Kerick Howlett <88661181+KerickHowlett@users.noreply.github.com> Date: Mon, 7 Oct 2024 19:30:23 -0400 Subject: [PATCH] fix(core): allow for non-js libs to be moved without errors or the creation of any unneeded tsconfig files closed #28349 --- .../src/generators/move/lib/update-imports.ts | 47 ++++++++++--------- packages/workspace/src/utilities/ts-config.ts | 21 ++++++--- 2 files changed, 38 insertions(+), 30 deletions(-) diff --git a/packages/workspace/src/generators/move/lib/update-imports.ts b/packages/workspace/src/generators/move/lib/update-imports.ts index 712fd0dc2c37a..d1eba3ec55b06 100644 --- a/packages/workspace/src/generators/move/lib/update-imports.ts +++ b/packages/workspace/src/generators/move/lib/update-imports.ts @@ -1,13 +1,13 @@ import { - ChangeType, - ProjectConfiguration, - StringChange, - Tree, applyChangesToString, + ChangeType, getProjects, getWorkspaceLayout, joinPathFragments, + ProjectConfiguration, readJson, + StringChange, + Tree, visitNotIgnoredFiles, writeJson, } from '@nx/devkit'; @@ -17,6 +17,8 @@ import { getImportPath } from '../../../utilities/get-import-path'; import { findNodes, getRootTsConfigPathInTree, + TSConfig, + type TSConfigFileName, } from '../../../utilities/ts-config'; import { ensureTypescript } from '../../../utilities/typescript'; import { NormalizedSchema } from '../schema'; @@ -43,28 +45,29 @@ export function updateImports( // use the source root to find the from location // this attempts to account for libs that have been created with --importPath - const tsConfigPath = getRootTsConfigPathInTree(tree); - let tsConfig: any; - let mainEntryPointImportPath: string; - let secondaryEntryPointImportPaths: string[]; - let serverEntryPointImportPath: string; + const tsConfigPath: TSConfigFileName | null = getRootTsConfigPathInTree(tree); + let tsConfig: TSConfig | undefined; + let paths: string[] = []; + + let mainEntryPointImportPath: string | undefined; + let secondaryEntryPointImportPaths: string[] = []; + let serverEntryPointImportPath: string | undefined; + if (tree.exists(tsConfigPath)) { tsConfig = readJson(tree, tsConfigPath); const sourceRoot = project.sourceRoot ?? joinPathFragments(project.root, 'src'); - mainEntryPointImportPath = Object.keys( - tsConfig.compilerOptions?.paths ?? {} - ).find((path) => + paths = Object.keys(tsConfig?.compilerOptions.paths ?? {}); + + mainEntryPointImportPath = paths.find((path) => tsConfig.compilerOptions.paths[path].some((x) => x.startsWith(ensureTrailingSlash(sourceRoot)) ) ); - secondaryEntryPointImportPaths = Object.keys( - tsConfig.compilerOptions?.paths ?? {} - ).filter((path) => + secondaryEntryPointImportPaths = paths.filter((path) => tsConfig.compilerOptions.paths[path].some( - (x) => + (x: string) => x.startsWith(ensureTrailingSlash(project.root)) && !x.startsWith(ensureTrailingSlash(sourceRoot)) ) @@ -72,9 +75,7 @@ export function updateImports( // Next.js libs have a custom path for the server we need to update that as well // example "paths": { @acme/lib/server : ['libs/lib/src/server.ts'] } - serverEntryPointImportPath = Object.keys( - tsConfig.compilerOptions?.paths ?? {} - ).find((path) => + serverEntryPointImportPath = paths.find((path) => tsConfig.compilerOptions.paths[path].some( (x) => x.startsWith(ensureTrailingSlash(sourceRoot)) && @@ -148,8 +149,8 @@ export function updateImports( to: schema.relativeToRootDestination, }; - if (tsConfig) { - const path = tsConfig.compilerOptions.paths[projectRef.from] as string[]; + if (tsConfig && paths.length > 0) { + const path = tsConfig.compilerOptions.paths[projectRef.from]; if (!path) { throw new Error( [ @@ -170,9 +171,9 @@ export function updateImports( } else { tsConfig.compilerOptions.paths[projectRef.from] = updatedPath; } - } - writeJson(tree, tsConfigPath, tsConfig); + writeJson(tree, tsConfigPath, tsConfig); + } } } diff --git a/packages/workspace/src/utilities/ts-config.ts b/packages/workspace/src/utilities/ts-config.ts index 927741226fc3c..f1fadeb0f2d73 100644 --- a/packages/workspace/src/utilities/ts-config.ts +++ b/packages/workspace/src/utilities/ts-config.ts @@ -1,11 +1,19 @@ import { offsetFromRoot, Tree, workspaceRoot } from '@nx/devkit'; import { existsSync } from 'fs'; import { dirname, join } from 'path'; -import type { Node, SyntaxKind } from 'typescript'; +import type { CompilerOptions, Node, SyntaxKind } from 'typescript'; import { ensureTypescript } from './typescript'; let tsModule: typeof import('typescript'); +const TSCONFIG_FILE_NAMES = ['tsconfig.base.json', 'tsconfig.json'] as const; +export type TSConfigFileName = (typeof TSCONFIG_FILE_NAMES)[number]; + +export type TSConfig = { + compilerOptions: CompilerOptions; + [string: string]: unknown; +}; + export function readTsConfig(tsConfigPath: string) { if (!tsModule) { tsModule = ensureTypescript(); @@ -20,15 +28,14 @@ export function readTsConfig(tsConfigPath: string) { dirname(tsConfigPath) ); } - -export function getRootTsConfigPathInTree(tree: Tree): string | null { - for (const path of ['tsconfig.base.json', 'tsconfig.json']) { +export function getRootTsConfigPathInTree(tree: Tree): TSConfigFileName | null { + for (const path of TSCONFIG_FILE_NAMES) { if (tree.exists(path)) { return path; } } - return 'tsconfig.base.json'; + return null; } export function getRelativePathToRootTsConfig( @@ -38,8 +45,8 @@ export function getRelativePathToRootTsConfig( return offsetFromRoot(targetPath) + getRootTsConfigPathInTree(tree); } -export function getRootTsConfigFileName(): string | null { - for (const tsConfigName of ['tsconfig.base.json', 'tsconfig.json']) { +export function getRootTsConfigFileName(): TSConfigFileName | null { + for (const tsConfigName of TSCONFIG_FILE_NAMES) { const tsConfigPath = join(workspaceRoot, tsConfigName); if (existsSync(tsConfigPath)) { return tsConfigName;