add script to generate tsconfig from package.json

This commit is contained in:
Mads Rasmussen
2024-02-09 22:18:36 +01:00
committed by Jacob Overgaard
parent 14f43cd5d3
commit 8ea1de0f26
3 changed files with 223 additions and 153 deletions

View File

@@ -0,0 +1,67 @@
import { readFileSync, writeFileSync } from 'fs';
import { format } from 'prettier';
const packageJsonPath = 'package.json';
const packageJsonData = JSON.parse(readFileSync(packageJsonPath).toString());
const packageJsonName = packageJsonData.name;
const packageJsonExports = packageJsonData.exports;
const tsconfigPath = 'tsconfig.json';
const tsconfigComment = `// Don't edit this file directly. It is generated by /scripts/generate-ts-config.js\n\n`;
const tsConfigBase = {
compilerOptions: {
module: 'esnext',
target: 'ES2020',
lib: ['es2020', 'dom', 'dom.iterable'],
outDir: './types',
allowSyntheticDefaultImports: true,
experimentalDecorators: true,
forceConsistentCasingInFileNames: true,
useDefineForClassFields: false,
baseUrl: '.',
incremental: true,
skipLibCheck: true,
/* Bundler mode */
moduleResolution: 'bundler',
allowImportingTsExtensions: true,
resolveJsonModule: true,
isolatedModules: true,
noEmit: true,
/* Linting */
strict: true,
noFallthroughCasesInSwitch: true,
noImplicitReturns: true,
},
include: ['src/**/*.ts', 'apps/**/*.ts', 'e2e/**/*.ts', 'index.ts', 'storybook/stories/**/*.ts', 'examples/**/*.ts'],
references: [
{
path: './tsconfig.node.json',
},
],
};
const paths = {};
// Iterate over the exports in package.json
for (const [key, value] of Object.entries(packageJsonExports || {})) {
// remove leading ./
if (value) {
const moduleName = key.replace(/^\.\//, '');
// replace ./dist-cms with src and remove /index.js
const modulePath = value.replace(/^\.\/dist-cms/, 'src').replace(/\/index.js$/, '');
const importAlias = `${packageJsonName}/${moduleName}`;
paths[importAlias] = [modulePath];
}
}
tsConfigBase.compilerOptions.paths = paths;
const content = tsconfigComment + JSON.stringify(tsConfigBase, null, ' ');
const formattedContent = await format(content, {
parser: 'json',
});
writeFileSync(tsconfigPath, formattedContent);