Skip to content

Commit

Permalink
fix(core): allow for non-js libs to be moved without errors or the cr…
Browse files Browse the repository at this point in the history
…eation of any unneeded tsconfig files

closed #28349
  • Loading branch information
iweg authored and KerickHowlett committed Oct 10, 2024
1 parent c3b77e7 commit a10cf17
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 30 deletions.
47 changes: 24 additions & 23 deletions packages/workspace/src/generators/move/lib/update-imports.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {
ChangeType,
ProjectConfiguration,
StringChange,
Tree,
applyChangesToString,
ChangeType,
getProjects,
getWorkspaceLayout,
joinPathFragments,
ProjectConfiguration,
readJson,
StringChange,
Tree,
visitNotIgnoredFiles,
writeJson,
} from '@nx/devkit';
Expand All @@ -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';
Expand All @@ -43,38 +45,37 @@ 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))
)
);

// 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)) &&
Expand Down Expand Up @@ -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(
[
Expand All @@ -170,9 +171,9 @@ export function updateImports(
} else {
tsConfig.compilerOptions.paths[projectRef.from] = updatedPath;
}
}

writeJson(tree, tsConfigPath, tsConfig);
writeJson(tree, tsConfigPath, tsConfig);
}
}
}

Expand Down
21 changes: 14 additions & 7 deletions packages/workspace/src/utilities/ts-config.ts
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -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(
Expand All @@ -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;
Expand Down

0 comments on commit a10cf17

Please sign in to comment.