Skip to content

Commit 15b59c1

Browse files
committed
Don't copy disabled feature assets into theme
And refresh theme/features, while preserving existing (possibly edited) feature assets
1 parent 0a229c1 commit 15b59c1

File tree

2 files changed

+73
-6
lines changed

2 files changed

+73
-6
lines changed

src/theme/common/features/search/search.css

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
/*
2+
* System-default search.css
3+
* */
14
.feat-search {
25
position: relative;
36
height: 3em;

src/theme/index.js

+70-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
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')
33
const { join } = require('path')
44
const Debug = require('../debug')
55
const Settings = require('../settings')
@@ -29,6 +29,7 @@ const Methods = (() => {
2929
Debug.debugLog('refresh theme')
3030
await refreshCustomTheme(customThemePath)
3131
} else {
32+
await refreshFeaturesDirectory(customThemePath)
3233
await collectCustomizerPaths(customThemePath)
3334
}
3435
} else {
@@ -90,11 +91,6 @@ const Methods = (() => {
9091

9192
const copyCommonResources = (targetPath) => {
9293
return Promise.all([
93-
cp(
94-
join(__dirname, 'common', FEATURES),
95-
join(targetPath, FEATURES),
96-
{ recursive: true }
97-
),
9894
cp(
9995
join(__dirname, 'common', PARTIALS.from),
10096
join(targetPath, PARTIALS.to),
@@ -107,6 +103,73 @@ const Methods = (() => {
107103
])
108104
}
109105

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+
110173
const copyBaseThemeResources = (customThemePath) => {
111174
const { theme } = Settings.getSettings()
112175
const themeSrcPath = join(__dirname, '..', '..', 'packages', `theme-${theme}`)
@@ -157,6 +220,7 @@ const Methods = (() => {
157220
])
158221

159222
await copyCommonResources(customThemePath)
223+
await refreshFeaturesDirectory(customThemePath)
160224
await copyBaseThemeResources(customThemePath)
161225
await copyCustomizers(customThemePath)
162226
}

0 commit comments

Comments
 (0)