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