From a4414cd6c0db356e2c271644c18891add61e342a Mon Sep 17 00:00:00 2001 From: Mathis Pinsault Date: Tue, 21 Oct 2025 22:45:11 +0200 Subject: [PATCH 1/3] change: setup binary script Signed-off-by: Mathis Pinsault --- package.json | 4 +- scripts/setup-binary.js | 90 ----------------------------------------- scripts/setup-binary.sh | 57 ++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 92 deletions(-) delete mode 100644 scripts/setup-binary.js create mode 100755 scripts/setup-binary.sh diff --git a/package.json b/package.json index 52487c4..03217b1 100644 --- a/package.json +++ b/package.json @@ -17,14 +17,14 @@ }, "files": [ "bin/moonx", - "scripts/setup-binary.js" + "scripts/setup-binary.sh" ], "scripts": { "build": "bun build --compile src/index.ts --outfile=\"bin/moonx-dev\"", "changeset": "changeset", "format": "prettier --check . --config ./configs/.prettierrc.json", "format:fix": "prettier --write . --config ./configs/.prettierrc.json", - "postinstall": "node scripts/setup-binary.js", + "postinstall": "scripts/setup-binary.sh", "lint": "pnpm run lint:knip && pnpm run lint:oxlint", "lint:fix": "pnpm run lint:knip:fix && pnpm run lint:oxlint:fix", "lint:knip": "knip --config ./configs/knip.json", diff --git a/scripts/setup-binary.js b/scripts/setup-binary.js deleted file mode 100644 index ac0b328..0000000 --- a/scripts/setup-binary.js +++ /dev/null @@ -1,90 +0,0 @@ -import { - chmodSync, - createWriteStream, - existsSync, - mkdirSync, - readFileSync, - renameSync, -} from "node:fs"; -import { get } from "node:https"; -import { join, dirname } from "node:path"; -import { pipeline } from "node:stream/promises"; -import { fileURLToPath } from "node:url"; - -const { platform, arch } = process; - -console.log(process.env.INIT_CWD, process.cwd()); - -// Skip installation in development workspace -// INIT_CWD is the directory where npm install was initiated -// If it differs from cwd, we're in a workspace install -if (process.env.INIT_CWD && process.env.INIT_CWD === process.cwd()) { - console.log("Skipping binary download in workspace install"); - process.exit(0); -} - -// Get package version from package.json -const __dirname = dirname(fileURLToPath(import.meta.url)); -const packageJsonPath = join(__dirname, "..", "package.json"); -const packageJson = JSON.parse(readFileSync(packageJsonPath, "utf-8")); -const version = packageJson.version; - -const binaryName = `${platform}-${arch}`; -const src = join(process.cwd(), "bin", binaryName, binaryName); -const dest = join(process.cwd(), "bin", "moonx"); - -// If binary already exists locally (from build), use it -if (existsSync(src)) { - renameSync(src, dest); - chmodSync(dest, 0o755); - console.log(`Binary installed successfully from local build`); - process.exit(0); -} - -// Download from GitHub releases -console.log(`Downloading binary for ${platform}-${arch}...`); - -const downloadUrl = `https://github.com/maastrich/moonx/releases/download/v${version}/${binaryName}`; - -async function downloadBinary(url, destPath) { - return new Promise((resolve, reject) => { - get(url, (response) => { - // Handle redirects - if (response.statusCode === 301 || response.statusCode === 302) { - downloadBinary(response.headers.location, destPath) - .then(resolve) - .catch(reject); - return; - } - - if (response.statusCode !== 200) { - reject( - new Error(`Failed to download binary: HTTP ${response.statusCode}`) - ); - return; - } - - // Ensure directory exists - mkdirSync(dirname(destPath), { recursive: true }); - - const fileStream = createWriteStream(destPath); - pipeline(response, fileStream) - .then(() => { - chmodSync(destPath, 0o755); - resolve(); - }) - .catch(reject); - }).on("error", reject); - }); -} - -try { - await downloadBinary(downloadUrl, dest); - console.log(`Binary downloaded and installed successfully`); -} catch (error) { - console.error(`Failed to download binary: ${error.message}`); - console.error(`Tried to download from: ${downloadUrl}`); - console.error(`Please ensure the release v${version} exists on GitHub`); - console.error(`\tsee: https://github.com/maastrich/moonx/releases`); - process.exit(1); -} diff --git a/scripts/setup-binary.sh b/scripts/setup-binary.sh new file mode 100755 index 0000000..ce85110 --- /dev/null +++ b/scripts/setup-binary.sh @@ -0,0 +1,57 @@ +#!/usr/bin/env bash + +set -e + +get_target() { + local platform=$(uname -s | tr '[:upper:]' '[:lower:]') + local arch=$(uname -m) + + # Normalize architecture + case "$arch" in + x86_64) arch="x64" ;; + aarch64) arch="arm64" ;; + esac + + # Normalize platform + case "$platform" in + mingw* | msys* | cygwin*) platform="win32" ;; + esac + + echo "${platform}-${arch}" +} + +get_version() { + local script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" + sed -n 's/.*"version"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' "$script_dir/../package.json" | head -n 1 +} + +download_binary() { + local target="$1" + local version="$2" + local url="https://github.com/maastrich/moonx/releases/download/v${version}/${target}" + local dest="$(pwd)/bin/moonx" + + mkdir -p "$(dirname "$dest")" + + if curl -L -f -o "$dest" "$url"; then + chmod 755 "$dest" + echo "Binary downloaded and installed successfully" + else + echo "Failed to download binary from: $url" >&2 + echo "See: https://github.com/maastrich/moonx/releases" >&2 + exit 1 + fi +} + +# Skip installation in development workspace +if [ -n "$INIT_CWD" ] && [ "$INIT_CWD" = "$(pwd)" ]; then + echo "Skipping binary download in workspace install" + exit 0 +fi + +# Main +TARGET=$(get_target) +VERSION=$(get_version) + +echo "Downloading binary for ${TARGET} v${VERSION}..." +download_binary "$TARGET" "$VERSION" From 30daabd7d7b8da8186357cab6842cf8ea74dd2b4 Mon Sep 17 00:00:00 2001 From: Mathis Pinsault Date: Tue, 21 Oct 2025 22:46:47 +0200 Subject: [PATCH 2/3] add changeset Signed-off-by: Mathis Pinsault --- .changeset/strong-towns-punch.md | 5 +++++ .changeset/tired-planets-love.md | 5 +++++ 2 files changed, 10 insertions(+) create mode 100644 .changeset/strong-towns-punch.md create mode 100644 .changeset/tired-planets-love.md diff --git a/.changeset/strong-towns-punch.md b/.changeset/strong-towns-punch.md new file mode 100644 index 0000000..2f82e73 --- /dev/null +++ b/.changeset/strong-towns-punch.md @@ -0,0 +1,5 @@ +--- +"@maastrich/moonx": patch +--- + +Make setup script a shell script diff --git a/.changeset/tired-planets-love.md b/.changeset/tired-planets-love.md new file mode 100644 index 0000000..b754907 --- /dev/null +++ b/.changeset/tired-planets-love.md @@ -0,0 +1,5 @@ +--- +"@maastrich/moonx": patch +--- + +Update setup script From 080fe0ec80e116bd2ec9029c6d1dd40b1b91610e Mon Sep 17 00:00:00 2001 From: Mathis Pinsault Date: Tue, 21 Oct 2025 22:48:32 +0200 Subject: [PATCH 3/3] update knip Signed-off-by: Mathis Pinsault --- configs/knip.json | 1 + 1 file changed, 1 insertion(+) diff --git a/configs/knip.json b/configs/knip.json index 9d22e91..50bf6a8 100644 --- a/configs/knip.json +++ b/configs/knip.json @@ -1,5 +1,6 @@ { "$schema": "https://unpkg.com/knip@5/schema.json", + "ignoreBinaries": ["scripts/setup-binary.sh"], "prettier": { "config": "./configs/.prettierrc.json" }