This repository has been archived by the owner on Mar 15, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
get-stellar.js
71 lines (58 loc) · 1.87 KB
/
get-stellar.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
const fs = require("fs");
const https = require("https");
const unzipper = require("unzipper");
const directory = "./.stellar";
const downloadStellar = async () => {
return new Promise((resolve, reject) => {
if (!fs.existsSync(directory)) fs.mkdirSync(directory);
const output = directory + "/html5up-stellar.zip";
const file = fs.createWriteStream(output);
const request = https.get("https://html5up.net/stellar/download", (response) => {
if (response.statusCode !== 200) {
reject(
new Error(
`Failed to download Stellar. Try manually downloading from https://html5up.net/stellar. Place the html5up-stellar.zip in ./.stellar' (${response.statusCode})`
)
);
return;
}
response.pipe(file);
});
// The destination stream is ended by the time it's called
file.on("finish", () => resolve(output));
request.on("error", (err) => {
fs.unlink(output, () => reject(err));
});
file.on("error", (err) => {
fs.unlink(output, () => reject(err));
});
request.end();
});
};
const unzipStellar = (stellar) => {
return new Promise((resolve, reject) => {
const stellarZip = fs.createReadStream(stellar);
stellarZip.pipe(unzipper.Extract({ path: directory }));
stellarZip.on("finish", () => resolve(output));
stellarZip.on("error", (err) => {
fs.unlink(stellar, () => reject(err));
});
});
};
const handleError = (error) => {
if (error instanceof Error) {
console.error(error);
process.exit(-1);
}
};
const init = async () => {
console.log("Downloading Stellar. Please Wait...");
const stellar = await downloadStellar();
handleError(stellar);
console.log(`Stellar downloaded to ${stellar}`);
const unzip = await unzipStellar(stellar);
handleError(unzip);
console.log(`Stellar unzipped to ${stellar}`);
process.exit();
};
init();