You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
59 lines
1.2 KiB
59 lines
1.2 KiB
const fs = require('fs')
|
|
const path = require('path')
|
|
|
|
const {
|
|
getJson
|
|
} = require('./json')
|
|
|
|
function parseThemeByJsonStr (jsonStr, keys, theme) {
|
|
if (jsonStr.indexOf('@') === -1) {
|
|
return jsonStr
|
|
}
|
|
keys.forEach(key => {
|
|
jsonStr = jsonStr.replace(new RegExp('@' + key, 'g'), theme[key])
|
|
})
|
|
return jsonStr
|
|
}
|
|
|
|
const themeJsonPath = path.join(process.env.UNI_INPUT_DIR, 'theme.json')
|
|
|
|
function hasTheme () {
|
|
return fs.existsSync(themeJsonPath)
|
|
}
|
|
|
|
function darkmode () {
|
|
return !!(global.uniPlugin.options || {}).darkmode
|
|
}
|
|
|
|
module.exports = {
|
|
darkmode,
|
|
hasTheme,
|
|
initTheme () {
|
|
if (!hasTheme()) {
|
|
return
|
|
}
|
|
if (darkmode()) {
|
|
return
|
|
}
|
|
try {
|
|
const theme = getJson('theme.json', true)
|
|
global.uniPlugin.defaultTheme = theme.light
|
|
} catch (e) {
|
|
console.error(e)
|
|
}
|
|
},
|
|
parseTheme (json) {
|
|
const theme = global.uniPlugin.defaultTheme
|
|
if (!theme) {
|
|
return json
|
|
}
|
|
const keys = Object.keys(theme)
|
|
if (!keys.length) {
|
|
return json
|
|
}
|
|
if (typeof json === 'string') {
|
|
return parseThemeByJsonStr(json, keys, theme)
|
|
}
|
|
return JSON.parse(parseThemeByJsonStr(JSON.stringify(json), keys, theme))
|
|
}
|
|
}
|
|
|