@@ -88,6 +88,41 @@ export async function writeFiles(
8888 } ,
8989 forced : boolean ,
9090) {
91+ const toRelativePath = ( filePath : string ) => {
92+ const normalizedFilePath = filePath . replace ( / \\ / g, '/' )
93+ const normalizedCwd = cwd . replace ( / \\ / g, '/' )
94+ const cwdWithoutDrive = normalizedCwd . replace ( / ^ [ a - z A - Z ] : / , '' )
95+ const cwdWithoutDriveNoLeading = cwdWithoutDrive . replace ( / ^ \/ + / , '' )
96+
97+ if ( normalizedFilePath === normalizedCwd ) {
98+ return ''
99+ }
100+ if ( normalizedFilePath . startsWith ( `${ normalizedCwd } /` ) ) {
101+ return normalizedFilePath . slice ( normalizedCwd . length + 1 )
102+ }
103+ if ( normalizedFilePath === cwdWithoutDrive ) {
104+ return ''
105+ }
106+ if ( normalizedFilePath . startsWith ( `${ cwdWithoutDrive } /` ) ) {
107+ return normalizedFilePath . slice ( cwdWithoutDrive . length + 1 )
108+ }
109+ if ( normalizedFilePath === cwdWithoutDriveNoLeading ) {
110+ return ''
111+ }
112+ if ( normalizedFilePath . startsWith ( `${ cwdWithoutDriveNoLeading } /` ) ) {
113+ return normalizedFilePath . slice ( cwdWithoutDriveNoLeading . length + 1 )
114+ }
115+
116+ return normalizedFilePath . replace ( / ^ \/ + / , '' )
117+ }
118+
119+ const relativeOutputFiles = Object . keys ( output . files ) . reduce <
120+ Record < string , string >
121+ > ( ( acc , filePath ) => {
122+ acc [ toRelativePath ( filePath ) ] = output . files [ filePath ]
123+ return acc
124+ } , { } )
125+
91126 const currentFiles = await recursivelyGatherFilesFromEnvironment (
92127 environment ,
93128 cwd ,
@@ -96,10 +131,9 @@ export async function writeFiles(
96131
97132 const overwrittenFiles : Array < string > = [ ]
98133 const changedFiles : Array < string > = [ ]
99- for ( const file of Object . keys ( output . files ) ) {
100- const relativeFile = file . replace ( cwd , '' )
134+ for ( const relativeFile of Object . keys ( relativeOutputFiles ) ) {
101135 if ( currentFiles [ relativeFile ] ) {
102- if ( currentFiles [ relativeFile ] !== output . files [ file ] ) {
136+ if ( currentFiles [ relativeFile ] !== relativeOutputFiles [ relativeFile ] ) {
103137 overwrittenFiles . push ( relativeFile )
104138 }
105139 } else {
@@ -118,9 +152,10 @@ export async function writeFiles(
118152 }
119153 }
120154
121- for ( const file of output . deletedFiles ) {
122- if ( environment . exists ( resolve ( cwd , file ) ) ) {
123- await environment . deleteFile ( resolve ( cwd , file ) )
155+ for ( const filePath of output . deletedFiles ) {
156+ const relativeFilePath = toRelativePath ( filePath )
157+ if ( environment . exists ( resolve ( cwd , relativeFilePath ) ) ) {
158+ await environment . deleteFile ( resolve ( cwd , relativeFilePath ) )
124159 }
125160 }
126161
@@ -132,7 +167,7 @@ export async function writeFiles(
132167
133168 for ( const file of [ ...changedFiles , ...overwrittenFiles ] ) {
134169 const fName = basename ( file )
135- const contents = output . files [ file ]
170+ const contents = relativeOutputFiles [ file ]
136171 if ( fName === 'package.json' ) {
137172 const currentJson = JSON . parse (
138173 await environment . readFile ( resolve ( cwd , file ) ) ,
0 commit comments