11import fs , { readdirSync , unlinkSync } from "fs" ;
2+ import fsPromise from 'fs/promises'
23import path from "path" ;
34import {
45 PluginConfigType ,
@@ -107,16 +108,21 @@ export function writeFile(
107108 content : string ,
108109 filePath ?: string ,
109110) {
111+ return new Promise < string > ( ( resolve , reject ) => {
110112 const writeFilePath = filePath
111113 ? path . join ( filePath , fileName )
112114 : path . join ( process . cwd ( ) , fileName ) ;
113115 try {
114116 filePath && fs . mkdirSync ( filePath , { recursive : true } ) ;
115117
116- fs . writeFileSync ( writeFilePath , content , "utf8" ) ;
118+ fsPromise . writeFile ( writeFilePath , content , { encoding : "utf8" } ) . then ( ( ) => {
119+ resolve ( 'done' )
120+ } ) . catch ( err => reject ( err ) )
117121 } catch ( error ) {
118122 console . log ( error ) ;
123+ reject ( error )
119124 }
125+ } )
120126}
121127
122128/**
@@ -182,6 +188,7 @@ export async function writeFileFromConfig(baseConfig: PluginConfigType) {
182188 : { component : "jsx" , native : "js" } ;
183189
184190 //writing file from base config
191+ const writeFilePromises : Promise < string > [ ] = [ ]
185192 baseConfig . files . forEach ( ( fileDetail ) => {
186193 const content =
187194 typeof fileDetail . content === "function"
@@ -193,9 +200,11 @@ export async function writeFileFromConfig(baseConfig: PluginConfigType) {
193200 ? `${ fileDetail . fileName } .${ fileType [ fileDetail . fileType ] } `
194201 : fileDetail . fileName ;
195202
196- writeFile ( fileName , content , path . join ( process . cwd ( ) , ...fileDetail . path ) ) ;
203+ writeFilePromises . push ( writeFile ( fileName , content , path . join ( process . cwd ( ) , ...fileDetail . path ) ) ) ;
197204 } ) ;
198205
206+ await Promise . allSettled ( writeFilePromises )
207+
199208 // If there are file modifications defined in the configuration, set them in the global state
200209 if ( baseConfig ?. fileModification ) {
201210 globalInstance . setPluginAppEntryConfig ( baseConfig . fileModification ) ;
@@ -416,12 +425,13 @@ async function pluginEntryAdderInReact(pluginConfigArr: ReactPluginEntry[]) {
416425 const homePagePath = path . join ( process . cwd ( ) , "src" , "components" , "home" ) ;
417426
418427 // Write the home page content and its CSS
428+ await Promise . allSettled ( [
419429 writeFile (
420430 `Home.${ isTsProject ? "tsx" : "jsx" } ` ,
421431 HomePageContent ,
422- homePagePath ,
423- ) ;
424- writeFile ( "Home.module.css" , homePageCss , homePagePath ) ;
432+ homePagePath , ) ,
433+ writeFile ( "Home.module.css" , homePageCss , homePagePath )
434+ ] )
425435}
426436
427437/**
@@ -472,10 +482,11 @@ async function pluginEntryAdderInNext(pluginConfigArr: NextPluginEntry[]) {
472482 const isTsProject = isFileExists ( process . cwd ( ) , "tsconfig.json" ) ;
473483 const homeContent = nextHomePageContent ( isTsProject , importAndComponentValues . example ) ;
474484 const homePath = path . join ( process . cwd ( ) , "src" , "app" ) ;
475- writeFile ( `page.${ isTsProject ? "tsx" : "js" } ` , homeContent , homePath ) ;
476- writeFile ( "page.module.css" , nextHomeCssContent ( ) , homePath )
477- writeFile ( "globals.css" , "" , homePath )
478-
485+ await Promise . allSettled ( [
486+ writeFile ( `page.${ isTsProject ? "tsx" : "js" } ` , homeContent , homePath ) ,
487+ writeFile ( "page.module.css" , nextHomeCssContent ( ) , homePath ) ,
488+ writeFile ( "globals.css" , "" , homePath ) ,
489+ ] )
479490}
480491
481492/**
0 commit comments