libs/nx/src/generators/library/updaters/update-base-tsconfig-json/update-base-tsconfig-json.ts
Properties |
compilerOptions | |
Type |
literal type
|
import { logger, Tree } from '@nx/devkit';
import { ProjectDetails } from '../../generator';
interface TsconfigJson {
compilerOptions: {
paths: Record<string, string[]>;
};
}
export function updateBaseTsconfigJson(tree: Tree, options: ProjectDetails) {
const possibleTsConfigBaseFileNames = ['tsconfig.base.json', 'tsconfig.json'];
const tsConfigBaseFileName = possibleTsConfigBaseFileNames.find((fileName) =>
tree.exists(fileName)
);
if (!tsConfigBaseFileName) {
logger.warn(
`Could not find a base tsconfig file in the workspace root (tried ${possibleTsConfigBaseFileNames.join(
' and '
)}). Typescript paths for the library will not be added.`
);
return;
}
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const baseTsconfigJsonBuffer: Buffer = tree.read(tsConfigBaseFileName)!;
const baseTsconfigJson = JSON.parse(baseTsconfigJsonBuffer.toString()) as TsconfigJson;
baseTsconfigJson.compilerOptions.paths ??= {};
baseTsconfigJson.compilerOptions.paths[options.projectName] = [
`${options.projectRoot}/src/index.ts`
];
baseTsconfigJson.compilerOptions.paths[`${options.projectName}/*`] = [
`${options.projectRoot}/*/src/index.ts`
];
tree.write(tsConfigBaseFileName, JSON.stringify(baseTsconfigJson, null, 2));
}