@@ -3,8 +3,8 @@ import type {LoggerDiagnosticInput} from '../../src/plugins/plugin-core'
33import * as fs from 'node:fs'
44import path from 'node:path'
55import process from 'node:process'
6- import { buildFileOperationDiagnostic } from '@ /diagnostics'
7- import { resolveRuntimeEnvironment , resolveUserPath } from '@ /runtime-environment'
6+ import { buildFileOperationDiagnostic } from '../../src /diagnostics'
7+ import { resolveRuntimeEnvironment , resolveUserPath } from '../../src /runtime-environment'
88
99type PlatformFixedDir = 'win32' | 'darwin' | 'linux'
1010
@@ -41,8 +41,7 @@ export function deletePathSync(p: string): void {
4141 if ( stat . isSymbolicLink ( ) ) {
4242 if ( process . platform === 'win32' ) fs . rmSync ( p , { recursive : true , force : true } )
4343 else fs . unlinkSync ( p )
44- }
45- else if ( stat . isDirectory ( ) ) fs . rmSync ( p , { recursive : true , force : true } )
44+ } else if ( stat . isDirectory ( ) ) fs . rmSync ( p , { recursive : true , force : true } )
4645 else fs . unlinkSync ( p )
4746}
4847
@@ -56,8 +55,7 @@ export function writeFileSync(filePath: string, data: string | Buffer, encoding:
5655export function readFileSync ( filePath : string , encoding : BufferEncoding = 'utf8' ) : string {
5756 try {
5857 return fs . readFileSync ( filePath , encoding )
59- }
60- catch ( error ) {
58+ } catch ( error ) {
6159 const msg = error instanceof Error ? error . message : String ( error )
6260 throw new Error ( `Failed to read file "${ filePath } ": ${ msg } ` )
6361 }
@@ -98,8 +96,7 @@ async function deletePath(p: string): Promise<boolean> {
9896
9997 await fs . promises . unlink ( p )
10098 return true
101- }
102- catch ( error ) {
99+ } catch ( error ) {
103100 if ( ( error as NodeJS . ErrnoException ) . code === 'ENOENT' ) return false
104101 throw error
105102 }
@@ -109,19 +106,14 @@ async function deleteEmptyDirectory(p: string): Promise<boolean> {
109106 try {
110107 await fs . promises . rmdir ( p )
111108 return true
112- }
113- catch ( error ) {
109+ } catch ( error ) {
114110 const { code} = error as NodeJS . ErrnoException
115111 if ( code === 'ENOENT' || code === 'ENOTEMPTY' ) return false
116112 throw error
117113 }
118114}
119115
120- async function mapWithConcurrencyLimit < T , TResult > (
121- items : readonly T [ ] ,
122- concurrency : number ,
123- worker : ( item : T ) => Promise < TResult >
124- ) : Promise < TResult [ ] > {
116+ async function mapWithConcurrencyLimit < T , TResult > ( items : readonly T [ ] , concurrency : number , worker : ( item : T ) => Promise < TResult > ) : Promise < TResult [ ] > {
125117 if ( items . length === 0 ) return [ ]
126118
127119 const results : TResult [ ] = [ ]
@@ -147,20 +139,14 @@ async function mapWithConcurrencyLimit<T, TResult>(
147139 return results
148140}
149141
150- async function deletePaths (
151- paths : readonly string [ ] ,
152- options ?: { readonly sortByDepthDescending ?: boolean }
153- ) : Promise < DeletionResult > {
154- const sortedPaths = options ?. sortByDepthDescending === true
155- ? [ ...paths ] . sort ( ( a , b ) => b . length - a . length || b . localeCompare ( a ) )
156- : [ ...paths ]
142+ async function deletePaths ( paths : readonly string [ ] , options ?: { readonly sortByDepthDescending ?: boolean } ) : Promise < DeletionResult > {
143+ const sortedPaths = options ?. sortByDepthDescending === true ? [ ...paths ] . sort ( ( a , b ) => b . length - a . length || b . localeCompare ( a ) ) : [ ...paths ]
157144
158145 const results = await mapWithConcurrencyLimit ( sortedPaths , DELETE_CONCURRENCY , async currentPath => {
159146 try {
160147 const deleted = await deletePath ( currentPath )
161148 return { path : currentPath , deleted}
162- }
163- catch ( error ) {
149+ } catch ( error ) {
164150 return { path : currentPath , error}
165151 }
166152 } )
@@ -201,8 +187,7 @@ export async function deleteEmptyDirectories(dirs: readonly string[]): Promise<D
201187 try {
202188 const deleted = await deleteEmptyDirectory ( currentPath )
203189 if ( deleted ) deletedPaths . push ( currentPath )
204- }
205- catch ( error ) {
190+ } catch ( error ) {
206191 errors . push ( { path : currentPath , error} )
207192 }
208193 }
@@ -214,14 +199,8 @@ export async function deleteEmptyDirectories(dirs: readonly string[]): Promise<D
214199 }
215200}
216201
217- export async function deleteTargets ( targets : {
218- readonly files ?: readonly string [ ]
219- readonly dirs ?: readonly string [ ]
220- } ) : Promise < DeleteTargetsResult > {
221- const [ fileResult , dirResult ] = await Promise . all ( [
222- deleteFiles ( targets . files ?? [ ] ) ,
223- deleteDirectories ( targets . dirs ?? [ ] )
224- ] )
202+ export async function deleteTargets ( targets : { readonly files ?: readonly string [ ] , readonly dirs ?: readonly string [ ] } ) : Promise < DeleteTargetsResult > {
203+ const [ fileResult , dirResult ] = await Promise . all ( [ deleteFiles ( targets . files ?? [ ] ) , deleteDirectories ( targets . dirs ?? [ ] ) ] )
225204
226205 return {
227206 deletedFiles : fileResult . deletedPaths ,
@@ -264,21 +243,22 @@ export function writeFileSafe(options: SafeWriteOptions): SafeWriteResult {
264243 writeFileSync ( fullPath , content )
265244 logger . trace ( { action : 'write' , type, path : fullPath } )
266245 return { path : relativePath , success : true }
267- }
268- catch ( error ) {
246+ } catch ( error ) {
269247 const errMsg = error instanceof Error ? error . message : String ( error )
270- logger . error ( buildFileOperationDiagnostic ( {
271- code : 'OUTPUT_FILE_WRITE_FAILED' ,
272- title : `Failed to write ${ type } output` ,
273- operation : 'write' ,
274- targetKind : `${ type } output file` ,
275- path : fullPath ,
276- error : errMsg ,
277- details : {
278- relativePath,
279- type
280- }
281- } ) )
248+ logger . error (
249+ buildFileOperationDiagnostic ( {
250+ code : 'OUTPUT_FILE_WRITE_FAILED' ,
251+ title : `Failed to write ${ type } output` ,
252+ operation : 'write' ,
253+ targetKind : `${ type } output file` ,
254+ path : fullPath ,
255+ error : errMsg ,
256+ details : {
257+ relativePath,
258+ type
259+ }
260+ } )
261+ )
282262 return { path : relativePath , success : false , error : error as Error }
283263 }
284264}
0 commit comments