@@ -5,6 +5,9 @@ let fs = require('fs/promises')
55let chokidar = require ( 'chokidar' )
66
77let resolveToolRoot = require ( './resolve-tool-root' )
8+ let FILE_STATE = {
9+ NotFound : Symbol ( ) ,
10+ }
811
912function getWatcherOptions ( ) {
1013 return {
@@ -28,7 +31,7 @@ module.exports = function ({
2831 cleanup = true ,
2932} = { } ) {
3033 let toolRoot = resolveToolRoot ( )
31- let fileCache = { }
34+ let fileCache = new Map ( )
3235
3336 let absoluteOutputFolder = path . resolve ( toolRoot , output )
3437 let absoluteInputFolder = path . resolve ( toolRoot , input )
@@ -41,9 +44,9 @@ module.exports = function ({
4144 // Restore all written files
4245 afterEach ( async ( ) => {
4346 await Promise . all (
44- Object . entries ( fileCache ) . map ( async ( [ file , content ] ) => {
47+ Array . from ( fileCache . entries ( ) ) . map ( async ( [ file , content ] ) => {
4548 try {
46- if ( content === null ) {
49+ if ( content === FILE_STATE . NotFound ) {
4750 return await fs . unlink ( file )
4851 } else {
4952 return await fs . writeFile ( file , content , 'utf8' )
@@ -89,7 +92,7 @@ module.exports = function ({
8992 return {
9093 cleanupFile ( file ) {
9194 let filePath = path . resolve ( toolRoot , file )
92- fileCache [ filePath ] = null
95+ fileCache . set ( filePath , FILE_STATE . NotFound )
9396 } ,
9497 async fileExists ( file ) {
9598 let filePath = path . resolve ( toolRoot , file )
@@ -98,8 +101,11 @@ module.exports = function ({
98101 async removeFile ( file ) {
99102 let filePath = path . resolve ( toolRoot , file )
100103
101- if ( ! fileCache [ filePath ] ) {
102- fileCache [ filePath ] = await fs . readFile ( filePath , 'utf8' ) . catch ( ( ) => null )
104+ if ( ! fileCache . has ( filePath ) ) {
105+ fileCache . set (
106+ filePath ,
107+ await fs . readFile ( filePath , 'utf8' ) . catch ( ( ) => FILE_STATE . NotFound )
108+ )
103109 }
104110
105111 await fs . unlink ( filePath ) . catch ( ( ) => null )
@@ -114,8 +120,8 @@ module.exports = function ({
114120 } ,
115121 async appendToInputFile ( file , contents ) {
116122 let filePath = path . resolve ( absoluteInputFolder , file )
117- if ( ! fileCache [ filePath ] ) {
118- fileCache [ filePath ] = await fs . readFile ( filePath , 'utf8' )
123+ if ( ! fileCache . has ( filePath ) ) {
124+ fileCache . set ( filePath , await fs . readFile ( filePath , 'utf8' ) )
119125 }
120126
121127 return fs . appendFile ( filePath , contents , 'utf8' )
@@ -133,12 +139,12 @@ module.exports = function ({
133139 await fs . mkdir ( path . dirname ( filePath ) , { recursive : true } )
134140 }
135141
136- if ( ! fileCache [ filePath ] ) {
142+ if ( ! fileCache . has ( filePath ) ) {
137143 try {
138- fileCache [ filePath ] = await fs . readFile ( filePath , 'utf8' )
144+ fileCache . set ( filePath , await fs . readFile ( filePath , 'utf8' ) )
139145 } catch ( err ) {
140146 if ( err . code === 'ENOENT' ) {
141- fileCache [ filePath ] = null // Sentinel value to `delete` the file afterwards. This also means that we are writing to a `new` file inside the test.
147+ fileCache . set ( filePath , FILE_STATE . NotFound )
142148 } else {
143149 throw err
144150 }
0 commit comments