Skip to content

Rewrite your app in Rust, piece by piece, checked against the original at every step. Works with TypeScript, JavaScript, Python, Go, Ruby, Java, C, PHP, C#, Kotlin, PowerShell and Swift.

npm: ship each platform's binary as its own optional package (#1368)

erKamf490edac49ed
diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml
index 1c25807e..2d576293 100644
--- a/.github/workflows/release.yml
+++ b/.github/workflows/release.yml
@@ -145,12 +145,15 @@ jobs:
- name: Publish to npm (trusted publishing)
run: |
version="${GITHUB_REF_NAME#v}"
- dir=$(node scripts/npm-packages.mjs "$version" npm-bin npm-out)
- if npm view "rust-anything@$version" version > /dev/null 2>&1; then
- echo "rust-anything@$version is already on npm"
- else
- npm publish "$dir" --access public
- fi
+ dirs=$(node scripts/npm-packages.mjs "$version" npm-bin npm-out) || exit 1
+ for dir in $dirs; do
+ name=$(node -p "require('$dir/package.json').name")
+ if npm view "$name@$version" version > /dev/null 2>&1; then
+ echo "$name@$version is already on npm"
+ else
+ npm publish "$dir" --access public
+ fi
+ done
crates:
needs: build
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 689e7c6c..7e66b8b2 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,11 @@
## Unreleased
+- npm: `rust-anything` no longer bundles the binaries of all five platforms (210 MB unpacked). Each
+ platform's binary is its own package (`rust-anything-darwin-arm64`, `-darwin-x64`, `-linux-arm64`,
+ `-linux-x64`, `-win32-x64`), listed as an optional dependency, so `npm install` and `npx` fetch only
+ the one for the machine (about 35 MB unpacked, 9 MB download). The `rust-anything` command is
+ unchanged; when the platform package is missing (installed with `--omit=optional`) it says so.
- Install with Homebrew (`brew install erkamyaman/tap/rust-anything`), Scoop (the
`erkamyaman/scoop-bucket` bucket), a `.deb` for x86_64 and arm64 Linux attached to each release,
or Nix (`nix run github:erkamyaman/rust-anything`). The release workflow builds the `.deb` files
diff --git a/npm/rust-anything/bin/rust-anything.js b/npm/rust-anything/bin/rust-anything.js
index 03bd9cc0..a70487c5 100644
--- a/npm/rust-anything/bin/rust-anything.js
+++ b/npm/rust-anything/bin/rust-anything.js
@@ -8,11 +8,27 @@ const musl =
process.platform === 'linux' && !(process.report?.getReport()?.header?.glibcVersionRuntime)
const key = `${process.platform}-${process.arch}${musl ? '-musl' : ''}`
const exe = process.platform === 'win32' ? 'rust-anything.exe' : 'rust-anything'
-const bin = path.join(__dirname, '..', 'binaries', key, exe)
-if (!fs.existsSync(bin)) {
+
+function find() {
+ try {
+ const dir = path.dirname(require.resolve(`rust-anything-${key}/package.json`))
+ const bin = path.join(dir, exe)
+ if (fs.existsSync(bin)) return bin
+ } catch {}
+ const bundled = path.join(__dirname, '..', 'binaries', key, exe)
+ return fs.existsSync(bundled) ? bundled : null
+}
+
+const bin = find()
+if (!bin) {
+ const known = ['darwin-arm64', 'darwin-x64', 'linux-arm64', 'linux-x64', 'win32-x64'].includes(key)
console.error(
- `rust-anything: no prebuilt binary for ${key}${musl ? ' (the Linux binaries need glibc)' : ''}.\n` +
- 'Install it with Cargo instead: cargo install rust-anything',
+ known
+ ? `rust-anything: the binary package rust-anything-${key} is not installed. ` +
+ 'It is an optional dependency, so it is skipped by --omit=optional or --no-optional; ' +
+ 'install rust-anything again without that flag, or run: cargo install rust-anything'
+ : `rust-anything: no prebuilt binary for ${key}${musl ? ' (the Linux binaries need glibc)' : ''}.\n` +
+ 'Install it with Cargo instead: cargo install rust-anything',
)
process.exit(1)
}
diff --git a/npm/rust-anything/package.json b/npm/rust-anything/package.json
index 8c6d8358..76c55766 100644
--- a/npm/rust-anything/package.json
+++ b/npm/rust-anything/package.json
@@ -18,7 +18,7 @@
"bin": {
"rust-anything": "bin/rust-anything.js"
},
- "files": ["bin", "binaries", "LICENSE-MIT", "LICENSE-APACHE"],
+ "files": ["bin", "LICENSE-MIT", "LICENSE-APACHE"],
"engines": {
"node": ">=18"
}
diff --git a/scripts/npm-packages.mjs b/scripts/npm-packages.mjs
index 5ffec18a..2645314f 100644
--- a/scripts/npm-packages.mjs
+++ b/scripts/npm-packages.mjs
@@ -20,24 +20,52 @@ if (cargoVersion !== version) {
const platforms = ['darwin-arm64', 'darwin-x64', 'linux-arm64', 'linux-x64', 'win32-x64']
fs.rmSync(outDir, { recursive: true, force: true })
-const pkgDir = path.join(outDir, 'rust-anything')
-fs.cpSync(path.join(root, 'npm/rust-anything'), pkgDir, { recursive: true })
+const mainDir = path.join(outDir, 'rust-anything')
+fs.cpSync(path.join(root, 'npm/rust-anything'), mainDir, { recursive: true })
+const main = JSON.parse(fs.readFileSync(path.join(mainDir, 'package.json'), 'utf8'))
+main.version = version
+main.optionalDependencies = {}
+
+const dirs = []
for (const key of platforms) {
- const exe = key.startsWith('win32') ? 'rust-anything.exe' : 'rust-anything'
+ const [os, cpu] = key.split('-')
+ const exe = os === 'win32' ? 'rust-anything.exe' : 'rust-anything'
const from = path.join(binDir, `npm-${key}`, exe)
if (!fs.existsSync(from)) {
console.error(`missing binary for ${key}: ${from}`)
process.exit(1)
}
- const to = path.join(pkgDir, 'binaries', key, exe)
- fs.mkdirSync(path.dirname(to), { recursive: true })
- fs.copyFileSync(from, to)
- fs.chmodSync(to, 0o755)
+ const name = `rust-anything-${key}`
+ const dir = path.join(outDir, name)
+ fs.mkdirSync(dir, { recursive: true })
+ fs.copyFileSync(from, path.join(dir, exe))
+ fs.chmodSync(path.join(dir, exe), 0o755)
+ for (const f of ['LICENSE-MIT', 'LICENSE-APACHE']) fs.copyFileSync(path.join(root, f), path.join(dir, f))
+ const pkg = {
+ name,
+ version,
+ description: `The ${key} binary of rust-anything. Install rust-anything instead: it picks this package for you.`,
+ license: main.license,
+ repository: main.repository,
+ homepage: main.homepage,
+ author: main.author,
+ os: [os],
+ cpu: [cpu],
+ files: [exe, 'LICENSE-MIT', 'LICENSE-APACHE'],
+ }
+ if (os === 'linux') pkg.libc = ['glibc']
+ fs.writeFileSync(path.join(dir, 'package.json'), JSON.stringify(pkg, null, 2) + '\n')
+ fs.writeFileSync(
+ path.join(dir, 'README.md'),
+ `# ${name}\n\nThe ${key} binary of [rust-anything](https://github.com/erkamyaman/rust-anything). ` +
+ 'Install `rust-anything` instead; npm installs the package for your platform.\n',
+ )
+ main.optionalDependencies[name] = version
+ dirs.push(dir)
}
-const pkg = JSON.parse(fs.readFileSync(path.join(pkgDir, 'package.json'), 'utf8'))
-pkg.version = version
-fs.writeFileSync(path.join(pkgDir, 'package.json'), JSON.stringify(pkg, null, 2) + '\n')
-fs.copyFileSync(path.join(root, 'README.md'), path.join(pkgDir, 'README.md'))
-for (const f of ['LICENSE-MIT', 'LICENSE-APACHE']) fs.copyFileSync(path.join(root, f), path.join(pkgDir, f))
-console.log(pkgDir)
+fs.writeFileSync(path.join(mainDir, 'package.json'), JSON.stringify(main, null, 2) + '\n')
+fs.copyFileSync(path.join(root, 'README.md'), path.join(mainDir, 'README.md'))
+for (const f of ['LICENSE-MIT', 'LICENSE-APACHE']) fs.copyFileSync(path.join(root, f), path.join(mainDir, f))
+
+for (const dir of [...dirs, mainDir]) console.log(dir)
diff --git a/website/scripts/asset-bypass.mjs b/website/scripts/asset-bypass.mjs
index 3f802c44..98bfb6ad 100644
--- a/website/scripts/asset-bypass.mjs
+++ b/website/scripts/asset-bypass.mjs
@@ -26,5 +26,6 @@ const STATIC = [
]
config.assets.run_worker_first = ['/**', ...STATIC.map((p) => `!${p}`)]
+config.previews ??= {}
writeFileSync(wranglerPath, `${JSON.stringify(config, null, 2)}\n`)
console.log(`asset-bypass: ${STATIC.length} static paths skip the Worker`)