/ scripts / projectSetup.js
projectSetup.js
  1  const fs = require("fs");
  2  const path = require("path");
  3  
  4  const toggleComment = ({ filepath, regex }) => {
  5    let updatedContent = fs.readFileSync(filepath, "utf8");
  6    const match = updatedContent.match(regex);
  7  
  8    if (match) {
  9      const matchedContent = match[0];
 10      const hasComment = matchedContent.startsWith("# ");
 11      if (hasComment) {
 12        updatedContent = updatedContent.replace(
 13          regex,
 14          matchedContent.replace("# ", ""),
 15        );
 16        fs.writeFileSync(filepath, updatedContent, "utf8");
 17      } else {
 18        const hasBreakline = matchedContent.includes("\n");
 19        if (hasBreakline) {
 20          const content = matchedContent
 21            .split("\n")
 22            .map((line) => "# " + line)
 23            .join("\n");
 24          updatedContent = updatedContent.replace(regex, content);
 25          fs.writeFileSync(filepath, updatedContent, "utf8");
 26        }
 27      }
 28    }
 29  };
 30  
 31  const getFolderName = (rootfolder) => {
 32    const configPath = path.join(rootfolder, "exampleSite/hugo.toml");
 33    const getConfig = fs.readFileSync(configPath, "utf8");
 34    const match = getConfig.match(/theme\s*=\s*\[?"([^"\]]+)"\]?/);
 35    let selectedTheme = null;
 36    if (match && match[1]) {
 37      selectedTheme = match[1];
 38    }
 39    return selectedTheme;
 40  };
 41  
 42  const deleteFolder = (folderPath) => {
 43    if (fs.existsSync(folderPath)) {
 44      fs.rmSync(folderPath, { recursive: true, force: true });
 45    }
 46  };
 47  
 48  const createNewfolder = (rootfolder, folderName) => {
 49    const newFolder = path.join(rootfolder, folderName);
 50    fs.mkdirSync(newFolder, { recursive: true });
 51    return newFolder;
 52  };
 53  
 54  const iterateFilesAndFolders = (rootFolder, { destinationRoot }) => {
 55    const directory = path.join(rootFolder);
 56    const items = fs.readdirSync(directory, { withFileTypes: true });
 57    items.forEach((item) => {
 58      if (item.isDirectory()) {
 59        createNewfolder(destinationRoot, item.name);
 60        iterateFilesAndFolders(path.join(directory, item.name), {
 61          currentFolder: item.name,
 62          destinationRoot: path.join(destinationRoot, item.name),
 63        });
 64      } else {
 65        const sourceFile = path.join(directory, item.name);
 66        const destinationFile = path.join(destinationRoot, item.name);
 67        fs.renameSync(sourceFile, destinationFile);
 68      }
 69    });
 70  };
 71  
 72  const setupProject = () => {
 73    const rootfolder = path.join(__dirname, "../");
 74    if (!fs.existsSync(path.join(rootfolder, "themes"))) {
 75      // remove this part if you don't using theme demo as a module
 76      [
 77        {
 78          filepath: path.join(rootfolder, "exampleSite/hugo.toml"),
 79          regex: /^.*theme\s*=\s*("[^"\]]+"|\S+)/m,
 80        },
 81        {
 82          filepath: path.join(
 83            rootfolder,
 84            "exampleSite/config/_default/module.toml",
 85          ),
 86          regex: /\[\[imports\]\]\s*\r?\npath = "([^"]+)"/,
 87        },
 88      ].forEach(toggleComment);
 89  
 90      const folderList = ["layouts", "assets", "static"];
 91      const folderName = getFolderName(rootfolder);
 92      const newfolderName = createNewfolder(
 93        path.join(rootfolder, "themes"),
 94        folderName,
 95      );
 96  
 97      folderList.forEach((folder) => {
 98        const source = path.join(rootfolder, folder);
 99        const destination = path.join(newfolderName, folder);
100        if (fs.existsSync(source)) {
101          fs.mkdirSync(destination, { recursive: true });
102          iterateFilesAndFolders(source, {
103            currentFolder: folder,
104            destinationRoot: destination,
105          });
106          deleteFolder(source);
107        }
108      });
109  
110      const exampleSite = path.join(rootfolder, "exampleSite");
111      iterateFilesAndFolders(exampleSite, { destinationRoot: rootfolder });
112      deleteFolder(exampleSite);
113    }
114  };
115  
116  setupProject();