build(theme): Patch theme packaging and generation function (#1162)

This commit is contained in:
chenxi-20 2023-12-19 15:03:55 +08:00 committed by GitHub
parent 3dfe4cc082
commit 9ee37fc23d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 117 additions and 145 deletions

View File

@ -63,169 +63,146 @@ const cssScopedMap = {
const createTheme = (callbackFn) => { const createTheme = (callbackFn) => {
const createDir = (path, fn = null) => { const createDir = (path, fn = null) => {
fs.mkdir(path, (err) => { if (isPathExist(path)) {
if (err) { return
console.log(path, err) }
}
fn && fn()
})
}
let timer = null fs.mkdirSync(path)
const readerDirStart = () => {
clearTimeout(timer) fn && fn()
timer = null
timer = setTimeout(() => {
readDir(originRootPath, 'less')
readDir(originThemeRootPath, 'js')
}, 10)
} }
const beginCreateDir = () => { const beginCreateDir = () => {
for (let k in buildThemePathMap) { for (let k in buildThemePathMap) {
createDir(originRootPath + k, readerDirStart) createDir(originRootPath + k)
} }
readDir(originRootPath, 'less')
readDir(originThemeRootPath, 'js')
callbackFn()
} }
const readDir = (originPath, type) => { const readDir = (originPath, type) => {
fs.readdir(originPath, (err, data) => { const fileNames = fs.readdirSync(originPath)
if (err) { fileNames.forEach((fileDir) => {
console.log('readDir', err) mkStat(originPath, fileDir, type)
return
}
data.forEach((fileDir) => {
mkStat(originPath, fileDir, type)
})
}) })
} }
let isFinalTimer = null let isFinalTimer = null
const isFinalFn = () => { const copyDir = (originPath, targetPath) => {
clearTimeout(isFinalTimer) fsExtra.copySync(originPath, targetPath)
isFinalTimer = null
isFinalTimer = setTimeout(() => {
callbackFn()
}, 20)
} }
const copyDir = (originPath, targetPath) => { const isPathExist = (path) => {
fsExtra.copy(originPath, targetPath, (err) => { return fs.existsSync(path)
if (err) {
console.log(err)
}
})
} }
const mkStat = (originPath, fileDir, type) => { const mkStat = (originPath, fileDir, type) => {
const statPath = `${originPath}/${fileDir}` const statPath = `${originPath}/${fileDir}`
const smbPath = `${originPath}/smb-theme/${fileDir}` const smbPath = `${originPath}/${smbThemeName}/${fileDir}`
const auroraPath = `${originPath}/aurora-theme/${fileDir}` const auroraPath = `${originPath}/${auroraThemeName}/${fileDir}`
fs.stat(statPath, (err, data) => {
if (err) { if (!isPathExist(statPath)) {
console.log('mkStat', err) return
return }
} const data = fs.statSync(statPath)
if (data.isDirectory()) {
if (fileDir !== 'base' && fileDir !== 'theme') { if (data.isDirectory()) {
if (fileDir === 'images') { if (fileDir !== 'base' && fileDir !== 'theme') {
copyDir(statPath, smbPath) if (fileDir === 'images') {
copyDir(statPath, auroraPath)
} else {
isFileExist(originPath, fileDir, type)
}
}
} else {
if (fileDir === 'index.less') {
copyDir(statPath, smbPath) copyDir(statPath, smbPath)
copyDir(statPath, auroraPath) copyDir(statPath, auroraPath)
} else {
isFileExist(originPath, fileDir, type)
} }
} }
}) } else {
if (fileDir === 'index.less') {
copyDir(statPath, smbPath)
copyDir(statPath, auroraPath)
}
}
} }
const isFileExist = (originPath, fileDir, type) => { const isFileExist = (originPath, fileDir, type) => {
const parentPath = `${originPath}${fileDir}` const parentPath = `${originPath}${fileDir}`
const path = `${parentPath}/index.${type}` const path = `${parentPath}/index.${type}`
fs.exists(path, (exist) => { const exist = fs.existsSync(path)
if (exist) { if (exist) {
if (type === 'less') { if (type === 'less') {
readComponentsFile(fileDir) readComponentsFile(fileDir)
} else if (type === 'js') { } else if (type === 'js') {
readThemeFile(fileDir) readThemeFile(fileDir)
}
} }
}) }
} }
const writeThemeIndexLess = (path, fileDir, themeName) => { const writeThemeIndexLess = (path, fileDir, themeName) => {
fs.stat(path, (err, data) => { const parentPath = `${originRootPath}${themeName}/${fileDir}`
const parentPath = `${originRootPath}${themeName}/${fileDir}`
const writeTheme = () => {
if (err) {
writeFile(parentPath + '/index.less', emptyThemeContent.replace('{{}}', fileDir))
return const writeTheme = () => {
} if (!isPathExist(path)) {
writeFile(parentPath + '/index.less', emptyThemeContent.replace('{{}}', fileDir))
if (!data.isDirectory()) { return
fs.readFile(path, 'utf8', (subErr, dataStr) => {
if (subErr) {
console.log(subErr)
return
}
const startIndex = dataStr.indexOf('{') + 4
const endIndex = dataStr.indexOf('}')
const newDataStr =
dataStr
.slice(startIndex, endIndex)
.replace(/\'ti-/g, '--ti-')
.replace(/\'/g, '')
.replace(/\,\n/g, ';\n')
.replace(/\, \/\//g, '; //')
.slice(0, -1) + ';'
let scropedData = scopedTitle.replace('{{}}', fileDir)
if (cssScopedMap[fileDir]) {
if (Array.isArray(cssScopedMap[fileDir])) {
cssScopedMap[fileDir].forEach((item) => {
scropedData += scopedContent.replace(/\{compName\}/g, item).replace(/\{var\}/g, newDataStr)
})
} else {
scropedData += scopedContent
.replace(/\{compName\}/g, cssScopedMap[fileDir])
.replace(/\{var\}/g, newDataStr)
}
} else {
scropedData += scopedContent.replace(/\{compName\}/g, fileDir).replace(/\{var\}/g, newDataStr)
}
writeFile(parentPath + '/index.less', scropedData)
})
}
} }
createDir(parentPath, writeTheme) const data = fs.statSync(path)
})
if (!data.isDirectory()) {
const dataStr = fs.readFileSync(path, { encoding: 'utf8' })
const startIndex = dataStr.indexOf('{') + 4
const endIndex = dataStr.indexOf('}')
const newDataStr =
dataStr
.slice(startIndex, endIndex)
.replace(/\'ti-/g, '--ti-')
.replace(/\'/g, '')
.replace(/\,\n/g, ';\n')
.replace(/\, \/\//g, '; //')
.slice(0, -1) + ';'
let scropedData = scopedTitle.replace('{{}}', fileDir)
if (cssScopedMap[fileDir]) {
if (Array.isArray(cssScopedMap[fileDir])) {
cssScopedMap[fileDir].forEach((item) => {
scropedData += scopedContent.replace(/\{compName\}/g, item).replace(/\{var\}/g, newDataStr)
})
} else {
scropedData += scopedContent.replace(/\{compName\}/g, cssScopedMap[fileDir]).replace(/\{var\}/g, newDataStr)
}
} else {
scropedData += scopedContent.replace(/\{compName\}/g, fileDir).replace(/\{var\}/g, newDataStr)
}
writeFile(parentPath + '/index.less', scropedData)
}
}
createDir(parentPath, writeTheme)
} }
const readComponentsFile = (fileDir) => { const readComponentsFile = (fileDir) => {
if (fileDir === auroraThemeName || fileDir === smbThemeName) {
return
}
const indexPath = `${originRootPath}/${fileDir}/${indexLessPath}` const indexPath = `${originRootPath}/${fileDir}/${indexLessPath}`
if (!isPathExist(indexPath)) {
return
}
const smbPath = `${originRootPath}/${fileDir}/${smbThemeName}.js` const smbPath = `${originRootPath}/${fileDir}/${smbThemeName}.js`
const auroraPath = `${originRootPath}/${fileDir}/${auroraThemeName}.js` const auroraPath = `${originRootPath}/${fileDir}/${auroraThemeName}.js`
fs.readFile(indexPath, 'utf8', (err, dataStr) => { const dataStr = fs.readFileSync(indexPath, 'utf8')
if (err) {
console.log(err)
}
// 写入smb-theme/**/index.less // // 写入smb-theme/**/index.less
writeThemeIndexLess(smbPath, fileDir, smbThemeName) writeThemeIndexLess(smbPath, fileDir, smbThemeName)
// 写入aurora-theme/**/index.less // 写入aurora-theme/**/index.less
writeThemeIndexLess(auroraPath, fileDir, auroraThemeName) writeThemeIndexLess(auroraPath, fileDir, auroraThemeName)
})
} }
const readThemeFile = (fileDir) => { const readThemeFile = (fileDir) => {
@ -235,38 +212,33 @@ const createTheme = (callbackFn) => {
return return
} }
fs.readFile(indexPath, 'utf8', (err, dataStr) => { if (!isPathExist(indexPath)) {
createDir(buildThemePathMap[fileDir]) return
}
if (err) { const dataStr = fs.readFileSync(indexPath, 'utf8')
console.log(err)
}
const startIndex = dataStr.indexOf('{') - 1 createDir(buildThemePathMap[fileDir])
const endIndex = dataStr.indexOf('}')
let newDataStr = dataStr.slice(startIndex, endIndex)
const lastIndex = newDataStr.lastIndexOf("'")
newDataStr =
baseContent +
newDataStr
.slice(0, lastIndex)
.replace(/\'ti-/g, '--ti-')
.replace(/\'/g, '')
.replace(/\,\n/g, ';\n')
.replace(/\, \/\//g, '; //') +
';\n}'
writeFile(buildThemePathMap[fileDir] + '/index.less', newDataStr) const startIndex = dataStr.indexOf('{') - 1
}) const endIndex = dataStr.indexOf('}')
let newDataStr = dataStr.slice(startIndex, endIndex)
const lastIndex = newDataStr.lastIndexOf("'")
newDataStr =
baseContent +
newDataStr
.slice(0, lastIndex)
.replace(/\'ti-/g, '--ti-')
.replace(/\'/g, '')
.replace(/\,\n/g, ';\n')
.replace(/\, \/\//g, '; //') +
';\n}'
writeFile(buildThemePathMap[fileDir] + '/index.less', newDataStr)
} }
const writeFile = (filePath, data) => { const writeFile = (filePath, data) => {
fs.writeFile(filePath, data, 'utf8', (err) => { fs.writeFileSync(filePath, data, 'utf8')
if (err) {
console.log('writeFile', err)
}
isFinalFn()
})
} }
beginCreateDir() beginCreateDir()