-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.js
More file actions
131 lines (107 loc) · 3.5 KB
/
setup.js
File metadata and controls
131 lines (107 loc) · 3.5 KB
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
126
127
128
129
130
131
const fs = require("fs");
const {
deleteAllSubDirectories,
rebuildSite,
copyFolder,
getGroupSrcDir,
updateFlaxSiteConfigFile,
getGroupPublicDir,
downloadAndSaveFile,
updateFlaxSiteFile,
} = require("./helpers");
const syncAllData = require("./syncData");
const { getGroups } = require("./groups");
const { getCMSLayout, getActiveCmsFilesTree } = require("./queries");
const DEFAULT_GROUP = { id: "", name: "kotahi" };
const reCreateFileStructure= async (files, parentFolder ) => {
const entityName = parentFolder + '/' + files.name;
if (files.fileId === null) {
fs.mkdirSync(entityName, { recursive: true });
}
if (files.children && files.children.length > 0) {
for (const child of files.children) {
await reCreateFileStructure(child, entityName);
}
}
if (files.fileId && files.url) {
await downloadAndSaveFile(files.url, entityName)
.then(() => console.log(`File ${entityName} downloaded and saved successfully.`))
.catch(error => console.error(`Error downloading file ${entityName}: ${error.message}`));
}
}
const setupDirectoryFromUrl = async (group, hexCode) => {
if (hexCode) {
// Eleventy doesn't reliably delete a non-draft group that it has previously built.
// So we force its deletion.
await deleteAllSubDirectories(`public/${group.name}`)
}
const currentGroupDir = getGroupSrcDir(group);
await deleteAllSubDirectories(currentGroupDir);
fs.mkdirSync(currentGroupDir, { recursive: true });
const updatedConfig = {
defaultImagesDirectory: `${hexCode ? '/' + hexCode : ''}/assets/images/`,
defaultArticleDirectory: `${hexCode ? '/' + hexCode : ''}/articles/`,
group,
}
const files = await getActiveCmsFilesTree(group)
const parsedFiles = JSON.parse(files.getActiveCmsFilesTree)
for (const child of parsedFiles.children) {
await reCreateFileStructure(child, getGroupSrcDir(group, hexCode));
}
// await copyFolder(defaultGroupDir, currentGroupDir);
await updateFlaxSiteConfigFile(group, hexCode, updatedConfig);
await setupSiteFlag(group, hexCode);
return true;
}
const setupGroup = async (currentGroup, cmsLayout, buildConfig) => {
const { hexCode } = cmsLayout
const publicDir = getGroupPublicDir(currentGroup, hexCode);
const currentGroupDir = getGroupSrcDir(currentGroup, hexCode);
const updatedConfig = {
group: currentGroup,
defaultImagesDirectory: `${hexCode ? '/' + hexCode : ''}/assets/images/`,
defaultArticleDirectory: `${hexCode ? '/' + hexCode : ''}/articles/`,
...buildConfig.updatedConfig,
}
await setupDirectoryFromUrl(currentGroup, hexCode)
await updateFlaxSiteConfigFile(currentGroup, hexCode, updatedConfig);
if (buildConfig.build != false) {
if (!fs.existsSync(publicDir)) {
try {
await rebuildSite(currentGroup, hexCode);
} catch (err) {
console.log(err);
}
}
await syncAllData(currentGroup, cmsLayout, buildConfig);
// TODO Why are we repeating this?
if (fs.existsSync(publicDir)) {
try {
await rebuildSite(currentGroup, hexCode);
await syncAllData(currentGroup, cmsLayout, buildConfig);
} catch (err) {
console.log(err)
}
}
}
};
const setupSiteFlag = async (group, hexCode) => {
return updateFlaxSiteFile(group, hexCode);
}
const setupAllGroups = async () => {
const groups = await getGroups();
if (!groups) {
console.warn("No groups found.");
return;
}
let results = [];
for (const group of groups) {
const cmsLayout = await getCMSLayout(group)
results.push(await setupGroup(group, cmsLayout, { build: true }));
}
return results;
};
module.exports = {
setupAllGroups,
setupGroup,
};