11import fs from 'node:fs' ;
22import fsP from 'node:fs/promises' ;
33import { platform } from 'node:os' ;
4- import path , { join } from 'node:path' ;
4+ import path , { basename , dirname , join , relative , resolve } from 'node:path' ;
55import { type RsbuildConfig , logger } from '@rsbuild/core' ;
66import MagicString from 'magic-string' ;
77import color from 'picocolors' ;
@@ -23,8 +23,8 @@ export function loadTsconfig(tsconfigPath: string): ts.ParsedCommandLine {
2323export const TEMP_FOLDER = '.rslib' ;
2424export const TEMP_DTS_DIR : string = `${ TEMP_FOLDER } /declarations` ;
2525
26- export function ensureTempDeclarationDir ( cwd : string ) : string {
27- const dirPath = path . join ( cwd , TEMP_DTS_DIR ) ;
26+ export function ensureTempDeclarationDir ( cwd : string , name : string ) : string {
27+ const dirPath = path . join ( cwd , TEMP_DTS_DIR , name ) ;
2828
2929 if ( fs . existsSync ( dirPath ) ) {
3030 return dirPath ;
@@ -38,6 +38,37 @@ export function ensureTempDeclarationDir(cwd: string): string {
3838 return dirPath ;
3939}
4040
41+ export async function pathExists ( path : string ) : Promise < boolean > {
42+ return fs . promises
43+ . access ( path )
44+ . then ( ( ) => true )
45+ . catch ( ( ) => false ) ;
46+ }
47+
48+ export async function emptyDir ( dir : string ) : Promise < void > {
49+ if ( ! ( await pathExists ( dir ) ) ) {
50+ return ;
51+ }
52+
53+ try {
54+ for ( const file of await fs . promises . readdir ( dir ) ) {
55+ await fs . promises . rm ( path . resolve ( dir , file ) , {
56+ recursive : true ,
57+ force : true ,
58+ } ) ;
59+ }
60+ } catch ( err ) {
61+ logger . debug ( `Failed to empty dir: ${ dir } ` ) ;
62+ logger . debug ( err ) ;
63+ }
64+ }
65+
66+ export async function clearTempDeclarationDir ( cwd : string ) : Promise < void > {
67+ const dirPath = path . join ( cwd , TEMP_DTS_DIR ) ;
68+
69+ await emptyDir ( dirPath ) ;
70+ }
71+
4172export function getFileLoc (
4273 diagnostic : ts . Diagnostic ,
4374 configPath : string ,
@@ -196,3 +227,41 @@ export async function calcLongestCommonPath(
196227
197228 return lca ;
198229}
230+
231+ export async function cleanDtsFiles ( dir : string ) : Promise < void > {
232+ const patterns = [ '/**/*.d.ts' , '/**/*.d.cts' , '/**/*.d.mts' ] ;
233+ const files = await Promise . all (
234+ patterns . map ( ( pattern ) =>
235+ glob ( convertPath ( join ( dir , pattern ) ) , { absolute : true } ) ,
236+ ) ,
237+ ) ;
238+
239+ const allFiles = files . flat ( ) ;
240+
241+ await Promise . all ( allFiles . map ( ( file ) => fsP . rm ( file , { force : true } ) ) ) ;
242+ }
243+
244+ export async function cleanTsBuildInfoFile (
245+ tsconfigPath : string ,
246+ tsConfigResult : ts . ParsedCommandLine ,
247+ ) : Promise < void > {
248+ const tsconfigDir = dirname ( tsconfigPath ) ;
249+ const { outDir, rootDir, tsBuildInfoFile } = tsConfigResult . options ;
250+ let tsbuildInfoFilePath = `${ basename (
251+ tsconfigPath ,
252+ '.json' ,
253+ ) } ${ tsBuildInfoFile ?? '.tsbuildinfo' } `;
254+ if ( outDir ) {
255+ if ( rootDir ) {
256+ tsbuildInfoFilePath = join (
257+ outDir ,
258+ relative ( resolve ( tsconfigDir , rootDir ) , tsconfigDir ) ,
259+ tsbuildInfoFilePath ,
260+ ) ;
261+ } else {
262+ tsbuildInfoFilePath = join ( outDir , tsbuildInfoFilePath ) ;
263+ }
264+ }
265+
266+ await fsP . rm ( tsbuildInfoFilePath , { force : true } ) ;
267+ }
0 commit comments