-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathfetch-domain-data.ts
More file actions
56 lines (50 loc) · 1.55 KB
/
fetch-domain-data.ts
File metadata and controls
56 lines (50 loc) · 1.55 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
import fs from 'fs';
import { replaceInFileSync } from 'replace-in-file';
export const fetchDomainData = (generateNewOptionFiles = true) => {
return {
name: 'pull-domain-data',
transform(code, id, options) {
if (id.includes('src/routes/en/docs') && id.endsWith('options.ts')) {
if (options?.ssr) {
if (id.includes('dwd-api')) {
const basePath = id.replace('options.ts', '');
const domainMetaData = JSON.parse(
fs.readFileSync(basePath + 'meta-data-poc.json', 'utf8')
);
fetch('https://openmeteo.s3.amazonaws.com/data/dwd_icon/static/meta.json')
.then((response) => response.json())
.then(function (response) {
console.log(response);
let newHourly = `export const hourly = [
[`;
for (const [index, variable] of domainMetaData.variables.entries()) {
newHourly += `
{ value: '${variable}', label: '${domainMetaData.labels[index]}' },`;
}
newHourly += `
]
];`;
const regex = /export const hourly = \[[\s\S]*?\];/;
if (generateNewOptionFiles) {
const replaceOptions = {
files: basePath + 'domains/dwd-icon.ts',
from: regex,
to: newHourly,
countMatches: true
};
try {
const results = replaceInFileSync(replaceOptions);
console.log('Replacement results:', results);
} catch (error) {
console.error('Error occurred:', error);
}
} else {
return code.replace(regex, newHourly);
}
});
}
}
}
}
};
};