forked from tari-project/rfcs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
executable file
·60 lines (55 loc) · 2.13 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
#!/usr/bin/env node
const axios = require('axios');
const fs = require('fs');
const fsp = fs.promises;
const crypto = require('crypto');
const { exec } = require('child_process');
const tar = require('tar');
const VERSION = process.env.MDBOOK_VERSION || '0.4.37'; // replace with the version you want
const MDBOOK_HASH = process.env.MDBOOK_HASH || "93f9a98032be3f4b7b4bab42fdc97d58b5d69d81eef910a5c8fa68e03fbf8a8d";
const url = `https://github.com/rust-lang/mdBook/releases/download/v${VERSION}/mdbook-v${VERSION}-x86_64-unknown-linux-gnu.tar.gz`;
async function downloadFile(url, path) {
const writer = fs.createWriteStream(path);
const response = await axios({ url, method: 'GET', responseType: 'stream' });
response.data.pipe(writer);
return new Promise((resolve, reject) => {
writer.on('finish', resolve);
writer.on('error', reject);
});
}
function calculateHash(path) {
return new Promise((resolve, reject) => {
const hash = crypto.createHash('sha256');
const stream = fs.createReadStream(path);
stream.on('data', (data) => hash.update(data));
stream.on('end', () => resolve(hash.digest('hex')));
stream.on('error', reject);
});
}
async function main() {
const buildPath = './build';
try {
await fsp.mkdir(buildPath, { recursive: true })
} catch (e) {
console.error(`👎️ Could not create build directory ${e}`);
process.exit(2);
}
const zipLoc = `${buildPath}/mdbook.tar.gz`;
await downloadFile(url, zipLoc);
const hash = await calculateHash(zipLoc);
if (hash !== MDBOOK_HASH) {
console.error(`👎️ Hash mismatch. expected ${MDBOOK_HASH} but got ${hash}`);
process.exit(1);
}
console.log(`👍️ MDBook release hash matches ${hash}`);
await tar.x({ file: zipLoc, C: buildPath });
exec(`${buildPath}/mdbook build -d ./book`, (error, stdout, stderr) => {
if (error) {
console.error(`👎️ exec error: ${error}`);
return;
}
console.log(`stdout: ${stdout}`);
console.error(`stderr: ${stderr}`);
});
}
main().catch(console.error);