-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathbuild.js
77 lines (62 loc) · 2.5 KB
/
build.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
import { createWriteStream, readFileSync } from 'fs';
import { promisify } from 'node:util';
import stream from 'node:stream';
import fs from 'node:fs';
import got from 'got';
import glob from 'glob';
import { keyboardDefinitionV2ToVIADefinitionV2 } from 'via-reader';
import { exit } from 'process';
const viaUrl = "https://www.caniusevia.com/keyboards.v2.json";
async function getFile(url, out){
const pipeline = promisify(stream.pipeline);
await pipeline(
got.stream(url),
fs.createWriteStream(out)
);
}
async function main(){
// Download VIA official keyboards JSON
await getFile(viaUrl, './via_official_keyboards.json');
// Load official data
let data = JSON.parse(readFileSync('./via_official_keyboards.json').toString());
let defs = data.definitions;
let via_vidpids = Object.keys(defs);
let mia_vidpids = [];
console.log(`${via_vidpids.length} keyboards found in the official VIA database.`);
// Load ViaMia keyboards
let errors = 0;
let jsons = glob.sync('./keyboards/**/*.json');
for (const keyboard of jsons) {
// Load JSON
console.log(`Processing ${keyboard}`);
let keyboard_data = JSON.parse(fs.readFileSync(keyboard).toString());
// Convert to VIA format
let via_data = keyboardDefinitionV2ToVIADefinitionV2(keyboard_data);
let keyboard_vidpid = via_data.vendorProductId;
// Check if vidpid is unique
if(via_vidpids.includes(keyboard_vidpid.toString())){
console.log(`🛑 Keyboard file ${keyboard} matches official VIA keyboard (${defs[keyboard_vidpid].name})`);
errors++;
}else if(mia_vidpids.includes(keyboard_vidpid.toString())){
console.log(`🛑 Keyboard file ${keyboard} matches another ViaMia keyboard (${defs[keyboard_vidpid].name})`);
errors++;
}else{
// Append to keyboards & mia_vidpid list (so ViaMia duplicates are also detected)
mia_vidpids.push(keyboard_vidpid.toString());
data.definitions[keyboard_vidpid] = via_data;
}
}
// Set date
data.generatedAt = + new Date();
console.log(`Finished. Added ${mia_vidpids.length} keyboards.`);
// Write final JSON
fs.writeFileSync('keyboards.v2.json', JSON.stringify(data));
// Clean up
fs.unlinkSync('via_official_keyboards.json');
// Throw errors
if(errors > 0){
console.error(`ERROR: ${errors} keyboard configs had duplicate vidpid combinations.`);
exit(1);
}
}
main();