1
1
const { tmpdir } = require ( 'os' )
2
- const { stat, rm, mkdir, mkdtemp, cp, readdir } = require ( 'fs/promises' )
2
+ const { stat, rm, rmdir , mkdir, mkdtemp, cp, readdir } = require ( 'fs/promises' )
3
3
const { join } = require ( 'path' )
4
4
const Debug = require ( '../debug' )
5
5
const Settings = require ( '../settings' )
@@ -29,6 +29,7 @@ const Methods = (() => {
29
29
Debug . debugLog ( 'refresh theme' )
30
30
await refreshCustomTheme ( customThemePath )
31
31
} else {
32
+ await refreshFeaturesDirectory ( customThemePath )
32
33
await collectCustomizerPaths ( customThemePath )
33
34
}
34
35
} else {
@@ -90,11 +91,6 @@ const Methods = (() => {
90
91
91
92
const copyCommonResources = ( targetPath ) => {
92
93
return Promise . all ( [
93
- cp (
94
- join ( __dirname , 'common' , FEATURES ) ,
95
- join ( targetPath , FEATURES ) ,
96
- { recursive : true }
97
- ) ,
98
94
cp (
99
95
join ( __dirname , 'common' , PARTIALS . from ) ,
100
96
join ( targetPath , PARTIALS . to ) ,
@@ -107,6 +103,73 @@ const Methods = (() => {
107
103
] )
108
104
}
109
105
106
+ const refreshFeaturesDirectory = async ( targetPath ) => {
107
+ const settings = Settings . getSettings ( )
108
+
109
+ const features = [
110
+ [ 'syntaxHighlighting' , 'highlight' ] ,
111
+ [ 'search' , 'search' ]
112
+ ]
113
+
114
+ const enabledFeatures = features
115
+ . filter ( ( [ settingsKey , dirName ] ) => {
116
+ return settings [ settingsKey ] !== 'off'
117
+ } )
118
+ . map ( ( [ , dirName ] ) => dirName )
119
+
120
+ const disabledFeatures = features
121
+ . filter ( ( [ settingsKey , dirName ] ) => {
122
+ return settings [ settingsKey ] === 'off'
123
+ } )
124
+ . map ( ( [ , dirName ] ) => dirName )
125
+
126
+ Debug . debugLog ( 'enabledFeatures' , enabledFeatures )
127
+ Debug . debugLog ( 'disabledFeatures' , disabledFeatures )
128
+
129
+ disabledFeatures . forEach ( featureDirName => {
130
+ Debug . debugLog ( 'rm -rf' , join ( targetPath , FEATURES , featureDirName ) )
131
+ } )
132
+
133
+ const copyEnabledFeaturesIfMissing = async ( ) => Promise . all (
134
+ enabledFeatures . map ( featureDirName => {
135
+ const path = join ( targetPath , FEATURES , featureDirName )
136
+ return stat ( path )
137
+ . then ( ( ) => Promise . resolve ( ) )
138
+ . catch ( ( ) => cp (
139
+ join ( __dirname , 'common' , FEATURES , featureDirName ) ,
140
+ join ( targetPath , FEATURES , featureDirName ) ,
141
+ { recursive : true }
142
+ ) )
143
+ } )
144
+ )
145
+
146
+ const deleteDisabledFeaturesIfExisting = async ( ) => Promise . all (
147
+ disabledFeatures . map ( async featureDirName => {
148
+ const path = join ( targetPath , FEATURES , featureDirName )
149
+ return stat ( path )
150
+ . then ( ( ) => rmdir ( path , { recursive : true , force : true } ) )
151
+ . catch ( ( ) => Promise . resolve ( ) )
152
+ } )
153
+ )
154
+
155
+ const deleteEmptyFeaturesDirectory = async ( ) => readdir ( join ( targetPath , FEATURES ) )
156
+ . then ( featuresDirectory => {
157
+ if ( ! featuresDirectory . length ) {
158
+ Debug . debugLog ( 'Deleting empty features directory' )
159
+ return rmdir ( join ( targetPath , FEATURES ) )
160
+ }
161
+ return Promise . resolve ( )
162
+ } )
163
+ . catch ( ( ) => {
164
+ Debug . debugLog ( 'Features directory not found' )
165
+ return Promise . resolve ( )
166
+ } )
167
+
168
+ await copyEnabledFeaturesIfMissing ( )
169
+ await deleteDisabledFeaturesIfExisting ( )
170
+ return deleteEmptyFeaturesDirectory ( )
171
+ }
172
+
110
173
const copyBaseThemeResources = ( customThemePath ) => {
111
174
const { theme } = Settings . getSettings ( )
112
175
const themeSrcPath = join ( __dirname , '..' , '..' , 'packages' , `theme-${ theme } ` )
@@ -157,6 +220,7 @@ const Methods = (() => {
157
220
] )
158
221
159
222
await copyCommonResources ( customThemePath )
223
+ await refreshFeaturesDirectory ( customThemePath )
160
224
await copyBaseThemeResources ( customThemePath )
161
225
await copyCustomizers ( customThemePath )
162
226
}
0 commit comments