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