1
0

pack-npm.sh 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. # npm-shim.js CLI/MCP launcher (execs the bundled Node) — the `bin`.
  71. # npm-sdk.js programmatic/embedded entry (#354): re-exports the installed
  72. # platform bundle's compiled library — the `main`.
  73. # dist/ the .d.ts tree only (types). The runtime .js stays in the
  74. # per-platform bundle so its deps aren't duplicated here.
  75. cp "$ROOT/scripts/npm-shim.js" "$NPM/main/npm-shim.js"
  76. cp "$ROOT/scripts/npm-sdk.js" "$NPM/main/npm-sdk.js"
  77. [ -f "$ROOT/README.md" ] && cp "$ROOT/README.md" "$NPM/main/README.md"
  78. # Ship the type declarations so `types`/`exports.types` resolve. Built from this
  79. # same release, so they can't skew from the runtime npm-sdk.js re-exports.
  80. [ -f "$ROOT/dist/index.d.ts" ] || ( echo "[pack-npm] building dist for .d.ts" >&2 && cd "$ROOT" && npm run build >/dev/null )
  81. ROOT="$ROOT" DEST="$NPM/main" node -e '
  82. const fs=require("fs"), path=require("path");
  83. const src=path.join(process.env.ROOT,"dist"), dest=path.join(process.env.DEST,"dist");
  84. fs.cpSync(src, dest, { recursive:true, filter(s){
  85. try { return fs.statSync(s).isDirectory() || s.endsWith(".d.ts"); } catch (e) { return false; }
  86. }});
  87. '
  88. VERSION="$VERSION" SCOPE="$SCOPE" TARGETS="${targets[*]}" \
  89. node -e '
  90. const fs=require("fs");
  91. const opt={};
  92. for (const t of process.env.TARGETS.split(/\s+/).filter(Boolean))
  93. opt[`${process.env.SCOPE}/codegraph-${t}`]=process.env.VERSION;
  94. fs.writeFileSync(process.argv[1], JSON.stringify({
  95. name: `${process.env.SCOPE}/codegraph`,
  96. version: process.env.VERSION,
  97. description: "Local-first code intelligence for AI agents (MCP). Self-contained — bundles its own runtime.",
  98. bin: { codegraph: "npm-shim.js" },
  99. main: "npm-sdk.js",
  100. types: "dist/index.d.ts",
  101. exports: {
  102. ".": { types: "./dist/index.d.ts", default: "./npm-sdk.js" },
  103. "./package.json": "./package.json"
  104. },
  105. optionalDependencies: opt,
  106. files: ["npm-shim.js","npm-sdk.js","dist","README.md"],
  107. license: "MIT"
  108. }, null, 2) + "\n");
  109. ' "$NPM/main/package.json"
  110. echo "[pack-npm] ${SCOPE}/codegraph@${VERSION} (${#targets[@]} platform packages in optionalDependencies)"
  111. echo "[pack-npm] output: $NPM"