-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostinstall.js
64 lines (56 loc) · 1.66 KB
/
postinstall.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
// @ts-check
import { existsSync } from "fs";
import { ensureDir } from "fs-extra";
import { homedir } from "os";
import path from "path";
import { extract } from "tar";
import XzDecompress from "xz-decompress";
const { XzReadableStream } = XzDecompress;
const DATA_VERSION = "v0.0.6-alpha";
const APP_DIR = path.join(homedir(), `.tdb`);
const DATA_DIR = path.join(APP_DIR, "data");
async function ensureAppDirectories() {
await ensureDir(APP_DIR);
await ensureDir(DATA_DIR);
}
async function downloadBinaryFromGitHub() {
try {
const tarballDownloadBuffer = await fetch(
`https://github.com/nebulatgs/tdb/releases/download/${DATA_VERSION}/data.txz`
)
.then((response) => new Response(new XzReadableStream(response.body)))
.then((response) => response.arrayBuffer());
const tarballBuffer = Buffer.from(tarballDownloadBuffer);
const unpack = extract({
cwd: APP_DIR,
sync: false,
});
/** @type {Promise<void>} */
const unpackPromise = new Promise((resolve) => {
unpack.write(tarballBuffer);
unpack.end();
unpack.once("end", () => {
console.log("Binary downloaded and unpacked successfully.");
resolve();
});
});
await unpackPromise;
} catch (error) {
console.error("Error downloading binary from GitHub:", error);
}
}
function isDataDirDownloaded() {
try {
return existsSync(DATA_DIR);
} catch (e) {
return false;
}
}
// Skip downloading the binary if it was already installed via optionalDependencies
if (!isDataDirDownloaded()) {
console.log("Data directory not found. Will download from GitHub.");
await ensureAppDirectories();
downloadBinaryFromGitHub();
} else {
console.log("Data directory already installed.");
}