11const { tmpdir } = require ( 'os' )
2- const { stat, rm, mkdir, mkdtemp, cp, readdir } = require ( 'fs/promises' )
32const { join } = require ( 'path' )
43const Debug = require ( '../debug' )
54const Settings = require ( '../settings' )
65const createDecorator = require ( './decorator' )
7- const { atomicReplace } = require ( '../lib/fileSystemHelpers' )
6+ const { atomicReplace, atomicFS } = require ( '../lib/fileSystemHelpers' )
87const {
98 ASSETS ,
109 PARTIALS ,
@@ -42,7 +41,7 @@ const Methods = (() => {
4241
4342 const customThemeExists = async ( customThemePath ) => {
4443 try {
45- return await stat ( customThemePath )
44+ return await atomicFS . stat ( customThemePath )
4645 } catch {
4746 return false
4847 }
@@ -51,10 +50,12 @@ const Methods = (() => {
5150 const refreshCustomTheme = async ( customThemePath ) => {
5251 const backupKeepDir = async ( keepPath ) => {
5352 try {
54- if ( await stat ( keepPath ) ) {
55- const tempPath = await mkdtemp ( join ( tmpdir ( ) , 'writ-theme-keep' ) )
53+ if ( await atomicFS . stat ( keepPath ) ) {
54+ const tempPath = await atomicFS . mkdtemp (
55+ join ( tmpdir ( ) , 'writ-theme-keep' )
56+ )
5657 Debug . debugLog ( 'refresh theme temp dir' , tempPath )
57- await cp ( keepPath , tempPath , { recursive : true } )
58+ await atomicFS . cp ( keepPath , tempPath )
5859 return tempPath
5960 }
6061 } catch {
@@ -64,10 +65,7 @@ const Methods = (() => {
6465 }
6566
6667 const applyKeepOverrides = async ( keepBackupPath , themePath ) => {
67- const entries = await readdir ( keepBackupPath , {
68- recursive : true ,
69- withFileTypes : true
70- } )
68+ const entries = await atomicFS . readdirRecursive ( keepBackupPath )
7169
7270 await Promise . all (
7371 entries
@@ -77,7 +75,7 @@ const Methods = (() => {
7775 entry . parentPath . replace ( keepBackupPath , '' ) ,
7876 entry . name
7977 )
80- return cp (
78+ return atomicFS . cp (
8179 join ( keepBackupPath , relativePath ) ,
8280 join ( themePath , relativePath )
8381 )
@@ -98,10 +96,8 @@ const Methods = (() => {
9896 } )
9997
10098 if ( keepBackupPath ) {
101- await mkdir ( join ( tempPath , KEEP_PATH ) , { recursive : true } )
102- await cp ( keepBackupPath , join ( tempPath , KEEP_PATH ) , {
103- recursive : true
104- } )
99+ await atomicFS . mkdir ( join ( tempPath , KEEP_PATH ) )
100+ await atomicFS . cp ( keepBackupPath , join ( tempPath , KEEP_PATH ) )
105101 await applyKeepOverrides ( keepBackupPath , tempPath )
106102 }
107103 } )
@@ -110,13 +106,13 @@ const Methods = (() => {
110106 throw e
111107 } finally {
112108 if ( keepBackupPath ) {
113- await rm ( keepBackupPath , { recursive : true , force : true } )
109+ await atomicFS . rm ( keepBackupPath )
114110 }
115111 }
116112 }
117113
118114 const collectCustomizerPaths = async ( customThemePath ) => {
119- const paths = await readdir ( customThemePath )
115+ const paths = await atomicFS . readdir ( customThemePath )
120116 const customizerPaths = paths . filter ( p => {
121117 return p . endsWith ( '.css' ) || p . endsWith ( '.js' )
122118 } )
@@ -125,12 +121,11 @@ const Methods = (() => {
125121
126122 const copyCommonResources = ( targetPath ) => {
127123 return Promise . all ( [
128- cp (
124+ atomicFS . cp (
129125 join ( __dirname , 'common' , PARTIALS . from ) ,
130- join ( targetPath , PARTIALS . to ) ,
131- { recursive : true }
126+ join ( targetPath , PARTIALS . to )
132127 ) ,
133- cp (
128+ atomicFS . cp (
134129 join ( __dirname , 'common' , TEMPLATE_HELPERS . from ) ,
135130 join ( targetPath , PARTIALS . to , TEMPLATE_HELPERS . to )
136131 )
@@ -141,17 +136,15 @@ const Methods = (() => {
141136 const { theme } = Settings . getSettings ( )
142137 const themeSrcPath = join ( __dirname , '..' , '..' , 'packages' , `theme-${ theme } ` )
143138 return Promise . all ( [
144- cp (
139+ atomicFS . cp (
145140 join ( themeSrcPath , ASSETS ) ,
146- join ( customThemePath , ASSETS , theme ) ,
147- { recursive : true }
141+ join ( customThemePath , ASSETS , theme )
148142 ) ,
149- cp (
143+ atomicFS . cp (
150144 join ( themeSrcPath , PARTIALS . from ) ,
151- join ( customThemePath , PARTIALS . to ) ,
152- { recursive : true }
145+ join ( customThemePath , PARTIALS . to )
153146 ) ,
154- cp (
147+ atomicFS . cp (
155148 join ( themeSrcPath , THEME_SETTINGS ) ,
156149 join ( customThemePath , THEME_SETTINGS )
157150 )
@@ -163,14 +156,14 @@ const Methods = (() => {
163156 }
164157
165158 const copyCustomizers = async ( customThemePath ) => {
166- const paths = await readdir ( join ( __dirname , 'customizers' ) )
159+ const paths = await atomicFS . readdir ( join ( __dirname , 'customizers' ) )
167160 const customizers = paths . filter ( p => {
168161 return p . endsWith ( '.css' ) || p . endsWith ( '.js' )
169162 } )
170163 State . customizers . push ( ...customizers )
171164 return Promise . all (
172165 paths . map ( path => {
173- return cp (
166+ return atomicFS . cp (
174167 join ( __dirname , 'customizers' , path ) ,
175168 join ( customThemePath , path )
176169 )
@@ -180,12 +173,12 @@ const Methods = (() => {
180173
181174 const makeCustomThemeDirectory = async ( customThemePath , options = { } ) => {
182175 if ( ! options . skipContainer ) {
183- await mkdir ( customThemePath )
176+ await atomicFS . mkdir ( customThemePath )
184177 }
185178
186179 await Promise . all ( [
187- mkdir ( join ( customThemePath , ASSETS ) ) ,
188- mkdir ( join ( customThemePath , PARTIALS . to ) )
180+ atomicFS . mkdir ( join ( customThemePath , ASSETS ) ) ,
181+ atomicFS . mkdir ( join ( customThemePath , PARTIALS . to ) )
189182 ] )
190183
191184 await copyCommonResources ( customThemePath )
0 commit comments