-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.ts
85 lines (63 loc) · 2.04 KB
/
index.ts
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
'use strict';
import fs from 'fs';
import util from 'util';
import fetch from 'node-fetch';
import { connectorsToHostFile } from './utils';
const mkdir = util.promisify(fs.mkdir);
const writeFile = util.promisify(fs.writeFile);
const removeFile = util.promisify(fs.unlink);
const owner = 'web-scrobbler';
const repo = 'web-scrobbler';
const rawContentUrl = `https://raw.githubusercontent.com/${owner}/${repo}`;
const resDir = 'resources';
const moduleFile = 'connectors.ts';
const listFile = `${resDir}/connectors.json`;
const hostFile = `${resDir}/hostfile.txt`;
async function main(args: string[]) {
const latestTag = args.at(-1);
if (!latestTag) {
console.error('You must provide version as an argument');
process.exit(1);
}
let exitCode = 0;
try {
await downloadModule(latestTag);
if (!fs.existsSync(resDir)) {
mkdir(resDir);
}
await dumpConnectors();
console.log(`Dumped connectors from ${latestTag} release.`);
await dumpHostfile();
console.log(`Dumped hostfile from ${latestTag} release.`);
await removeFile(moduleFile);
} catch (e) {
console.error(`Unable to dump connectors from ${latestTag} release.`);
console.log(e);
exitCode = 1;
}
process.exit(exitCode);
}
async function downloadModule(tagName) {
const moduleUrl = getModuleUrl(tagName);
const response = await fetch(moduleUrl);
if (!response.ok) {
throw new Error('Cannot download module');
}
const moduleCode = await response.text();
await writeFile(moduleFile, moduleCode);
}
async function dumpConnectors() {
const connectors = (await import(`./${moduleFile}`)).default as any[];
const labelArray = connectors.map((entry) => entry.label);
const contents = JSON.stringify(labelArray, null, 2) + '\n';
await writeFile(listFile, contents);
}
async function dumpHostfile() {
const connectors = (await import(`./${moduleFile}`)).default as any[];
const contents = connectorsToHostFile(connectors);
await writeFile(hostFile, contents);
}
function getModuleUrl(tagName) {
return `${rawContentUrl}/${tagName}/src/core/connectors.ts`;
}
main(process.argv);