Skip to content

Commit

Permalink
feat: node-based build script (#131)
Browse files Browse the repository at this point in the history
Add a node.js-based build script so that this site can run in Cloudflare
pages.

The script does the following:
1. Downloads the specified version of mdbook from the GitHub releases
page
2. Verifies its SHA256 hash
3. Extracts it to a temporary directory
4. Runs mdbook build against the book.toml file in the current directory
5. Places the built book in the temporary directory

`mdbook.env` (which is checked in and MUST NOT hold any secrets) allows
you to set the version of mdbook and its hash.
  • Loading branch information
CjS77 committed Mar 6, 2024
1 parent 584e997 commit ead2b4d
Show file tree
Hide file tree
Showing 5 changed files with 298 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
target
.env
build
node_modules

# Ignore IDE workspace files
.idea
Expand Down
61 changes: 61 additions & 0 deletions build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/usr/bin/env node
require('dotenv').config({ path: './mdbook.env'});
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;
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);
7 changes: 7 additions & 0 deletions mdbook.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#############################################################################
# DO NOT PLACE SECRETS IN THIS FILE #
# This file is checked into VCS. #
#############################################################################

MDBOOK_VERSION=0.4.37
MDBOOK_HASH=93f9a98032be3f4b7b4bab42fdc97d58b5d69d81eef910a5c8fa68e03fbf8a8d
212 changes: 212 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "rfcs",
"version": "1.0.0",
"description": "This repository contains requests for comment (RFCs) for Tari protocols.",
"main": "build.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Cayle Sharrock <CjS77@users.noreply.github.com>",
"license": "BSD-3-Clause",
"dependencies": {
"axios": "=1.6.7",
"dotenv": "=16.4.5",
"tar": "=6.2.0"
}
}

0 comments on commit ead2b4d

Please sign in to comment.