2024-04-26 23:19:09 +02:00
|
|
|
const fs = require('fs')
|
|
|
|
|
const path = require('path')
|
2024-02-14 16:01:14 +00:00
|
|
|
|
|
|
|
|
const toggleComment = ({ filepath, regex }) => {
|
2024-04-26 23:19:09 +02:00
|
|
|
let updatedContent = fs.readFileSync(filepath, 'utf8')
|
|
|
|
|
const match = updatedContent.match(regex)
|
2024-02-14 16:01:14 +00:00
|
|
|
|
|
|
|
|
if (match) {
|
2024-04-26 23:19:09 +02:00
|
|
|
const matchedContent = match[0]
|
|
|
|
|
const hasComment = matchedContent.startsWith('# ')
|
2024-02-14 16:01:14 +00:00
|
|
|
if (hasComment) {
|
2024-04-26 23:19:09 +02:00
|
|
|
const hasBreakline = matchedContent.includes('\n')
|
2024-02-14 16:01:14 +00:00
|
|
|
if (hasBreakline) {
|
2024-04-26 23:19:09 +02:00
|
|
|
updatedContent = updatedContent.replace(regex, matchedContent.replace(/# /gm, ''))
|
|
|
|
|
fs.writeFileSync(filepath, updatedContent, 'utf8')
|
2024-02-14 16:01:14 +00:00
|
|
|
}
|
|
|
|
|
} else {
|
2024-04-26 23:19:09 +02:00
|
|
|
updatedContent = updatedContent.replace(regex, '# ' + matchedContent)
|
|
|
|
|
fs.writeFileSync(filepath, updatedContent, 'utf8')
|
2024-02-14 16:01:14 +00:00
|
|
|
}
|
|
|
|
|
}
|
2024-04-26 23:19:09 +02:00
|
|
|
}
|
2024-02-14 16:01:14 +00:00
|
|
|
|
|
|
|
|
const createNewfolder = (rootfolder, folderName) => {
|
2024-04-26 23:19:09 +02:00
|
|
|
const newFolder = path.join(rootfolder, folderName)
|
|
|
|
|
fs.mkdirSync(newFolder, { recursive: true })
|
|
|
|
|
return newFolder
|
|
|
|
|
}
|
2024-02-14 16:01:14 +00:00
|
|
|
|
|
|
|
|
const deleteFolder = (folderPath) => {
|
|
|
|
|
if (fs.existsSync(folderPath)) {
|
2024-04-26 23:19:09 +02:00
|
|
|
fs.rmSync(folderPath, { recursive: true, force: true })
|
2024-02-14 16:01:14 +00:00
|
|
|
}
|
2024-04-26 23:19:09 +02:00
|
|
|
}
|
2024-02-14 16:01:14 +00:00
|
|
|
|
|
|
|
|
const getFolderName = (rootfolder) => {
|
2024-04-26 23:19:09 +02:00
|
|
|
const configPath = path.join(rootfolder, 'exampleSite/hugo.toml')
|
|
|
|
|
const getConfig = fs.readFileSync(configPath, 'utf8')
|
|
|
|
|
const match = getConfig.match(/theme\s*=\s*\[?"([^"\]]+)"\]?/)
|
|
|
|
|
let selectedTheme = null
|
2024-02-14 16:01:14 +00:00
|
|
|
if (match && match[1]) {
|
2024-04-26 23:19:09 +02:00
|
|
|
selectedTheme = match[1]
|
2024-02-14 16:01:14 +00:00
|
|
|
}
|
2024-04-26 23:19:09 +02:00
|
|
|
return selectedTheme
|
|
|
|
|
}
|
2024-02-14 16:01:14 +00:00
|
|
|
|
|
|
|
|
const iterateFilesAndFolders = (rootFolder, { destinationRoot }) => {
|
2024-04-26 23:19:09 +02:00
|
|
|
const directory = path.join(rootFolder)
|
|
|
|
|
const items = fs.readdirSync(directory, { withFileTypes: true })
|
2024-02-14 16:01:14 +00:00
|
|
|
items.forEach((item) => {
|
|
|
|
|
if (item.isDirectory()) {
|
2024-04-26 23:19:09 +02:00
|
|
|
createNewfolder(destinationRoot, item.name)
|
2024-02-14 16:01:14 +00:00
|
|
|
iterateFilesAndFolders(path.join(directory, item.name), {
|
|
|
|
|
currentFolder: item.name,
|
|
|
|
|
destinationRoot: path.join(destinationRoot, item.name),
|
2024-04-26 23:19:09 +02:00
|
|
|
})
|
2024-02-14 16:01:14 +00:00
|
|
|
} else {
|
2024-04-26 23:19:09 +02:00
|
|
|
const sourceFile = path.join(directory, item.name)
|
|
|
|
|
const destinationFile = path.join(destinationRoot, item.name)
|
|
|
|
|
fs.renameSync(sourceFile, destinationFile)
|
2024-02-14 16:01:14 +00:00
|
|
|
}
|
2024-04-26 23:19:09 +02:00
|
|
|
})
|
|
|
|
|
}
|
2024-02-14 16:01:14 +00:00
|
|
|
|
|
|
|
|
const setupTheme = () => {
|
2024-04-26 23:19:09 +02:00
|
|
|
const rootFolder = path.join(__dirname, '../')
|
2024-02-14 16:01:14 +00:00
|
|
|
|
2024-04-26 23:19:09 +02:00
|
|
|
if (!fs.existsSync(path.join(rootFolder, 'exampleSite'))) {
|
2024-02-14 16:01:14 +00:00
|
|
|
// remove this part if you don't using theme demo as a module
|
2024-04-26 23:19:09 +02:00
|
|
|
;[
|
2024-02-14 16:01:14 +00:00
|
|
|
{
|
2024-04-26 23:19:09 +02:00
|
|
|
filepath: path.join(rootFolder, 'config/_default/module.toml'),
|
2024-02-14 16:01:14 +00:00
|
|
|
regex: /# \[\[imports\]\]\s*\r?\n# path = "([^"]+)"/,
|
|
|
|
|
},
|
|
|
|
|
{
|
2024-04-26 23:19:09 +02:00
|
|
|
filepath: path.join(rootFolder, 'hugo.toml'),
|
2024-02-14 16:01:14 +00:00
|
|
|
regex: /^.*theme\s*=\s*("[^"\]]+"|\S+)/m,
|
|
|
|
|
},
|
2024-04-26 23:19:09 +02:00
|
|
|
].forEach(toggleComment)
|
2024-02-14 16:01:14 +00:00
|
|
|
|
|
|
|
|
const includesFiles = [
|
2024-04-26 23:19:09 +02:00
|
|
|
'tailwind.config.js',
|
|
|
|
|
'postcss.config.js',
|
|
|
|
|
'go.mod',
|
|
|
|
|
'hugo.toml',
|
|
|
|
|
'assets',
|
|
|
|
|
'config',
|
|
|
|
|
'data',
|
|
|
|
|
'content',
|
|
|
|
|
'i18n',
|
|
|
|
|
'static',
|
|
|
|
|
]
|
2024-02-14 16:01:14 +00:00
|
|
|
|
2024-04-26 23:19:09 +02:00
|
|
|
const folder = createNewfolder(rootFolder, 'exampleSite')
|
2024-02-14 16:01:14 +00:00
|
|
|
|
|
|
|
|
fs.readdirSync(rootFolder, { withFileTypes: true }).forEach((file) => {
|
|
|
|
|
if (includesFiles.includes(file.name)) {
|
|
|
|
|
if (file.isDirectory()) {
|
2024-04-26 23:19:09 +02:00
|
|
|
const destination = path.join(rootFolder, 'exampleSite', file.name)
|
|
|
|
|
fs.mkdirSync(destination, { recursive: true })
|
2024-02-14 16:01:14 +00:00
|
|
|
iterateFilesAndFolders(path.join(rootFolder, file.name), {
|
|
|
|
|
destinationRoot: destination,
|
2024-04-26 23:19:09 +02:00
|
|
|
})
|
|
|
|
|
deleteFolder(path.join(rootFolder, file.name))
|
2024-02-14 16:01:14 +00:00
|
|
|
} else {
|
2024-04-26 23:19:09 +02:00
|
|
|
fs.renameSync(path.join(rootFolder, file.name), path.join(folder, file.name))
|
2024-02-14 16:01:14 +00:00
|
|
|
}
|
|
|
|
|
}
|
2024-04-26 23:19:09 +02:00
|
|
|
})
|
2024-02-14 16:01:14 +00:00
|
|
|
|
2024-04-26 23:19:09 +02:00
|
|
|
const themes = path.join(rootFolder, 'themes')
|
2024-02-14 16:01:14 +00:00
|
|
|
iterateFilesAndFolders(path.join(themes, getFolderName(rootFolder)), {
|
|
|
|
|
destinationRoot: rootFolder,
|
2024-04-26 23:19:09 +02:00
|
|
|
})
|
|
|
|
|
deleteFolder(themes)
|
2024-02-14 16:01:14 +00:00
|
|
|
}
|
2024-04-26 23:19:09 +02:00
|
|
|
}
|
2024-02-14 16:01:14 +00:00
|
|
|
|
2024-04-26 23:19:09 +02:00
|
|
|
setupTheme()
|