Skip to content

Commit

Permalink
add installer
Browse files Browse the repository at this point in the history
  • Loading branch information
femshima committed Oct 19, 2023
1 parent 615b8ef commit 5cacfb3
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,13 @@ jobs:
echo "Prerelease"
echo "prerelease=true" >> $GITHUB_OUTPUT
fi
- name: Create node package
run: tar czvf node.tar.gz install.mjs index.js index.d.ts package.json
- name: Create release
uses: "marvinpinto/action-automatic-releases@latest"
with:
repo_token: "${{ secrets.GITHUB_TOKEN }}"
prerelease: ${{ steps.check-tag.outputs.prerelease == 'true' }}
files: |
**/*.node
node.tar.gz
60 changes: 60 additions & 0 deletions install.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//@ts-check

import fs from "node:fs/promises"
import path from "node:path";

// If this program is running on node-altjtalk-binding repo,
// do not download prebuilt binary
const isRepo = await fs.access("build.rs").then(() => true, () => false);
if (isRepo) process.exit(0);

async function isMusl() {
// For Node 10
if (!process.report || typeof process.report.getReport !== 'function') {
try {
const lddPath = require('child_process').execSync('which ldd').toString().trim()
return (await fs.readFile(lddPath, 'utf8')).includes('musl')
} catch (e) {
return true
}
} else {
const report = process.report.getReport();
const { glibcVersionRuntime } = (typeof report === "object" ? report : JSON.parse(report)).header
return !glibcVersionRuntime
}
}

const baseUrl = "https://github.com/femshima/node-altjtalk-binding/releases/download";

const file = new URL(import.meta.url).pathname;
const dir = path.dirname(file);

const packageJSON = JSON.parse(
await fs.readFile(path.join(dir, "package.json"), { encoding: "utf-8" })
);
const packageName = packageJSON.name ?? "node-altjtalk-binding";
const version = `v${packageJSON.version}` ?? "latest";

const { platform, arch } = process;

let suffix = "";
switch (platform) {
case 'win32':
suffix = "-msvc";
break;
case 'linux':
suffix = (await isMusl()) ? "-musl" : "-gnu";
break;
}

const fileName = `${packageName}.${platform}-${arch}${suffix}.node`

const url = `${baseUrl}/${version}/${fileName}`

const response = await fetch(url);
if (!response.ok) {
throw new Error(`Fetch failed.\nRequest to ${url} returned status code ${response.status}.`);
}

const buf = await response.arrayBuffer();
fs.writeFile(fileName, Buffer.from(buf));
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
"prepublishOnly": "napi prepublish -t npm",
"test": "ava",
"universal": "napi universal",
"version": "napi version"
"version": "napi version",
"postinstall": "node install.mjs"
},
"packageManager": "yarn@3.6.4"
}

0 comments on commit 5cacfb3

Please sign in to comment.