-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ad9e907
commit 1503f3c
Showing
5 changed files
with
373 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
const fs = require("fs"); | ||
|
||
const clearModules = (filePath) => { | ||
if (fs.existsSync(filePath)) { | ||
let fileContent = fs.readFileSync(filePath, "utf8"); | ||
fileContent = fileContent.replace(/require\s*\([\s\S]*?\)/, ""); | ||
fs.writeFileSync(filePath, fileContent, "utf8"); | ||
} else { | ||
console.log("File does not exist."); | ||
} | ||
}; | ||
|
||
clearModules("go.mod"); | ||
clearModules("exampleSite/go.mod"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
const fs = require("fs"); | ||
const path = require("path"); | ||
|
||
const toggleComment = ({ filepath, regex }) => { | ||
let updatedContent = fs.readFileSync(filepath, "utf8"); | ||
const match = updatedContent.match(regex); | ||
|
||
if (match) { | ||
const matchedContent = match[0]; | ||
const hasComment = matchedContent.startsWith("# "); | ||
if (hasComment) { | ||
updatedContent = updatedContent.replace( | ||
regex, | ||
matchedContent.replace("# ", ""), | ||
); | ||
fs.writeFileSync(filepath, updatedContent, "utf8"); | ||
} else { | ||
const hasBreakline = matchedContent.includes("\n"); | ||
if (hasBreakline) { | ||
const content = matchedContent | ||
.split("\n") | ||
.map((line) => "# " + line) | ||
.join("\n"); | ||
updatedContent = updatedContent.replace(regex, content); | ||
fs.writeFileSync(filepath, updatedContent, "utf8"); | ||
} | ||
} | ||
} | ||
}; | ||
|
||
const getFolderName = (rootfolder) => { | ||
const configPath = path.join(rootfolder, "exampleSite/hugo.toml"); | ||
const getConfig = fs.readFileSync(configPath, "utf8"); | ||
const match = getConfig.match(/theme\s*=\s*\[?"([^"\]]+)"\]?/); | ||
let selectedTheme = null; | ||
if (match && match[1]) { | ||
selectedTheme = match[1]; | ||
} | ||
return selectedTheme; | ||
}; | ||
|
||
const deleteFolder = (folderPath) => { | ||
if (fs.existsSync(folderPath)) { | ||
fs.rmSync(folderPath, { recursive: true, force: true }); | ||
} | ||
}; | ||
|
||
const createNewfolder = (rootfolder, folderName) => { | ||
const newFolder = path.join(rootfolder, folderName); | ||
fs.mkdirSync(newFolder, { recursive: true }); | ||
return newFolder; | ||
}; | ||
|
||
const iterateFilesAndFolders = (rootFolder, { destinationRoot }) => { | ||
const directory = path.join(rootFolder); | ||
const items = fs.readdirSync(directory, { withFileTypes: true }); | ||
items.forEach((item) => { | ||
if (item.isDirectory()) { | ||
createNewfolder(destinationRoot, item.name); | ||
iterateFilesAndFolders(path.join(directory, item.name), { | ||
currentFolder: item.name, | ||
destinationRoot: path.join(destinationRoot, item.name), | ||
}); | ||
} else { | ||
const sourceFile = path.join(directory, item.name); | ||
const destinationFile = path.join(destinationRoot, item.name); | ||
fs.renameSync(sourceFile, destinationFile); | ||
} | ||
}); | ||
}; | ||
|
||
const setupProject = () => { | ||
const rootfolder = path.join(__dirname, "../"); | ||
if (!fs.existsSync(path.join(rootfolder, "themes"))) { | ||
// remove this part if you don't using theme demo as a module | ||
[ | ||
{ | ||
filepath: path.join(rootfolder, "exampleSite/hugo.toml"), | ||
regex: /^.*theme\s*=\s*("[^"\]]+"|\S+)/m, | ||
}, | ||
{ | ||
filepath: path.join( | ||
rootfolder, | ||
"exampleSite/config/_default/module.toml", | ||
), | ||
regex: /\[\[imports\]\]\s*\r?\npath = "([^"]+)"/, | ||
}, | ||
].forEach(toggleComment); | ||
|
||
const folderList = ["layouts", "assets", "static"]; | ||
const folderName = getFolderName(rootfolder); | ||
const newfolderName = createNewfolder( | ||
path.join(rootfolder, "themes"), | ||
folderName, | ||
); | ||
|
||
folderList.forEach((folder) => { | ||
const source = path.join(rootfolder, folder); | ||
const destination = path.join(newfolderName, folder); | ||
if (fs.existsSync(source)) { | ||
fs.mkdirSync(destination, { recursive: true }); | ||
iterateFilesAndFolders(source, { | ||
currentFolder: folder, | ||
destinationRoot: destination, | ||
}); | ||
deleteFolder(source); | ||
} | ||
}); | ||
|
||
const exampleSite = path.join(rootfolder, "exampleSite"); | ||
iterateFilesAndFolders(exampleSite, { destinationRoot: rootfolder }); | ||
deleteFolder(exampleSite); | ||
} | ||
}; | ||
|
||
setupProject(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
const fs = require("fs"); | ||
const path = require("path"); | ||
|
||
const rootDirs = ["assets/scss", "layouts"]; | ||
const configFiles = [ | ||
{ | ||
filePath: "exampleSite/tailwind.config.js", | ||
patterns: ["darkmode:\\s*{[^}]*},", 'darkMode:\\s*"class",'], | ||
}, | ||
{ | ||
filePath: "exampleSite/data/theme.json", | ||
patterns: ["colors.darkmode"], | ||
}, | ||
]; | ||
|
||
// asset paths | ||
const deleteAssetList = [ | ||
"exampleSite/assets/images/logo-darkmode.png", | ||
"layouts/partials/components/theme-switcher.html", | ||
]; | ||
|
||
const filePaths = [ | ||
{ | ||
filePath: "layouts/partials/essentials/header.html", | ||
patterns: [ | ||
'{{\\s*partial\\s*"components\\/theme-switcher"\\s*\\([^)]*\\)\\s*}}', | ||
], | ||
}, | ||
]; | ||
|
||
filePaths.forEach(({ filePath, patterns }) => | ||
removeDarkModeFromFiles(filePath, patterns), | ||
); | ||
|
||
deleteAssetList.forEach((asset) => { | ||
try { | ||
fs.unlinkSync(asset); | ||
console.log(`${path.basename(asset)} deleted successfully!`); | ||
} catch (error) { | ||
console.error(`${asset} not found`); | ||
} | ||
}); | ||
|
||
rootDirs.forEach(removeDarkModeFromPages); | ||
configFiles.forEach(removeDarkMode); | ||
|
||
function removeDarkModeFromFiles(filePath, regexPatterns) { | ||
const fileContent = fs.readFileSync(filePath, "utf8"); | ||
let updatedContent = fileContent; | ||
regexPatterns.forEach((pattern) => { | ||
const regex = new RegExp(pattern, "g"); | ||
updatedContent = updatedContent.replace(regex, ""); | ||
}); | ||
|
||
fs.writeFileSync(filePath, updatedContent, "utf8"); | ||
} | ||
|
||
// like html file | ||
function removeDarkModeFromPages(directoryPath) { | ||
const files = fs.readdirSync(directoryPath); | ||
|
||
files.forEach((file) => { | ||
const filePath = path.join(directoryPath, file); | ||
const stats = fs.statSync(filePath); | ||
if (stats.isDirectory()) { | ||
removeDarkModeFromPages(filePath); | ||
} else if (stats.isFile()) { | ||
removeDarkModeFromFiles(filePath, [ | ||
'(?:(?!["])\\S)*dark:(?:(?![,;"])\\S)*', | ||
"@apply?(\\s)*;", | ||
]); | ||
} | ||
}); | ||
} | ||
|
||
function removeDarkMode(configFile) { | ||
const { filePath, patterns } = configFile; | ||
if (filePath === "exampleSite/tailwind.config.js") { | ||
removeDarkModeFromFiles(filePath, patterns); | ||
} else { | ||
const contentFile = JSON.parse(fs.readFileSync(filePath, "utf8")); | ||
patterns.forEach((pattern) => deleteNestedProperty(contentFile, pattern)); | ||
fs.writeFileSync(filePath, JSON.stringify(contentFile)); | ||
} | ||
} | ||
|
||
function deleteNestedProperty(obj, propertyPath) { | ||
const properties = propertyPath.split("."); | ||
let currentObj = obj; | ||
for (let i = 0; i < properties.length - 1; i++) { | ||
const property = properties[i]; | ||
if (currentObj.hasOwnProperty(property)) { | ||
currentObj = currentObj[property]; | ||
} else { | ||
return; // Property not found, no need to continue | ||
} | ||
} | ||
delete currentObj[properties[properties.length - 1]]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
const fs = require("fs"); | ||
const path = require("path"); | ||
|
||
const toggleComment = ({ filepath, regex }) => { | ||
let updatedContent = fs.readFileSync(filepath, "utf8"); | ||
const match = updatedContent.match(regex); | ||
|
||
if (match) { | ||
const matchedContent = match[0]; | ||
const hasComment = matchedContent.startsWith("# "); | ||
if (hasComment) { | ||
const hasBreakline = matchedContent.includes("\n"); | ||
if (hasBreakline) { | ||
updatedContent = updatedContent.replace( | ||
regex, | ||
matchedContent.replace(/# /gm, ""), | ||
); | ||
fs.writeFileSync(filepath, updatedContent, "utf8"); | ||
} | ||
} else { | ||
updatedContent = updatedContent.replace(regex, "# " + matchedContent); | ||
fs.writeFileSync(filepath, updatedContent, "utf8"); | ||
} | ||
} | ||
}; | ||
|
||
const createNewfolder = (rootfolder, folderName) => { | ||
const newFolder = path.join(rootfolder, folderName); | ||
fs.mkdirSync(newFolder, { recursive: true }); | ||
return newFolder; | ||
}; | ||
|
||
const deleteFolder = (folderPath) => { | ||
if (fs.existsSync(folderPath)) { | ||
fs.rmSync(folderPath, { recursive: true, force: true }); | ||
} | ||
}; | ||
|
||
const getFolderName = (rootfolder) => { | ||
const configPath = path.join(rootfolder, "exampleSite/hugo.toml"); | ||
const getConfig = fs.readFileSync(configPath, "utf8"); | ||
const match = getConfig.match(/theme\s*=\s*\[?"([^"\]]+)"\]?/); | ||
let selectedTheme = null; | ||
if (match && match[1]) { | ||
selectedTheme = match[1]; | ||
} | ||
return selectedTheme; | ||
}; | ||
|
||
const iterateFilesAndFolders = (rootFolder, { destinationRoot }) => { | ||
const directory = path.join(rootFolder); | ||
const items = fs.readdirSync(directory, { withFileTypes: true }); | ||
items.forEach((item) => { | ||
if (item.isDirectory()) { | ||
createNewfolder(destinationRoot, item.name); | ||
iterateFilesAndFolders(path.join(directory, item.name), { | ||
currentFolder: item.name, | ||
destinationRoot: path.join(destinationRoot, item.name), | ||
}); | ||
} else { | ||
const sourceFile = path.join(directory, item.name); | ||
const destinationFile = path.join(destinationRoot, item.name); | ||
fs.renameSync(sourceFile, destinationFile); | ||
} | ||
}); | ||
}; | ||
|
||
const setupTheme = () => { | ||
const rootFolder = path.join(__dirname, "../"); | ||
|
||
if (!fs.existsSync(path.join(rootFolder, "exampleSite"))) { | ||
// remove this part if you don't using theme demo as a module | ||
[ | ||
{ | ||
filepath: path.join(rootFolder, "config/_default/module.toml"), | ||
regex: /# \[\[imports\]\]\s*\r?\n# path = "([^"]+)"/, | ||
}, | ||
{ | ||
filepath: path.join(rootFolder, "hugo.toml"), | ||
regex: /^.*theme\s*=\s*("[^"\]]+"|\S+)/m, | ||
}, | ||
].forEach(toggleComment); | ||
|
||
const includesFiles = [ | ||
"tailwind.config.js", | ||
"postcss.config.js", | ||
"go.mod", | ||
"hugo.toml", | ||
"assets", | ||
"config", | ||
"data", | ||
"content", | ||
"i18n", | ||
"static", | ||
]; | ||
|
||
const folder = createNewfolder(rootFolder, "exampleSite"); | ||
|
||
fs.readdirSync(rootFolder, { withFileTypes: true }).forEach((file) => { | ||
if (includesFiles.includes(file.name)) { | ||
if (file.isDirectory()) { | ||
const destination = path.join(rootFolder, "exampleSite", file.name); | ||
fs.mkdirSync(destination, { recursive: true }); | ||
iterateFilesAndFolders(path.join(rootFolder, file.name), { | ||
destinationRoot: destination, | ||
}); | ||
deleteFolder(path.join(rootFolder, file.name)); | ||
} else { | ||
fs.renameSync( | ||
path.join(rootFolder, file.name), | ||
path.join(folder, file.name), | ||
); | ||
} | ||
} | ||
}); | ||
|
||
const themes = path.join(rootFolder, "themes"); | ||
iterateFilesAndFolders(path.join(themes, getFolderName(rootFolder)), { | ||
destinationRoot: rootFolder, | ||
}); | ||
deleteFolder(themes); | ||
} | ||
}; | ||
|
||
setupTheme(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
const { exec } = require("child_process"); | ||
|
||
const repositoryUrl = "https://github.com/zeon-studio/hugoplate"; | ||
const localDirectory = "./themes/hugoplate"; | ||
const foldersToFetch = ["assets", "layouts"]; | ||
const foldersToSkip = ["exampleSite"]; | ||
|
||
const fetchFolder = (folder) => { | ||
exec( | ||
`curl -L ${repositoryUrl}/tarball/main | tar -xz --strip-components=1 --directory=${localDirectory} --exclude=$(curl -sL ${repositoryUrl}/tarball/main | tar -tz | grep -E "/(${foldersToSkip.join( | ||
"|", | ||
)})/") */${folder}`, | ||
); | ||
}; | ||
|
||
// Fetch each specified folder | ||
foldersToFetch.forEach((folder) => { | ||
fetchFolder(folder); | ||
}); |