forked from zeon-studio/hugoplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprojectSetup.js
125 lines (113 loc) · 3.72 KB
/
projectSetup.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
const fs = require("fs");
const path = require("path");
const toggleComment = ({ filepath, regex }) => {
let updatedContent = fs.readFileSync(filepath, "utf8");
const match = updatedContent.match(regex);
if (filepath.endsWith("hugo.toml")) {
updatedContent = updatedContent.replace(
'baseURL = "https://example.org"',
'baseURL = "/"',
);
}
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 hasLineBreak = matchedContent.includes("\n");
if (hasLineBreak) {
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");
const match = getConfig.match(/theme\s*=\s*\[?"([^"\]]+)"\]?/);
let selectedTheme = null;
if (match && match[1]) {
selectedTheme = match[1];
}
return selectedTheme;
};
const deleteFolder = (folderPath) => {
if (fs.existsSync(folderPath)) {
fs.rmSync(folderPath, { recursive: true, force: true });
}
};
const createNewFolder = (rootFolder, folderName) => {
const newFolder = path.join(rootFolder, folderName);
fs.mkdirSync(newFolder, { recursive: true });
return newFolder;
};
const iterateFilesAndFolders = (rootFolder, { destinationRoot }) => {
const directory = path.join(rootFolder);
const items = fs.readdirSync(directory, { withFileTypes: true });
items.forEach((item) => {
if (item.isDirectory()) {
createNewFolder(destinationRoot, item.name);
iterateFilesAndFolders(path.join(directory, item.name), {
currentFolder: item.name,
destinationRoot: path.join(destinationRoot, item.name),
});
} else {
const sourceFile = path.join(directory, item.name);
const destinationFile = path.join(destinationRoot, item.name);
fs.renameSync(sourceFile, destinationFile);
}
});
};
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(
path.join(rootFolder, "themes"),
folderName,
);
folderList.forEach((folder) => {
const source = path.join(rootFolder, folder);
const destination = path.join(newFolderName, folder);
if (fs.existsSync(source)) {
fs.mkdirSync(destination, { recursive: true });
iterateFilesAndFolders(source, {
currentFolder: folder,
destinationRoot: destination,
});
deleteFolder(source);
}
});
const exampleSite = path.join(rootFolder, "exampleSite");
iterateFilesAndFolders(exampleSite, { destinationRoot: rootFolder });
deleteFolder(exampleSite);
} else {
console.log("Project already setup");
}
};
setupProject();