Skip to content

Commit 1503f3c

Browse files
committed
accidentally deleted scripts
1 parent ad9e907 commit 1503f3c

File tree

5 files changed

+373
-0
lines changed

5 files changed

+373
-0
lines changed

scripts/clearModules.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const fs = require("fs");
2+
3+
const clearModules = (filePath) => {
4+
if (fs.existsSync(filePath)) {
5+
let fileContent = fs.readFileSync(filePath, "utf8");
6+
fileContent = fileContent.replace(/require\s*\([\s\S]*?\)/, "");
7+
fs.writeFileSync(filePath, fileContent, "utf8");
8+
} else {
9+
console.log("File does not exist.");
10+
}
11+
};
12+
13+
clearModules("go.mod");
14+
clearModules("exampleSite/go.mod");

scripts/projectSetup.js

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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();

scripts/removeDarkmode.js

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
const fs = require("fs");
2+
const path = require("path");
3+
4+
const rootDirs = ["assets/scss", "layouts"];
5+
const configFiles = [
6+
{
7+
filePath: "exampleSite/tailwind.config.js",
8+
patterns: ["darkmode:\\s*{[^}]*},", 'darkMode:\\s*"class",'],
9+
},
10+
{
11+
filePath: "exampleSite/data/theme.json",
12+
patterns: ["colors.darkmode"],
13+
},
14+
];
15+
16+
// asset paths
17+
const deleteAssetList = [
18+
"exampleSite/assets/images/logo-darkmode.png",
19+
"layouts/partials/components/theme-switcher.html",
20+
];
21+
22+
const filePaths = [
23+
{
24+
filePath: "layouts/partials/essentials/header.html",
25+
patterns: [
26+
'{{\\s*partial\\s*"components\\/theme-switcher"\\s*\\([^)]*\\)\\s*}}',
27+
],
28+
},
29+
];
30+
31+
filePaths.forEach(({ filePath, patterns }) =>
32+
removeDarkModeFromFiles(filePath, patterns),
33+
);
34+
35+
deleteAssetList.forEach((asset) => {
36+
try {
37+
fs.unlinkSync(asset);
38+
console.log(`${path.basename(asset)} deleted successfully!`);
39+
} catch (error) {
40+
console.error(`${asset} not found`);
41+
}
42+
});
43+
44+
rootDirs.forEach(removeDarkModeFromPages);
45+
configFiles.forEach(removeDarkMode);
46+
47+
function removeDarkModeFromFiles(filePath, regexPatterns) {
48+
const fileContent = fs.readFileSync(filePath, "utf8");
49+
let updatedContent = fileContent;
50+
regexPatterns.forEach((pattern) => {
51+
const regex = new RegExp(pattern, "g");
52+
updatedContent = updatedContent.replace(regex, "");
53+
});
54+
55+
fs.writeFileSync(filePath, updatedContent, "utf8");
56+
}
57+
58+
// like html file
59+
function removeDarkModeFromPages(directoryPath) {
60+
const files = fs.readdirSync(directoryPath);
61+
62+
files.forEach((file) => {
63+
const filePath = path.join(directoryPath, file);
64+
const stats = fs.statSync(filePath);
65+
if (stats.isDirectory()) {
66+
removeDarkModeFromPages(filePath);
67+
} else if (stats.isFile()) {
68+
removeDarkModeFromFiles(filePath, [
69+
'(?:(?!["])\\S)*dark:(?:(?![,;"])\\S)*',
70+
"@apply?(\\s)*;",
71+
]);
72+
}
73+
});
74+
}
75+
76+
function removeDarkMode(configFile) {
77+
const { filePath, patterns } = configFile;
78+
if (filePath === "exampleSite/tailwind.config.js") {
79+
removeDarkModeFromFiles(filePath, patterns);
80+
} else {
81+
const contentFile = JSON.parse(fs.readFileSync(filePath, "utf8"));
82+
patterns.forEach((pattern) => deleteNestedProperty(contentFile, pattern));
83+
fs.writeFileSync(filePath, JSON.stringify(contentFile));
84+
}
85+
}
86+
87+
function deleteNestedProperty(obj, propertyPath) {
88+
const properties = propertyPath.split(".");
89+
let currentObj = obj;
90+
for (let i = 0; i < properties.length - 1; i++) {
91+
const property = properties[i];
92+
if (currentObj.hasOwnProperty(property)) {
93+
currentObj = currentObj[property];
94+
} else {
95+
return; // Property not found, no need to continue
96+
}
97+
}
98+
delete currentObj[properties[properties.length - 1]];
99+
}

scripts/themeSetup.js

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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();

scripts/themeUpdate.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const { exec } = require("child_process");
2+
3+
const repositoryUrl = "https://github.com/zeon-studio/hugoplate";
4+
const localDirectory = "./themes/hugoplate";
5+
const foldersToFetch = ["assets", "layouts"];
6+
const foldersToSkip = ["exampleSite"];
7+
8+
const fetchFolder = (folder) => {
9+
exec(
10+
`curl -L ${repositoryUrl}/tarball/main | tar -xz --strip-components=1 --directory=${localDirectory} --exclude=$(curl -sL ${repositoryUrl}/tarball/main | tar -tz | grep -E "/(${foldersToSkip.join(
11+
"|",
12+
)})/") */${folder}`,
13+
);
14+
};
15+
16+
// Fetch each specified folder
17+
foldersToFetch.forEach((folder) => {
18+
fetchFolder(folder);
19+
});

0 commit comments

Comments
 (0)