-
Notifications
You must be signed in to change notification settings - Fork 6
/
docker-inject-config.js
44 lines (41 loc) · 1.84 KB
/
docker-inject-config.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
/**
* The script is used in preparation to statically serve the application in /dist
*
* Injects front-end configuration properties in dist/index.html after building. This is used for Docker Images where the configuration file is provided only after the Vue app is compiled.
* Adapted from <https://medium.com/@marius.dras/dockerize-a-vue-app-with-a-configuration-file-a77cd98c879b>
*/
const fs = require('fs');
const htmlFile = './dist/index.html';
const configFile = './src/config/config.json';
if (!fs.existsSync(htmlFile)) {
console.warn(`The project is not compiled (${htmlFile} is missing for post-compilation config injection)`);
return;
}
const html = fs.readFileSync(htmlFile).toString();
let config;
if ('QUIT_URL' in process.env || 'SPARQL_QUERY' in process.env){
console.log('Environment found construct config from environment variables.');
const endpoint = {}
if ('QUIT_URL' in process.env) {
endpoint.quit_url = process.env.QUIT_URL
} else {
endpoint.query_url = process.env.SPARQL_QUERY
if ('SPARQL_UPDATE' in process.env) {
endpoint.update_url = process.env.SPARQL_UPDATE
}
}
config = {
"endpoint": endpoint,
"graph_iri": process.env.PRESELECTED_GRAPH_IRI,
"resource_iri": process.env.PRESELECTED_RESOURCE_IRI
}
} else if (fs.existsSync(configFile)) {
console.log(`Local config file found use it: ${configFile}`);
config = require(configFile);
} else {
console.warn(`Configuration is missing for post-compilation config injection. Provide ${configFile} or QUIT_UPDATE and QUIT_QUERY environment variables.`);
}
const configVariablePattern = /<script>const APP_CONFIG = null;<\/script>/g;
const newConfigVariable = '<script>const APP_CONFIG = ' + JSON.stringify(config) + ';</script>';
const injectedHtml = html.replace(configVariablePattern, newConfigVariable);
fs.writeFileSync(htmlFile, injectedHtml);