pack-npm.sh 5.4 KB

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