-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.js
executable file
·275 lines (238 loc) · 7.14 KB
/
run.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
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
#!/usr/bin/env node
import { spawnSync } from 'child_process';
import fs from 'fs';
import yaml from 'js-yaml';
import path from 'path';
import { fileURLToPath } from 'url';
const NAME = 'data-portal';
const DEV_MODE = process.argv.slice(2).includes('--dev');
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
/**
* Reads and merges configuration settings from default and specific YAML files.
* Overrides settings with environment variables prefixed with the application name.
*
* @returns {Object} The merged configuration settings.
* @throws {Error} If there is an error reading the configuration files.
*/
function readSettings() {
// Read the default configuration file
const defaultSettingsPath = path.join(__dirname, `${NAME}.default.yaml`);
const defaultSettings = yaml.load(fs.readFileSync(defaultSettingsPath, 'utf8'));
// Read the (optional) development or production specific configuration file
const specificSettingsPath = DEV_MODE ? `.devcontainer/${NAME}.yaml` : `${NAME}.yaml`;
let specificSettings = {};
try {
specificSettings = yaml.load(fs.readFileSync(specificSettingsPath, 'utf8'));
} catch (e) {
if (e.code !== 'ENOENT') throw e; // ignore non-existing file
}
// Merge the default and specific settings
const settings = { ...defaultSettings, ...specificSettings };
// Override settings with environment variables
const prefix = NAME.replace('-', '_');
for (const key in settings) {
if (!settings.hasOwnProperty(key)) continue;
const envVarName = `${prefix}_${key}`;
const value = settings[key];
let envVarValue = process.env[envVarName];
if (envVarValue === undefined) continue;
const isObject = typeof value === 'object' && value !== null;
if (isObject) {
envVarValue = JSON.parse(envVarValue);
} else {
const isBoolean = typeof value === 'boolean';
if (isBoolean) {
envVarValue = envVarValue.toLowerCase() === 'true';
} else {
const isNumber = typeof value === 'number';
if (isNumber) {
envVarValue = parseFloat(envVarValue);
}
}
}
settings[key] = envVarValue;
}
return settings;
}
/**
* Get the subdirectory with the browser files.
*
* @param {string} distDir - The distribution directory.
*/
function getBrowserDir(distDir) {
const browserDir = path.join(distDir, NAME, 'browser');
// Support for flattened distribution directory
return fs.existsSync(browserDir) ? browserDir : distDir;
}
/**
* Write the settings into the config file in the appropriate output directory.
*
* @param {Object} settings - The configuration settings to write.
* @throws {Error} If the output directory does not exist.
*/
function writeSettings(settings) {
const outputDir = DEV_MODE ? 'public' : getBrowserDir('dist');
// Ensure the output directory exists
if (!fs.existsSync(outputDir)) {
throw new Error(`Output directory not found: ${outputDir}`);
}
const configPath = path.join(outputDir, 'config.js');
const configScript = `window.config = ${JSON.stringify(settings)}`;
fs.writeFileSync(configPath, configScript, 'utf8');
}
/**
* Get the name and IP address of the specified URL.
*/
function getNameAndAddress(url) {
const { hostname } = new URL(url);
if (!hostname) {
console.error(`Invalid URL: ${url}`);
}
const result = spawnSync('dig', ['+short', hostname, '@8.8.8.8'], {
encoding: 'utf8',
});
const address = result.error ? null : result.stdout.trim();
if (!address) {
console.error(`Cannot resolve ${hostname}`);
process.exit(1);
}
return [hostname, address];
}
/**
* Add an entry to the /etc/hosts file.
*/
function addHostEntry(name, ip) {
const hostsFile = '/etc/hosts';
const hostsContent = fs.readFileSync(hostsFile, 'utf8');
if (!hostsContent.includes(` ${name}\n`)) {
spawnSync('sudo', ['sh', '-c', `echo "${ip} ${name}" >> ${hostsFile}`], {
stdio: 'inherit',
});
console.log(`Added ${name} to ${hostsFile}.`);
} else {
console.log(`${name} already exists in ${hostsFile}.`);
}
}
/**
* Run the development server on the specified host and port.
*/
function runDevServer(host, port, ssl, sslCert, sslKey, logLevel, baseUrl, basicAuth) {
console.log('Running the development server...');
if (baseUrl === `http://${host}:${port}`) {
console.log('Using the mock service worker for API calls.');
basicAuth = null;
} else if (baseUrl.startsWith('https://')) {
console.log(`Using ${baseUrl} as backend for API calls via proxy.`);
const [baseName, baseIp] = getNameAndAddress(baseUrl);
addHostEntry(baseName, baseIp);
console.log(`Your host computer should resolve ${baseName} to ${host}.`);
port = 443;
ssl = true;
} else {
console.error(`Invalid base URL: ${baseUrl}`);
process.exit(1);
}
// export settings used in the proxy config
process.env.data_portal_base_url = baseUrl;
if (basicAuth) {
process.env.data_portal_basic_auth = basicAuth;
} else {
delete process.env.data_portal_basic_auth;
}
const params = ['start', '--', '--host', host, '--port', port];
if (ssl) {
params.push('--ssl', '--ssl-cert', sslCert, '--ssl-key', sslKey);
}
if (logLevel == 'debug') {
params.push('--verbose');
}
const result = spawnSync('npm', params, {
stdio: 'inherit',
});
if (result.error) {
console.error(result.error.message);
process.exit(1);
}
process.exit(result.status);
}
/**
* Run the production server on the specified host and port.
*
* It is assumed that the application has already been built
* and that the "serve" package is installed globally.
*/
function runProdServer(host, port, ssl, sslCert, sslKey, logLevel) {
console.log('Running the production server...');
const distDir = getBrowserDir(path.join(__dirname, 'dist'));
process.chdir(distDir);
const params = [
'-a',
host,
'-p',
port,
'-g',
logLevel.toLowerCase(),
'-d',
'.',
'--page-fallback',
];
if (ssl) {
params.push('--http2', '--http2-tls-cert', sslCert, '--http2-tls-key', sslKey);
}
params.push('./index.html');
const result = spawnSync('static-web-server', params, {
stdio: 'inherit',
});
if (result.error) {
console.error(result.error.message);
process.exit(1);
}
process.exit(result.status);
}
/**
* Main entry point.
*/
function main() {
process.chdir(__dirname);
const settings = readSettings();
console.log('Runtime settings:');
console.table(settings);
const {
host,
port,
ssl,
ssl_cert,
ssl_key,
log_level: logLevel,
base_url: baseUrl,
basic_auth: basicAuth,
} = settings;
if (!host || !port) {
console.error('Host and port must be specified');
process.exit(1);
}
if (!baseUrl) {
console.error('The base URL must be specified');
process.exit(1);
}
delete settings.host;
delete settings.port;
delete settings.ssl;
delete settings.ssl_cert;
delete settings.ssl_key;
delete settings.log_level;
delete settings.basic_auth;
writeSettings(settings);
(DEV_MODE ? runDevServer : runProdServer)(
host,
port,
ssl,
ssl_cert,
ssl_key,
logLevel,
baseUrl,
basicAuth,
);
}
main();