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 ,
@@ -40,7 +39,7 @@ const Methods = (() => {
4039
4140 const customThemeExists = async ( customThemePath ) => {
4241 try {
43- return await stat ( customThemePath )
42+ return await atomicFS . stat ( customThemePath )
4443 } catch {
4544 return false
4645 }
@@ -49,10 +48,12 @@ const Methods = (() => {
4948 const refreshCustomTheme = async ( customThemePath ) => {
5049 const backupKeepDir = async ( keepPath ) => {
5150 try {
52- if ( await stat ( keepPath ) ) {
53- const tempPath = await mkdtemp ( join ( tmpdir ( ) , 'writ-theme-keep' ) )
51+ if ( await atomicFS . stat ( keepPath ) ) {
52+ const tempPath = await atomicFS . mkdtemp (
53+ join ( tmpdir ( ) , 'writ-theme-keep' )
54+ )
5455 Debug . debugLog ( 'refresh theme temp dir' , tempPath )
55- await cp ( keepPath , tempPath , { recursive : true } )
56+ await atomicFS . cp ( keepPath , tempPath )
5657 return tempPath
5758 }
5859 } catch {
@@ -62,10 +63,7 @@ const Methods = (() => {
6263 }
6364
6465 const applyKeepOverrides = async ( keepBackupPath , themePath ) => {
65- const entries = await readdir ( keepBackupPath , {
66- recursive : true ,
67- withFileTypes : true
68- } )
66+ const entries = await atomicFS . readdirRecursive ( keepBackupPath )
6967
7068 await Promise . all (
7169 entries
@@ -75,7 +73,7 @@ const Methods = (() => {
7573 entry . parentPath . replace ( keepBackupPath , '' ) ,
7674 entry . name
7775 )
78- return cp (
76+ return atomicFS . cp (
7977 join ( keepBackupPath , relativePath ) ,
8078 join ( themePath , relativePath )
8179 )
@@ -96,10 +94,8 @@ const Methods = (() => {
9694 } )
9795
9896 if ( keepBackupPath ) {
99- await mkdir ( join ( tempPath , KEEP_PATH ) , { recursive : true } )
100- await cp ( keepBackupPath , join ( tempPath , KEEP_PATH ) , {
101- recursive : true
102- } )
97+ await atomicFS . mkdir ( join ( tempPath , KEEP_PATH ) )
98+ await atomicFS . cp ( keepBackupPath , join ( tempPath , KEEP_PATH ) )
10399 await applyKeepOverrides ( keepBackupPath , tempPath )
104100 }
105101 } )
@@ -108,13 +104,13 @@ const Methods = (() => {
108104 throw e
109105 } finally {
110106 if ( keepBackupPath ) {
111- await rm ( keepBackupPath , { recursive : true , force : true } )
107+ await atomicFS . rm ( keepBackupPath )
112108 }
113109 }
114110 }
115111
116112 const collectCustomizerPaths = async ( customThemePath ) => {
117- const paths = await readdir ( customThemePath )
113+ const paths = await atomicFS . readdir ( customThemePath )
118114 const customizerPaths = paths . filter ( p => {
119115 return p . endsWith ( '.css' ) || p . endsWith ( '.js' )
120116 } )
@@ -123,12 +119,11 @@ const Methods = (() => {
123119
124120 const copyCommonResources = ( targetPath ) => {
125121 return Promise . all ( [
126- cp (
122+ atomicFS . cp (
127123 join ( __dirname , 'common' , PARTIALS . from ) ,
128- join ( targetPath , PARTIALS . to ) ,
129- { recursive : true }
124+ join ( targetPath , PARTIALS . to )
130125 ) ,
131- cp (
126+ atomicFS . cp (
132127 join ( __dirname , 'common' , TEMPLATE_HELPERS . from ) ,
133128 join ( targetPath , PARTIALS . to , TEMPLATE_HELPERS . to )
134129 )
@@ -139,17 +134,15 @@ const Methods = (() => {
139134 const { theme } = Settings . getSettings ( )
140135 const themeSrcPath = join ( __dirname , '..' , '..' , 'packages' , `theme-${ theme } ` )
141136 return Promise . all ( [
142- cp (
137+ atomicFS . cp (
143138 join ( themeSrcPath , ASSETS ) ,
144- join ( customThemePath , ASSETS , theme ) ,
145- { recursive : true }
139+ join ( customThemePath , ASSETS , theme )
146140 ) ,
147- cp (
141+ atomicFS . cp (
148142 join ( themeSrcPath , PARTIALS . from ) ,
149- join ( customThemePath , PARTIALS . to ) ,
150- { recursive : true }
143+ join ( customThemePath , PARTIALS . to )
151144 ) ,
152- cp (
145+ atomicFS . cp (
153146 join ( themeSrcPath , THEME_SETTINGS ) ,
154147 join ( customThemePath , THEME_SETTINGS )
155148 )
@@ -161,14 +154,14 @@ const Methods = (() => {
161154 }
162155
163156 const copyCustomizers = async ( customThemePath ) => {
164- const paths = await readdir ( join ( __dirname , 'customizers' ) )
157+ const paths = await atomicFS . readdir ( join ( __dirname , 'customizers' ) )
165158 const customizers = paths . filter ( p => {
166159 return p . endsWith ( '.css' ) || p . endsWith ( '.js' )
167160 } )
168161 State . customizers . push ( ...customizers )
169162 return Promise . all (
170163 paths . map ( path => {
171- return cp (
164+ return atomicFS . cp (
172165 join ( __dirname , 'customizers' , path ) ,
173166 join ( customThemePath , path )
174167 )
@@ -178,12 +171,12 @@ const Methods = (() => {
178171
179172 const makeCustomThemeDirectory = async ( customThemePath , options = { } ) => {
180173 if ( ! options . skipContainer ) {
181- await mkdir ( customThemePath )
174+ await atomicFS . mkdir ( customThemePath )
182175 }
183176
184177 await Promise . all ( [
185- mkdir ( join ( customThemePath , ASSETS ) ) ,
186- mkdir ( join ( customThemePath , PARTIALS . to ) )
178+ atomicFS . mkdir ( join ( customThemePath , ASSETS ) ) ,
179+ atomicFS . mkdir ( join ( customThemePath , PARTIALS . to ) )
187180 ] )
188181
189182 await copyCommonResources ( customThemePath )
0 commit comments