pack-npm.sh 5.2 KB

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