Skip to content

Commit

Permalink
Add a compilation script
Browse files Browse the repository at this point in the history
  • Loading branch information
devraymondsh committed Apr 5, 2024
1 parent 2fb5555 commit d27b71f
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 1 deletion.
8 changes: 7 additions & 1 deletion .github/workflows/scheduled-builds.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,10 @@ jobs:
run: apt-get update && apt-get --yes upgrade

- name: Install building tools
run: apt-get install --yes python3 python3-pip gcc g++ make
run: apt-get install --yes git python3 python3-pip gcc g++ make ninja nodejs

- name: Clone the repo
run: git clone https://github.com/devraymondsh/libnode-distributable

- name: Run the script
run: node index.js
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node-*
*.tar.gz
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"editor.defaultFormatter": "esbenp.prettier-vscode"
}
83 changes: 83 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import os from "node:os";
import syncFs from "node:fs";
import fs from "node:fs/promises";
import { Readable } from "node:stream";
import { finished } from "node:stream/promises";
import { spawnSync, spawn } from "node:child_process";

const constructSourceTarballName = (version) => `node-${version}.tar.gz`;
const constructSourceTarballDirName = (version) => `node-${version}`;

const constructSourceDownloadLink = (version) =>
`https://github.com/nodejs/node/archive/refs/tags/v${version}.tar.gz`;

const removeTheVCharacter = (str) => str.replace("v", "");

const nodeIndexUrl = "https://nodejs.org/dist/index.json";
const getLatestNodeVersion = async () => {
const res = await fetch(nodeIndexUrl);
const jsonData = await res.json();

return removeTheVCharacter(jsonData[0]["version"]);
};

const getLatestPublishedVersion = async () =>
removeTheVCharacter(await fs.readFile("version.txt", { encoding: "utf8" }));

const isANewerVersion = (oldVer, newVer) => {
const oldParts = oldVer.split(".");
const newParts = newVer.split(".");

for (var i = 0; i < newParts.length; i++) {
const a = ~~newParts[i]; // parse int
const b = ~~oldParts[i]; // parse int

if (a > b) return true;
if (a < b) return false;
}

return false;
};

const spawnAsync = (program, args, cwd) =>
new Promise((resolve, reject) => {
const child = spawn(program, args, { cwd });

child.stdout.on("data", (chunk) => console.log(chunk.toString()));
child.stderr.on("data", (chunk) => {
console.error(chunk.toString());
reject(chunk);
});

child.on("close", (code) => resolve(code));
});

const latestNodeVersion = await getLatestNodeVersion();

if (!isANewerVersion(await getLatestPublishedVersion(), latestNodeVersion)) {
console.log("Nothing to do!");
process.exit(0);
}

const tarballName = constructSourceTarballName(latestNodeVersion);
if (!syncFs.existsSync(tarballName)) {
const stream = syncFs.createWriteStream(tarballName);
const { body } = await fetch(constructSourceDownloadLink(latestNodeVersion));
await finished(Readable.fromWeb(body).pipe(stream));
}

const tarballDir = constructSourceTarballDirName(latestNodeVersion);
if (!syncFs.existsSync(tarballDir)) {
const { stderr } = spawnSync("tar", ["-xvf", `${tarballName}`]);
if (stderr.length > 0) {
console.error(stderr);
process.exit(0);
}
}

await spawnAsync("./configure", ["--ninja", "--shared", "--debug"], tarballDir);

const coreCount = os.cpus().length;
const threadCount = coreCount * 2;

await spawnAsync("make", [`-j${threadCount}`], tarballDir);
12 changes: 12 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "libnode-distributable",
"type": "module",
"description": "",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
1 change: 1 addition & 0 deletions version.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v21.7.1

0 comments on commit d27b71f

Please sign in to comment.