npm-shim.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #!/usr/bin/env node
  2. 'use strict';
  3. //
  4. // npm thin-installer launcher for CodeGraph.
  5. //
  6. // The heavy artifact (a vendored Node runtime + the app) ships as a per-platform
  7. // optionalDependency: @colbymchenry/codegraph-<platform>-<arch>. npm installs
  8. // only the one matching the host, via each package's `os`/`cpu` fields (the
  9. // esbuild pattern). This shim — run by the user's OWN Node — locates that bundle
  10. // and execs its launcher, so the real work always runs on the bundled Node 24
  11. // (with node:sqlite), regardless of the user's Node version. The user's Node is
  12. // only ever a launcher; even an ancient version can run this file.
  13. //
  14. // Wired up at release time as the main package's `bin`:
  15. // "bin": { "codegraph": "scripts/npm-shim.js" }
  16. // with the platform packages listed in `optionalDependencies`.
  17. var childProcess = require('child_process');
  18. var target = process.platform + '-' + process.arch; // e.g. darwin-arm64, linux-x64
  19. var pkg = '@colbymchenry/codegraph-' + target;
  20. var launcher = process.platform === 'win32' ? 'bin/codegraph.cmd' : 'bin/codegraph';
  21. var binPath;
  22. try {
  23. binPath = require.resolve(pkg + '/' + launcher);
  24. } catch (e) {
  25. process.stderr.write(
  26. 'codegraph: no prebuilt bundle for ' + target + '.\n' +
  27. 'Expected the optional package ' + pkg + ' to be installed.\n' +
  28. 'Try reinstalling: npm i -g @colbymchenry/codegraph\n' +
  29. 'Or use the standalone installer (no Node required):\n' +
  30. ' curl -fsSL https://raw.githubusercontent.com/colbymchenry/codegraph/main/install.sh | sh\n'
  31. );
  32. process.exit(1);
  33. }
  34. var res = childProcess.spawnSync(binPath, process.argv.slice(2), { stdio: 'inherit' });
  35. if (res.error) {
  36. process.stderr.write('codegraph: ' + res.error.message + '\n');
  37. process.exit(1);
  38. }
  39. process.exit(res.status === null ? 1 : res.status);