-
-
Notifications
You must be signed in to change notification settings - Fork 147
Expand file tree
/
Copy pathsetup.js
More file actions
306 lines (278 loc) · 8.76 KB
/
Copy pathsetup.js
File metadata and controls
306 lines (278 loc) · 8.76 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/* eslint-disable no-restricted-syntax */
/* eslint-disable no-console */
const fs = require("fs");
const fse = require("fs-extra");
const path = require("path");
const yaml = require("js-yaml");
const _ = require("lodash");
const rootDir = process.cwd();
const organizationsDir = path.join(rootDir, "organizations");
const internalConfigDir = path.join(path.join(rootDir, "internals"), "config");
const clientDir = path.join(rootDir, "client");
const serverDir = path.join(rootDir, "server");
const clientConfigsDir = path.join(clientDir, "configs");
const extraJSFilesDir = path.join(organizationsDir, "js");
// array to store configurations of the organizations
const clientConfigs = [];
const serverConfigs = [];
const organizations = [];
const defaultConfigFile = path.join(internalConfigDir, "default.yml");
// eslint-disable-next-line consistent-return
const customizer = (objValue, srcValue) => {
if (_.isArray(objValue) && _.isArray(srcValue)) return srcValue;
};
const merge = (origObj, srcObj) => _.mergeWith(origObj, srcObj, customizer);
const removeNullKeys = (obj) => {
const object = obj;
Object.entries(object).forEach(([k, v]) => {
if (v && typeof v === "object") {
removeNullKeys(v);
}
if ((v && typeof v === "object" && !Object.keys(v).length) || v === null) {
if (Array.isArray(object)) {
object.splice(k, 1);
} else if (!_.isEqual(object[k], {})) {
delete object[k];
}
}
});
return object;
};
const removeDefaultConfig = () => {
const organizationsFile = path.join(clientDir, "organizations.json");
const config = `${path.join(clientConfigsDir, "default")}.json`;
if (fs.existsSync(config)) fs.rmSync(config, {recursive: true});
if (fs.existsSync(organizationsFile)) {
const organization = JSON.parse(fs.readFileSync(organizationsFile));
fs.writeFileSync(
organizationsFile,
JSON.stringify(
organization.filter((org) => org.slug !== "default"),
null,
2,
),
);
}
const serverConfigFile = path.join(serverDir, "config.json");
if (fs.existsSync(serverConfigFile)) {
const serverConfig = JSON.parse(fs.readFileSync(serverConfigFile));
fs.writeFileSync(
serverConfigFile,
JSON.stringify(
serverConfig.filter((org) => org.slug !== "default"),
null,
2,
),
);
}
};
const getConfig = (filePath) => {
let defaultConfig = {};
if (fs.existsSync(defaultConfigFile))
defaultConfig = removeNullKeys(
yaml.load(fs.readFileSync(defaultConfigFile, "utf-8")),
);
const config = yaml.load(fs.readFileSync(filePath, "utf-8"));
return removeNullKeys(merge(defaultConfig, config));
};
const getModalContent = (config, modalKey, modalName, configDirPath) => {
const content = config.client[modalKey];
if (content) {
for (const key of Object.keys(content)) {
if (
path.extname(`${configDirPath}/server_assets/${content[key]}`) === ".md"
) {
if (!fs.existsSync(`${configDirPath}/server_assets/${content[key]}`)) {
content[key] = "";
console.warn(
`no such file or directory '${configDirPath}/server_assets/${content[key]}'. ${modalName} content key '${key}' is set null.`,
);
}
} else {
console.warn(
`'${content[key]}' is not a markdown file. ${modalName} content key '${key}' is set null.`,
);
content[key] = "";
}
}
}
return content;
};
const createConfig = (data, configDirPath, radiusSlug = null) => {
try {
const config = data;
// convert markdown to html
if (config.client && config.client.privacy_policy) {
config.client.privacy_policy = getModalContent(
config,
"privacy_policy",
"Privacy policy's",
configDirPath,
);
}
if (config.client && config.client.terms_and_conditions) {
config.client.terms_and_conditions = getModalContent(
config,
"terms_and_conditions",
"Terms and conditions",
configDirPath,
);
}
// extract client config from object
const clientConfig = {
name: config.name,
slug: config.slug,
settings: config.settings,
...config.client,
};
// extract server config from object
const serverConfig = {
name: config.name,
slug: config.slug,
settings: config.settings,
...config.server,
};
if (radiusSlug) {
serverConfig.custom = true;
serverConfig.radiusSlug = radiusSlug;
}
clientConfigs.push(clientConfig);
serverConfigs.push(serverConfig);
organizations.push({slug: config.slug});
// copy client assets
const clientAssetsPath = path.resolve(clientDir, "assets", config.slug);
fse.copySync(
path.join(configDirPath, "client_assets"),
clientAssetsPath,
{overwrite: true},
(err) => console.log(err),
);
// copy server assets
const serverAssetsPath = path.resolve(serverDir, "assets", config.slug);
fse.copySync(
path.join(configDirPath, "server_assets"),
serverAssetsPath,
{overwrite: true},
(err) => console.log(err),
);
} catch (err) {
console.log(err);
}
};
const writeConfigurations = () => {
const organizationsFile = path.join(clientDir, "organizations.json");
const previousSlugs = fs.existsSync(organizationsFile)
? JSON.parse(fs.readFileSync(organizationsFile, "utf-8")).map((o) => o.slug)
: [];
// Reset arrays so re-running this function (e.g. on file-watch re-run) does
// not accumulate duplicate entries from previous invocations.
clientConfigs.length = 0;
serverConfigs.length = 0;
organizations.length = 0;
// loop through all the config files
fs.readdirSync(organizationsDir).forEach((file) => {
const configDirPath = path.join(organizationsDir, file);
const configPath = path.join(configDirPath, `${file}.yml`);
if (fs.existsSync(configPath)) {
// read document, or log exception on error
try {
const config = getConfig(configPath);
createConfig(config, configDirPath);
// variants configurations
fs.readdirSync(configDirPath).forEach((customFile) => {
if (
path.extname(customFile) === ".yml" &&
customFile !== `${file}.yml`
) {
try {
const customConfig = removeNullKeys(
merge(
getConfig(configPath),
yaml.load(
fs.readFileSync(
path.join(configDirPath, customFile),
"utf-8",
),
),
),
);
if (config.slug === customConfig.slug) {
// for same radius organization
customConfig.slug = `${file}-${path.basename(
customFile,
path.extname(customFile),
)}`;
createConfig(customConfig, configDirPath, file);
} else {
// for new organization
createConfig(customConfig, configDirPath);
}
} catch (error) {
console.log(error);
}
}
});
//
} catch (error) {
console.log(error);
}
}
});
const currentSlugs = new Set(organizations.map((o) => o.slug));
previousSlugs
.filter((slug) => !currentSlugs.has(slug))
.forEach((slug) => {
fs.rmSync(path.resolve(clientDir, "assets", slug), {
recursive: true,
force: true,
});
fs.rmSync(path.resolve(serverDir, "assets", slug), {
recursive: true,
force: true,
});
});
if (fs.existsSync(clientConfigsDir))
fs.rmSync(clientConfigsDir, {recursive: true});
if (!fs.existsSync(clientConfigsDir))
fs.mkdirSync(clientConfigsDir, {recursive: true});
clientConfigs.forEach((config) => {
// write client configs
fs.writeFileSync(
`${path.join(clientConfigsDir, config.slug)}.json`,
JSON.stringify(config, null, 2),
(error) => {
if (error) console.log(error);
},
);
});
// write organizations
fs.writeFileSync(
`${clientDir}/organizations.json`,
JSON.stringify(organizations, null, 2),
(error) => {
if (error) console.log(error);
},
);
// write server configs
fs.writeFileSync(
`${serverDir}/config.json`,
JSON.stringify(serverConfigs, null, 2),
(error) => {
if (error) console.log(error);
},
);
};
const getExtraJsScripts = () => {
let customScript = "";
fs.readdirSync(extraJSFilesDir).forEach((file) => {
if (path.extname(file) === ".js")
customScript += `<script src="/${file}"></script>`;
});
return customScript;
};
writeConfigurations();
module.exports = {
removeDefaultConfig,
writeConfigurations,
getExtraJsScripts,
};