@o3r/workspace
Version:
Workspace tooling of the Otter Framework
46 lines • 2.3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.updateTsConfig = updateTsConfig;
const node_path_1 = require("node:path");
const ts = require("typescript");
/**
* Update workspace Tsconfig
* @param targetPath Path where the SDK has been generated
* @param projectName Name of the project
* @param scope scope of the package
*/
function updateTsConfig(targetPath, projectName, scope) {
const tsconfigs = ['tsconfig.base.json', 'tsconfig.build.json', 'tsconfig.json'];
const relativeTargetPath = targetPath.replace(/^\//, '');
return (tree, context) => {
const configs = tsconfigs
.filter((tsconfig) => tree.exists(tsconfig))
.map((tsconfig) => ({
tsconfig,
content: ts.parseConfigFileTextToJson(tsconfig, tree.readText(tsconfig)).config
}))
.filter(({ content }) => !!content);
const configWithPath = configs.find((config) => !!config.content?.compilerOptions?.paths) || configs[0];
if (!configWithPath) {
context.logger.warn('No tsconfig found, the path mapping will not be updated');
return tree;
}
configWithPath.content.compilerOptions ||= {};
configWithPath.content.compilerOptions.baseUrl ||= '.';
configWithPath.content.compilerOptions.paths ||= {};
configWithPath.content.compilerOptions.paths[`${scope ? `@${scope}/` : ''}${projectName}`] = [
...((0, node_path_1.basename)(configWithPath.tsconfig) === 'tsconfig.build.json' ? [`${relativeTargetPath}/dist`] : []),
`${relativeTargetPath}/src/index`
];
configWithPath.content.compilerOptions.paths[`${scope ? `@${scope}/` : ''}${projectName}/fixtures`] = [
`${relativeTargetPath}/src/fixtures/jest`
];
configWithPath.content.compilerOptions.paths[`${scope ? `@${scope}/` : ''}${projectName}/*`] = [
...((0, node_path_1.basename)(configWithPath.tsconfig) === 'tsconfig.build.json' ? [`${relativeTargetPath}/dist/*`] : []),
`${relativeTargetPath}/src/*`
];
tree.overwrite(configWithPath.tsconfig, JSON.stringify(configWithPath.content, null, 2));
return tree;
};
}
//# sourceMappingURL=update-ts-paths.rule.js.map