-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathwebBridge.js
42 lines (38 loc) · 1.72 KB
/
webBridge.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
/*jshint esversion: 6 */
const { configurationKey } = require('./setup.js');
const fs = require('fs-extra');
const yaml = require('js-yaml');
const webConfigurationPath = 'web/_data/config.json';
const assetServerConfigurationPath = 'asset_server/_data/config.json';
const buildsDataPath = 'web/_builds/';
const setWebConfiguration = async (preferences, configuration) => {
try {
const prefs = await preferences.getItem(configurationKey);
const webConfigPath = prefs.working_directory + webConfigurationPath;
const webConfiguration = JSON.parse(fs.readFileSync(webConfigPath, 'utf8'));
Object.assign(webConfiguration, configuration);
fs.writeFileSync(webConfigPath, JSON.stringify(webConfiguration));
if (prefs.assets_web) {
const assetServerConfigPath = prefs.working_directory + assetServerConfigurationPath;
fs.copySync(webConfigPath, assetServerConfigPath);
}
} catch (error) {
console.log(error);
}
}
const addBuild = async (preferences, build) => {
try {
const prefs = await preferences.getItem(configurationKey);
const webConfigPath = prefs.working_directory + webConfigurationPath;
const webConfiguration = JSON.parse(fs.readFileSync(webConfigPath, 'utf8'));
const buildPath = prefs.working_directory + buildsDataPath + build.folderPath + '.md';
const contents = `---\nlayout: build\n${yaml.safeDump(build)}---\n`;
fs.writeFileSync(buildPath, contents);
if (!webConfiguration.initialized) {
await setWebConfiguration(preferences, { "initialized": true });
}
} catch (error) {
console.log(error);
}
}
module.exports = { setWebConfiguration, addBuild };