Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 24 additions & 10 deletions .github/workflows/publish-npm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ on:
required: true
type: string

env:
UV_VERSION: "0.11.6"

jobs:
publish:
name: Upload to npmjs registry
Expand All @@ -19,25 +22,36 @@ jobs:
env:
PLAN: ${{ inputs.plan }}
steps:
- name: Fetch npm packages
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
with:
persist-credentials: false
- name: Fetch binary archives
uses: actions/download-artifact@70fc10c6e5e1ce46ad2ea6f2b72d43f7d47b13c3 # v8.0.0
with:
pattern: artifacts-build-global
path: npm/
pattern: artifacts-*
path: npm-artifacts/
merge-multiple: true
- uses: actions/setup-node@53b83947a5a98c8d113130e565377fae1a50d02f # v6.3.0
with:
node-version: "24.x"
registry-url: "https://registry.npmjs.org"
- name: Update npm
run: npm install -g npm@latest
- run: |
for release in $(echo "$PLAN" | jq --compact-output '.releases[] | select([.artifacts[] | endswith("-npm-package.tar.gz")] | any)'); do
pkg=$(echo "$release" | jq '.artifacts[] | select(endswith("-npm-package.tar.gz"))' --raw-output)
prerelease=$(echo "$PLAN" | jq ".announcement_is_prerelease")
- name: "Install uv"
uses: astral-sh/setup-uv@cec208311dfd045dd5311c1add060b2062131d57 # v8.0.0
with:
version: ${{ env.UV_VERSION }}
- name: Build npm packages
run: |
printf '%s' "$PLAN" > plan-dist-manifest.json
uv run ./scripts/build-npm-packages.py --plan plan-dist-manifest.json --artifacts-dir ./npm-artifacts --out-dir ./npm/.output
- name: Publish npm packages
run: |
prerelease=$(echo "$PLAN" | jq --raw-output '.announcement_is_prerelease')
jq --raw-output '.publishOrder[]' ./npm/.output/manifest.json | while IFS= read -r pkg; do
if [ "$prerelease" = "true" ]; then
npm publish --tag beta --provenance --access public "./npm/${pkg}"
npm publish --tag beta --provenance --access public "$pkg"
else
npm publish --provenance --access public "./npm/${pkg}"
npm publish --provenance --access public "$pkg"
fi
done
5 changes: 5 additions & 0 deletions .github/workflows/publish-pypi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ on:
required: true
type: string

env:
UV_VERSION: "0.11.6"

jobs:
publish:
name: Upload to PyPI
Expand All @@ -19,6 +22,8 @@ jobs:
steps:
- name: "Install uv"
uses: astral-sh/setup-uv@cec208311dfd045dd5311c1add060b2062131d57 # v8.0.0
with:
version: ${{ env.UV_VERSION }}
- uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
with:
pattern: wheels-*
Expand Down
5 changes: 4 additions & 1 deletion .github/workflows/sync-identify.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ on:

permissions: {}

env:
UV_VERSION: "0.11.6"

jobs:
sync:
if: github.repository == 'j178/prek'
Expand All @@ -19,7 +22,7 @@ jobs:
persist-credentials: false
- uses: astral-sh/setup-uv@cec208311dfd045dd5311c1add060b2062131d57 # v8.0.0
with:
version: "latest"
version: ${{ env.UV_VERSION }}
enable-cache: true
- name: "Sync identify tags"
run: uv run --upgrade gen.py
Expand Down
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,7 @@ site/

# profiling flamegraphs
*.flamegraph.svg

