From 0f938e5c168de8e371259b64b67aeb71c6ff1971 Mon Sep 17 00:00:00 2001 From: somrat sorkar Date: Wed, 12 Jul 2023 14:01:56 +0600 Subject: [PATCH] update project setup script --- scripts/projectSetup.js | 42 +++++++++++++++++++++++++++++++++++++++++ scripts/themeSetup.js | 35 ++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) diff --git a/scripts/projectSetup.js b/scripts/projectSetup.js index 16de15f..d87a67f 100644 --- a/scripts/projectSetup.js +++ b/scripts/projectSetup.js @@ -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( diff --git a/scripts/themeSetup.js b/scripts/themeSetup.js index 42b8419..1e90483 100644 --- a/scripts/themeSetup.js +++ b/scripts/themeSetup.js @@ -1,6 +1,29 @@ 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 }); @@ -46,6 +69,18 @@ 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",