forked from slsfi/digital-edition-frontend-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prebuild-common-fns.js
127 lines (115 loc) · 3.66 KB
/
prebuild-common-fns.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
const fs = require('fs');
const path = require('path');
/**
* Get the config object used by the app.
* @param configFilepath File path to config.ts where the config object is defined.
* @returns Object containing the config.
*/
function getConfig(configFilepath) {
// Join the relative file path with the current working directory to create an absolute path
const absoluteConfigFilepath = path.join(__dirname, configFilepath);
let config = null;
try {
// Read the contents of the config.ts file
const configTS = fs.readFileSync(absoluteConfigFilepath, 'utf-8');
// Extract the config object from the string
let configContent = configTS.split('export const config')[1] || '';
if (!configContent) {
console.error('Unable to read config.ts file.');
return null;
}
const c_start = configContent.indexOf('{');
const c_end = configContent.lastIndexOf('}') + 1;
configContent = 'module.exports = ' + configContent.slice(c_start, c_end);
// Write the config object to a temporary file
fs.writeFileSync(path.join(__dirname, 'src/tmp_config.js'), configContent);
// Import the config object from the temporary file as javascript
config = require(path.join(__dirname, 'src/tmp_config.js'));
} catch (err) {
console.error(err);
return null;
}
try {
// Delete the temporary config file
fs.unlinkSync(path.join(__dirname, 'src/tmp_config.js'));
} catch (e) {
console.error(e);
}
return config;
}
async function fetchFromAPI(endpoint) {
const res = await fetch(endpoint);
if (res.ok) {
return await res.json();
} else {
console.error(res);
return null;
}
}
/**
* Given an object with nested objects in the property 'branchingKey',
* returns a flattened array of the object. If 'requiredKey' is not
* undefined, only objects that have a non-empty 'requiredKey' property
* are included.
*/
function flattenObjectTree(data, branchingKey = 'children', requiredKey = undefined) {
const dataWithoutChildren = (({ [branchingKey]: _, ...d }) => d)(data);
let list = [];
if (!requiredKey || (requiredKey && data[requiredKey])) {
list = [dataWithoutChildren];
}
if (!data[branchingKey] && (!requiredKey || (requiredKey && data[requiredKey]))) {
return list;
}
if (data[branchingKey] && data[branchingKey].length) {
for (const child of data[branchingKey]) {
list = list.concat(flattenObjectTree(child, branchingKey, requiredKey));
}
}
return list;
}
/**
* Get the translation of the phrase with the given id in the given locale.
* The translation is fetched from the messages.<locale>.xlf files in the
* given folder.
* @returns The translation as a string or 'link' if unable to find the id
* in the translation file.
*/
function getTranslation(folderPath, locale, id) {
if (!id) {
return 'link';
}
// Join the relative file path with the current working directory to create an absolute path
const absoluteFilepath = path.join(__dirname, folderPath + 'messages.' + locale + '.xlf');
try {
// Read the contents of the .xlf file
const xlf = fs.readFileSync(absoluteFilepath, 'utf-8');
let start = xlf.indexOf('<unit id="' + id + '">');
if (start < 0) {
return 'link';
}
start = xlf.indexOf('<target>', start);
if (start < 0) {
return 'link';
} else {
start += 8;
}
const end = xlf.indexOf('</target>', start);
if (end > -1) {
return xlf.slice(start, end);
}
return 'link';
} catch (err) {
console.error(err);
return 'link';
}
}
/**
* Export all functions in this file as a CommonJS module.
*/
module.exports = {
getConfig,
fetchFromAPI,
flattenObjectTree,
getTranslation
};