1- import { mkdir , readFile , writeFile } from 'node:fs/promises'
1+ import { mkdir , readFile , unlink , writeFile } from 'node:fs/promises'
22import { existsSync , statSync } from 'node:fs'
33import { basename , dirname , resolve } from 'node:path'
44import { execaSync } from 'execa'
@@ -14,6 +14,7 @@ import { createApp } from './create-app.js'
1414import { readConfigFile , writeConfigFile } from './config-file.js'
1515import { formatCommand , sortObject } from './utils.js'
1616import { packageManagerInstall } from './package-manager.js'
17+ import { getBinaryFile , isBase64 , readFileHelper } from './file-helpers.js'
1718
1819import type { Environment , Mode , Options } from './types.js'
1920import type { PersistedOptions } from './config-file.js'
@@ -94,7 +95,7 @@ export async function addToApp(
9495 const relativeFile = file . replace ( process . cwd ( ) , '' )
9596 if ( existsSync ( file ) ) {
9697 if ( ! isDirectory ( file ) ) {
97- const contents = ( await readFile ( file ) ) . toString ( )
98+ const contents = readFileHelper ( file )
9899 if (
99100 [ 'package.json' , CONFIG_FILE ] . includes ( basename ( file ) ) ||
100101 contents !== output . files [ file ]
@@ -109,12 +110,17 @@ export async function addToApp(
109110 }
110111 }
111112
113+ const deletedFiles : Array < string > = [ ]
114+ for ( const file of output . deletedFiles ) {
115+ deletedFiles . push ( file . replace ( process . cwd ( ) , '' ) )
116+ }
117+
112118 environment . finishStep ( 'App setup processed' )
113119
114120 if ( overwrittenFiles . length > 0 && ! silent ) {
115121 environment . warn (
116122 'The following will be overwritten:' ,
117- overwrittenFiles . join ( '\n' ) ,
123+ [ ... overwrittenFiles , ... deletedFiles ] . join ( '\n' ) ,
118124 )
119125 const shouldContinue = await environment . confirm ( 'Do you want to continue?' )
120126 if ( ! shouldContinue ) {
@@ -124,6 +130,12 @@ export async function addToApp(
124130
125131 environment . startStep ( 'Writing files...' )
126132
133+ for ( const file of output . deletedFiles ) {
134+ if ( existsSync ( file ) ) {
135+ await unlink ( file )
136+ }
137+ }
138+
127139 for ( const file of [ ...changedFiles , ...overwrittenFiles ] ) {
128140 const targetFile = `.${ file } `
129141 const fName = basename ( file )
@@ -145,7 +157,11 @@ export async function addToApp(
145157 await writeFile ( targetFile , JSON . stringify ( currentJson , null , 2 ) )
146158 } else if ( fName !== CONFIG_FILE ) {
147159 await mkdir ( resolve ( dirname ( targetFile ) ) , { recursive : true } )
148- await writeFile ( resolve ( targetFile ) , contents )
160+ if ( isBase64 ( contents ) ) {
161+ await writeFile ( resolve ( targetFile ) , getBinaryFile ( contents ) ! )
162+ } else {
163+ await writeFile ( resolve ( targetFile ) , contents )
164+ }
149165 }
150166 }
151167
0 commit comments