-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: node-based build script (#131)
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
Showing
5 changed files
with
298 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
target | ||
.env | ||
build | ||
node_modules | ||
|
||
# Ignore IDE workspace files | ||
.idea | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 <[email protected]>", | ||
"license": "BSD-3-Clause", | ||
"dependencies": { | ||
"axios": "=1.6.7", | ||
"dotenv": "=16.4.5", | ||
"tar": "=6.2.0" | ||
} | ||
} |