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 fs = require("fs");
const path = require("path"); 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 getFolderName = (rootfolder) => {
const configPath = path.join(rootfolder, "exampleSite/hugo.toml"); const configPath = path.join(rootfolder, "exampleSite/hugo.toml");
const getConfig = fs.readFileSync(configPath, "utf8"); const getConfig = fs.readFileSync(configPath, "utf8");
@ -45,6 +72,21 @@ const iterateFilesAndFolders = (rootFolder, { destinationRoot }) => {
const setupProject = () => { const setupProject = () => {
const rootfolder = path.join(__dirname, "../"); const rootfolder = path.join(__dirname, "../");
if (!fs.existsSync(path.join(rootfolder, "themes"))) { 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 folderList = ["layouts", "assets", "static"];
const folderName = getFolderName(rootfolder); const folderName = getFolderName(rootfolder);
const newfolderName = createNewfolder( const newfolderName = createNewfolder(

View File

@ -1,6 +1,29 @@
const fs = require("fs"); const fs = require("fs");
const path = require("path"); 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 createNewfolder = (rootfolder, folderName) => {
const newFolder = path.join(rootfolder, folderName); const newFolder = path.join(rootfolder, folderName);
fs.mkdirSync(newFolder, { recursive: true }); fs.mkdirSync(newFolder, { recursive: true });
@ -46,6 +69,18 @@ const setupTheme = () => {
const rootFolder = path.join(__dirname, "../"); const rootFolder = path.join(__dirname, "../");
if (!fs.existsSync(path.join(rootFolder, "exampleSite"))) { 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 = [ const includesFiles = [
"tailwind.config.js", "tailwind.config.js",
"postcss.config.js", "postcss.config.js",