@@ -21,7 +21,10 @@ import {
21
21
} from '@angular-devkit/architect' ;
22
22
import { WorkspaceHost } from '@angular-devkit/architect/node' ;
23
23
import { TestProjectHost } from '@angular-devkit/architect/testing' ;
24
- import { getSystemPath , join , json , logging , normalize } from '@angular-devkit/core' ;
24
+ import { getSystemPath , json , logging } from '@angular-devkit/core' ;
25
+ import { existsSync , readFileSync , readdirSync } from 'node:fs' ;
26
+ import fs from 'node:fs/promises' ;
27
+ import { dirname , join } from 'node:path' ;
25
28
import { Observable , Subject , from as observableFrom , of as observableOf } from 'rxjs' ;
26
29
import { catchError , finalize , first , map , mergeMap , shareReplay } from 'rxjs/operators' ;
27
30
import { BuilderWatcherFactory , WatcherNotifier } from './file-watching' ;
@@ -85,6 +88,10 @@ export class BuilderHarness<T> {
85
88
this . schemaRegistry . addPostTransform ( json . schema . transforms . addUndefinedDefaults ) ;
86
89
}
87
90
91
+ private resolvePath ( path : string ) : string {
92
+ return join ( getSystemPath ( this . host . root ( ) ) , path ) ;
93
+ }
94
+
88
95
useProject ( name : string , metadata : Record < string , unknown > = { } ) : this {
89
96
if ( ! name ) {
90
97
throw new Error ( 'Project name cannot be an empty string.' ) ;
@@ -215,7 +222,7 @@ export class BuilderHarness<T> {
215
222
} ;
216
223
const context = new HarnessBuilderContext (
217
224
this . builderInfo ,
218
- getSystemPath ( this . host . root ( ) ) ,
225
+ this . resolvePath ( '.' ) ,
219
226
contextHost ,
220
227
useNativeFileWatching ? undefined : this . watcherNotifier ,
221
228
) ;
@@ -282,13 +289,12 @@ export class BuilderHarness<T> {
282
289
}
283
290
284
291
async writeFile ( path : string , content : string | Buffer ) : Promise < void > {
285
- this . host
286
- . scopedSync ( )
287
- . write ( normalize ( path ) , typeof content === 'string' ? Buffer . from ( content ) : content ) ;
292
+ const fullPath = this . resolvePath ( path ) ;
288
293
289
- this . watcherNotifier ?. notify ( [
290
- { path : getSystemPath ( join ( this . host . root ( ) , path ) ) , type : 'modified' } ,
291
- ] ) ;
294
+ await fs . mkdir ( dirname ( fullPath ) , { recursive : true } ) ;
295
+ await fs . writeFile ( fullPath , content , 'utf-8' ) ;
296
+
297
+ this . watcherNotifier ?. notify ( [ { path : fullPath , type : 'modified' } ] ) ;
292
298
}
293
299
294
300
async writeFiles ( files : Record < string , string | Buffer > ) : Promise < void > {
@@ -297,11 +303,12 @@ export class BuilderHarness<T> {
297
303
: undefined ;
298
304
299
305
for ( const [ path , content ] of Object . entries ( files ) ) {
300
- this . host
301
- . scopedSync ( )
302
- . write ( normalize ( path ) , typeof content === 'string' ? Buffer . from ( content ) : content ) ;
306
+ const fullPath = this . resolvePath ( path ) ;
307
+
308
+ await fs . mkdir ( dirname ( fullPath ) , { recursive : true } ) ;
309
+ await fs . writeFile ( fullPath , content , 'utf-8' ) ;
303
310
304
- watchEvents ?. push ( { path : getSystemPath ( join ( this . host . root ( ) , path ) ) , type : 'modified' } ) ;
311
+ watchEvents ?. push ( { path : fullPath , type : 'modified' } ) ;
305
312
}
306
313
307
314
if ( watchEvents ) {
@@ -310,11 +317,11 @@ export class BuilderHarness<T> {
310
317
}
311
318
312
319
async removeFile ( path : string ) : Promise < void > {
313
- this . host . scopedSync ( ) . delete ( normalize ( path ) ) ;
320
+ const fullPath = this . resolvePath ( path ) ;
314
321
315
- this . watcherNotifier ?. notify ( [
316
- { path : getSystemPath ( join ( this . host . root ( ) , path ) ) , type : 'deleted' } ,
317
- ] ) ;
322
+ await fs . unlink ( fullPath ) ;
323
+
324
+ this . watcherNotifier ?. notify ( [ { path : fullPath , type : 'deleted' } ] ) ;
318
325
}
319
326
320
327
async modifyFile (
@@ -323,27 +330,24 @@ export class BuilderHarness<T> {
323
330
) : Promise < void > {
324
331
const content = this . readFile ( path ) ;
325
332
await this . writeFile ( path , await modifier ( content ) ) ;
326
-
327
- this . watcherNotifier ?. notify ( [
328
- { path : getSystemPath ( join ( this . host . root ( ) , path ) ) , type : 'modified' } ,
329
- ] ) ;
330
333
}
331
334
332
335
hasFile ( path : string ) : boolean {
333
- return this . host . scopedSync ( ) . exists ( normalize ( path ) ) ;
336
+ const fullPath = this . resolvePath ( path ) ;
337
+
338
+ return existsSync ( fullPath ) ;
334
339
}
335
340
336
341
hasFileMatch ( directory : string , pattern : RegExp ) : boolean {
337
- return this . host
338
- . scopedSync ( )
339
- . list ( normalize ( directory ) )
340
- . some ( ( name ) => pattern . test ( name ) ) ;
342
+ const fullPath = this . resolvePath ( directory ) ;
343
+
344
+ return readdirSync ( fullPath ) . some ( ( name ) => pattern . test ( name ) ) ;
341
345
}
342
346
343
347
readFile ( path : string ) : string {
344
- const content = this . host . scopedSync ( ) . read ( normalize ( path ) ) ;
348
+ const fullPath = this . resolvePath ( path ) ;
345
349
346
- return Buffer . from ( content ) . toString ( 'utf8 ') ;
350
+ return readFileSync ( fullPath , 'utf-8 ') ;
347
351
}
348
352
349
353
private validateProjectName ( name : string ) : void {
0 commit comments