-
Notifications
You must be signed in to change notification settings - Fork 28
/
oryx-downloader.ts
61 lines (56 loc) · 1.91 KB
/
oryx-downloader.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
import axios from "axios";
import { existsSync } from "fs";
import { join } from "path";
import unzipper from "unzipper";
const ORYX_GRAPHQL_URL = "https://oryx.zsa.io/graphql";
export async function getKeymapSourceLink(hashId, revisionId = "latest") {
const query = `
query getLayout($hashId: String!, $revisionId: String!, $geometry: String) {
layout(hashId: $hashId, geometry: $geometry, revisionId: $revisionId) {
revision {
zipUrl
}
}
}`;
const { data } = await axios.post(ORYX_GRAPHQL_URL, {
operationName: "getLayout",
variables: {
hashId,
geometry: "moonlander",
revisionId,
},
query,
});
return data.data.layout.revision.zipUrl;
}
export async function unzipKeymapSource(url, path) {
const response = await axios({
method: "get",
url,
responseType: "stream",
});
const unzip = unzipper.Extract({ path });
return response.data.pipe(unzip).promise();
}
export async function downloadKeymapSource(layoutHashId, path) {
const zipUrl = await getKeymapSourceLink(layoutHashId);
await unzipKeymapSource(zipUrl, path);
console.log(
`Downloaded layout "${layoutHashId}" to ${path} (from ${zipUrl}).`,
);
}
if (typeof require !== "undefined" && require.main === module) {
const hashId = process.argv[2] || process.env.LAYOUT_ID;
const layoutFolder = process.argv[3] || process.env.LAYOUT_FOLDER;
const parentFolder =
process.argv[4] || process.env.LAYOUT_SRC || "./layout_src";
downloadKeymapSource(hashId, parentFolder).then(() => {
if (!existsSync(join(parentFolder, layoutFolder))) {
console.warn(
`WARNING: Please check your environment: configured LAYOUT_FOLDER was not found: ${parentFolder}/${layoutFolder}`,
);
} else {
console.log("Unpacked folder:", layoutFolder);
}
});
}