pack-npm.sh 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 "$REL"/codegraph-*.zip)
  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. fname="$(basename "$archive")"
  32. case "$fname" in
  33. *.tar.gz) base="${fname%.tar.gz}" ;; # codegraph-<target>
  34. *.zip) base="${fname%.zip}" ;;
  35. esac
  36. target="${base#codegraph-}" # <target>, e.g. darwin-arm64 / win32-x64
  37. os="${target%-*}" # darwin | linux | win32
  38. arch="${target##*-}" # arm64 | x64
  39. pkgdir="$NPM/$base"
  40. mkdir -p "$pkgdir"
  41. case "$fname" in
  42. *.zip)
  43. tmpx="$(mktemp -d)"
  44. unzip -q "$archive" -d "$tmpx"
  45. mv "$tmpx/codegraph-${target}"/* "$pkgdir"/
  46. rm -rf "$tmpx"
  47. nodefile="node.exe"
  48. ;;
  49. *)
  50. tar -xzf "$archive" -C "$pkgdir" --strip-components=1
  51. nodefile="node"
  52. ;;
  53. esac
  54. VERSION="$VERSION" SCOPE="$SCOPE" TARGET="$target" OSV="$os" ARCHV="$arch" NODEFILE="$nodefile" \
  55. node -e '
  56. const fs=require("fs");
  57. fs.writeFileSync(process.argv[1], JSON.stringify({
  58. name: `${process.env.SCOPE}/codegraph-${process.env.TARGET}`,
  59. version: process.env.VERSION,
  60. description: `CodeGraph self-contained bundle for ${process.env.TARGET}`,
  61. os: [process.env.OSV], cpu: [process.env.ARCHV],
  62. files: [process.env.NODEFILE, "lib", "bin"],
  63. license: "MIT"
  64. }, null, 2) + "\n");
  65. ' "$pkgdir/package.json"
  66. targets+=("$target")
  67. echo "[pack-npm] ${SCOPE}/codegraph-${target}@${VERSION}"
  68. done
  69. # Main shim package.
  70. cp "$ROOT/scripts/npm-shim.js" "$NPM/main/npm-shim.js"
  71. [ -f "$ROOT/README.md" ] && cp "$ROOT/README.md" "$NPM/main/README.md"
  72. VERSION="$VERSION" SCOPE="$SCOPE" TARGETS="${targets[*]}" \
  73. node -e '
  74. const fs=require("fs");
  75. const opt={};
  76. for (const t of process.env.TARGETS.split(/\s+/).filter(Boolean))
  77. opt[`${process.env.SCOPE}/codegraph-${t}`]=process.env.VERSION;
  78. fs.writeFileSync(process.argv[1], JSON.stringify({
  79. name: `${process.env.SCOPE}/codegraph`,
  80. version: process.env.VERSION,
  81. description: "Local-first code intelligence for AI agents (MCP). Self-contained — bundles its own runtime.",
  82. bin: { codegraph: "npm-shim.js" },
  83. optionalDependencies: opt,
  84. files: ["npm-shim.js","README.md"],
  85. license: "MIT"
  86. }, null, 2) + "\n");
  87. ' "$NPM/main/package.json"
  88. echo "[pack-npm] ${SCOPE}/codegraph@${VERSION} (${#targets[@]} platform packages in optionalDependencies)"
  89. echo "[pack-npm] output: $NPM"