update project setup script

This commit is contained in:
somrat sorkar
2023-07-12 14:01:56 +06:00
parent fe5cdce6d7
commit 0f938e5c16
2 changed files with 77 additions and 0 deletions

View File

@@ -1,6 +1,33 @@
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");
@@ -45,6 +72,21 @@ const iterateFilesAndFolders = (rootFolder, { destinationRoot }) => {
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(