pack-npm.sh 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #!/usr/bin/env bash
  2. #
  3. # Assemble the npm thin-installer packages from built bundles (esbuild pattern).
  4. #
  5. # Produces, under release/npm/:
  6. # codegraph-<target>/ one per built bundle — the vendored Node + app, tagged
  7. # with os/cpu so npm installs only the matching one.
  8. # main/ the @colbymchenry/codegraph shim package: a tiny bin
  9. # that execs the matching platform bundle, with every
  10. # platform package in optionalDependencies.
  11. #
  12. # The release pipeline then `npm publish`es each dir. This does NOT touch the
  13. # repo's package.json — the dev/from-source path keeps working; the *published*
  14. # main package's shape is generated here.
  15. #
  16. # Prereq: run build-bundle.sh for each target first (release/codegraph-*.tar.gz).
  17. # Usage: scripts/pack-npm.sh [version] (default: version from package.json)
  18. set -euo pipefail
  19. ROOT="$(cd "$(dirname "$0")/.." && pwd)"
  20. VERSION="${1:-$(node -p "require('$ROOT/package.json').version")}"
  21. SCOPE="@colbymchenry"
  22. REL="$ROOT/release"
  23. NPM="$REL/npm"
  24. rm -rf "$NPM"
  25. mkdir -p "$NPM/main"
  26. shopt -s nullglob
  27. archives=("$REL"/codegraph-*.tar.gz)
  28. [ ${#archives[@]} -gt 0 ] || { echo "[pack-npm] no bundles in $REL — run build-bundle.sh first" >&2; exit 1; }
  29. targets=()
  30. for archive in "${archives[@]}"; do
  31. base="$(basename "$archive" .tar.gz)" # codegraph-<target>
  32. target="${base#codegraph-}" # <target>, e.g. darwin-arm64
  33. os="${target%-*}" # darwin
  34. arch="${target##*-}" # arm64
  35. pkgdir="$NPM/$base"
  36. mkdir -p "$pkgdir"
  37. tar -xzf "$archive" -C "$pkgdir" --strip-components=1
  38. VERSION="$VERSION" SCOPE="$SCOPE" TARGET="$target" OSV="$os" ARCHV="$arch" \
  39. node -e '
  40. const fs=require("fs");
  41. fs.writeFileSync(process.argv[1], JSON.stringify({
  42. name: `${process.env.SCOPE}/codegraph-${process.env.TARGET}`,
  43. version: process.env.VERSION,
  44. description: `CodeGraph self-contained bundle for ${process.env.TARGET}`,
  45. os: [process.env.OSV], cpu: [process.env.ARCHV],
  46. files: ["node","lib","bin"],
  47. license: "MIT"
  48. }, null, 2) + "\n");
  49. ' "$pkgdir/package.json"
  50. targets+=("$target")
  51. echo "[pack-npm] ${SCOPE}/codegraph-${target}@${VERSION}"
  52. done
  53. # Main shim package.
  54. cp "$ROOT/scripts/npm-shim.js" "$NPM/main/npm-shim.js"
  55. [ -f "$ROOT/README.md" ] && cp "$ROOT/README.md" "$NPM/main/README.md"
  56. VERSION="$VERSION" SCOPE="$SCOPE" TARGETS="${targets[*]}" \
  57. node -e '
  58. const fs=require("fs");
  59. const opt={};
  60. for (const t of process.env.TARGETS.split(/\s+/).filter(Boolean))
  61. opt[`${process.env.SCOPE}/codegraph-${t}`]=process.env.VERSION;
  62. fs.writeFileSync(process.argv[1], JSON.stringify({
  63. name: `${process.env.SCOPE}/codegraph`,
  64. version: process.env.VERSION,
  65. description: "Local-first code intelligence for AI agents (MCP). Self-contained — bundles its own runtime.",
  66. bin: { codegraph: "npm-shim.js" },
  67. optionalDependencies: opt,
  68. files: ["npm-shim.js","README.md"],
  69. license: "MIT"
  70. }, null, 2) + "\n");
  71. ' "$NPM/main/package.json"
  72. echo "[pack-npm] ${SCOPE}/codegraph@${VERSION} (${#targets[@]} platform packages in optionalDependencies)"
  73. echo "[pack-npm] output: $NPM"