11// Run typescript init (with a version specified) to generate a tsconfig.json that will have all defaults listed.
22// store this json per ts version in src/default-configs.ts
33// get a list of TS version, maybe from npm and somehow filter only versions
4- import { writeFile } from 'node:fs/promises' ;
4+ import { rm , writeFile } from 'node:fs/promises' ;
55// eslint-disable-next-line unicorn/import-style
6- import { join } from 'node:path' ;
6+ import { dirname , join } from 'node:path' ;
77import type { CompilerOptions } from 'typescript' ;
88import {
99 ensureDirectoryExists ,
1010 executeProcess ,
1111 readTextFile ,
1212} from '@code-pushup/utils' ;
13- import { TS_CONFIG_DIR } from '../lib/constants.js' ;
1413import type { SemVerString } from '../lib/runner/types.js' ;
1514import { getCurrentTsVersion } from '../lib/runner/utils.js' ;
1615
17- export const TMP_TS_CONFIG_DIR = join ( 'tmp' , 'plugin-typescript-ts-config' ) ;
16+ const normalizedTsConfigFolder = join (
17+ '..' ,
18+ '..' ,
19+ '.code-pushup' ,
20+ 'typescript-plugin' ,
21+ 'default-ts-configs' ,
22+ ) ;
23+ const tmpDir = join ( normalizedTsConfigFolder , 'tmp' ) ;
1824
1925export async function generateDefaultTsConfig ( version : SemVerString ) {
20- await ensureDirectoryExists ( TS_CONFIG_DIR ) ;
26+ await ensureDirectoryExists ( normalizedTsConfigFolder ) ;
2127 await generateRawTsConfigFile ( version ) ;
2228 const config = await extractTsConfig ( version ) ;
29+ await cleanupArtefacts ( ) ;
2330 await cleanupNpmCache ( version ) ;
24- return writeFile (
25- join ( TS_CONFIG_DIR , `${ version } .ts ` ) ,
31+ await writeFile (
32+ join ( normalizedTsConfigFolder , `${ version } .js ` ) ,
2633 [
2734 `const config = ${ JSON . stringify ( config , null , 2 ) } ` ,
2835 `export default config;` ,
2936 ] . join ( '\n' ) ,
3037 ) ;
38+ console . log (
39+ `Generated default TS config for version ${ version } under ${ normalizedTsConfigFolder } ` ,
40+ ) ;
3141}
3242
3343export async function generateRawTsConfigFile ( version : SemVerString ) {
34- const dir = join ( TMP_TS_CONFIG_DIR , version ) ;
44+ const dir = join ( tmpDir , version ) ;
3545 await ensureDirectoryExists ( dir ) ;
3646 await executeProcess ( {
3747 command : 'npx' ,
@@ -47,7 +57,7 @@ export async function generateRawTsConfigFile(version: SemVerString) {
4757export async function extractTsConfig (
4858 version : SemVerString ,
4959) : Promise < CompilerOptions > {
50- const dir = join ( TMP_TS_CONFIG_DIR , version ) ;
60+ const dir = join ( tmpDir , version ) ;
5161 await ensureDirectoryExists ( dir ) ;
5262 try {
5363 return parseTsConfigJson ( await readTextFile ( join ( dir , 'tsconfig.json' ) ) ) ;
@@ -69,6 +79,14 @@ export async function cleanupNpmCache(version: SemVerString) {
6979 } ) ;
7080}
7181
82+ /**
83+ * Cleanup artefacts`
84+ * @param version
85+ */
86+ export async function cleanupArtefacts ( ) {
87+ await rm ( tmpDir , { recursive : true } ) ;
88+ }
89+
7290/**
7391 * Parse the tsconfig.json file content into a CompilerOptions object.
7492 * tsconfig.json files can have comments and trailing commas, which are not valid JSON.
0 commit comments