# NPM packages
npm-artifacts/
npm/.output/
6 changes: 1 addition & 5 deletions dist-workspace.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,7 @@ github-attestations-phase = "announce"
# Whether to publish prereleases to package managers
publish-prereleases = true
# The installers to generate for each app
installers = ["shell", "powershell", "npm", "homebrew"]
# A namespace to use when publishing this package to the npm registry
npm-scope = "@j178"
# Whether to produce an npm lockfile
npm-shrinkwrap = false
installers = ["shell", "powershell", "homebrew"]
# Target platforms to build apps for (Rust target-triple syntax)
targets = [
"aarch64-apple-darwin",
Expand Down
234 changes: 234 additions & 0 deletions npm/bin/prek.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,234 @@
#!/usr/bin/env node

const fs = require("fs");
const path = require("path");
const { spawnSync } = require("child_process");
const { createRequire } = require("module");

const DEBUG = process.env.PREK_DEBUG === "1";
const PLATFORMS = require("../platforms.json");

function debug(message) {
if (DEBUG) {
console.error(`[prek-debug] ${message}`);
}
}

function isMusl() {
if (process.platform !== "linux") {
return false;
}

if (fs.existsSync("/etc/alpine-release")) {
debug("detected musl via /etc/alpine-release");
return true;
}

const report = process.report?.getReport?.();
if (report?.header?.glibcVersionRuntime) {
debug(`detected glibc ${report.header.glibcVersionRuntime}`);
return false;
}
if (report?.sharedObjects?.some((entry) => entry.includes("musl"))) {
debug("detected musl via process.report shared objects");
return true;
}

try {
const result = spawnSync("ldd", ["--version"], {
encoding: "utf8",
stdio: ["ignore", "pipe", "pipe"],
});
const output = `${result.stdout ?? ""}${result.stderr ?? ""}`.toLowerCase();
const musl = output.includes("musl");
debug(`detected ${musl ? "musl" : "glibc"} via ldd`);
return musl;
Comment thread
j178 marked this conversation as resolved.
Resolved
} catch {
debug("failed to run ldd, assuming glibc");
}

try {
for (const libDir of ["/lib", "/lib64"]) {
if (!fs.existsSync(libDir)) {
continue;
}

const files = fs.readdirSync(libDir);
if (
files.some(
(file) => file.startsWith("ld-musl-") || file.startsWith("libc.musl-"),
)
) {
debug(`detected musl via ${libDir}`);
return true;
}
}
} catch {
debug("failed to scan /lib and /lib64 for musl");
}

const ldLibraryPath = process.env.LD_LIBRARY_PATH ?? "";
if (ldLibraryPath.toLowerCase().includes("musl")) {
debug("detected musl via LD_LIBRARY_PATH");
return true;
}

return false;
}

function armVersion() {
const version = Number(process.config.variables.arm_version ?? 0);
return Number.isFinite(version) ? version : 0;
}

function matchesArmVersion(spec) {
const version = armVersion();
if (spec.armVersionMin != null && version < spec.armVersionMin) {
return false;
}
if (spec.armVersionMax != null && version > spec.armVersionMax) {
return false;
}
return true;
}

function currentLibc() {
if (process.platform !== "linux") {
return null;
}
return isMusl() ? "musl" : "glibc";
}

function pickPlatformPackage() {
const libc = currentLibc();
debug(
`selecting package for platform=${process.platform} arch=${process.arch}` +
`${libc ? ` libc=${libc}` : ""} armVersion=${armVersion()}`,
);

const candidates = PLATFORMS.filter((spec) => {
if (!spec.os.includes(process.platform)) {
return false;
}
if (!spec.cpu.includes(process.arch)) {
return false;
}
if (spec.libc && spec.libc !== libc) {
return false;
}
if (!matchesArmVersion(spec)) {
return false;
}
return true;
}).sort((left, right) => {
const leftSpecificity = left.armVersionMin ?? 0;
const rightSpecificity = right.armVersionMin ?? 0;
return rightSpecificity - leftSpecificity;
});

if (candidates.length === 0) {
const libcSuffix = libc ? ` (${libc})` : "";
throw new Error(
`Unsupported platform: ${process.platform} ${process.arch}${libcSuffix}`,
);
}

return candidates[0];
}

function resolvePackageJson(packageName) {
const specifier = `${packageName}/package.json`;
const attempts = [];

const resolvers = [
() => createRequire(__filename).resolve(specifier),
Comment thread
j178 marked this conversation as resolved.
Resolved
() =>
path.resolve(
__dirname,
"..",
"..",
packageName.split("/")[1],
"package.json",
),
];

for (const resolve of resolvers) {
try {
const packageJsonPath = resolve();
attempts.push(packageJsonPath);
debug(`resolved ${specifier} candidate: ${packageJsonPath}`);
if (fs.existsSync(packageJsonPath)) {
debug(`using ${specifier}: ${packageJsonPath}`);
return packageJsonPath;
}
debug(`candidate does not exist: ${packageJsonPath}`);
} catch (error) {
attempts.push(String(error.message ?? error));
debug(`failed to resolve ${specifier}: ${error.message ?? error}`);
}
}

debug(`resolution attempts for ${packageName}: ${attempts.join(" | ")}`);
throw new Error(
`Platform package ${packageName} is not installed. Reinstall @j178/prek for this platform.`,
);
}

function ensureExecutable(binaryPath) {
if (process.platform === "win32") {
return;
}

try {
fs.accessSync(binaryPath, fs.constants.X_OK);
} catch {
fs.chmodSync(binaryPath, 0o755);
}
}

function main() {
if (process.env.PREK_BINARY) {
const override = path.resolve(process.env.PREK_BINARY);
debug(`using PREK_BINARY override: ${override}`);
if (!fs.existsSync(override)) {
throw new Error(`PREK_BINARY does not exist: ${override}`);
}
ensureExecutable(override);
runBinary(override);
return;
}

const spec = pickPlatformPackage();
debug(`selected package ${spec.packageName}`);

const packageJsonPath = resolvePackageJson(spec.packageName);
const binaryPath = path.join(path.dirname(packageJsonPath), spec.binaryName);
debug(`selected binary ${binaryPath}`);
ensureExecutable(binaryPath);
runBinary(binaryPath);
}

function runBinary(binaryPath) {
debug(`running ${binaryPath} ${process.argv.slice(2).join(" ")}`);
const result = spawnSync(binaryPath, process.argv.slice(2), {
stdio: "inherit",
});

if (result.error) {
throw result.error;
}

if (result.signal) {
process.kill(process.pid, result.signal);
return;
}

process.exit(result.status ?? 1);
}

try {
main();
} catch (error) {
console.error(`prek: ${error.message}`);
process.exit(1);
}
Loading
Loading