-
Notifications
You must be signed in to change notification settings - Fork 0
fix(bin): single bin + lazy pho symlink so unpinned npx works #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| import { existsSync, lstatSync, symlinkSync } from "node:fs"; | ||
| import { join, resolve, sep } from "node:path"; | ||
|
|
||
| /** | ||
| * Lazily install `pho` as a sibling of `photon` in whichever bin directory | ||
| * we were launched from. | ||
| * | ||
| * Why not declare both bins in package.json? npm 11's `npx <scoped-pkg>` | ||
| * (no version) skips bin auto-resolve when `bin` has multiple keys. Keeping | ||
| * `bin` as a single string preserves clean `npx @photon-ai/photon` usage. | ||
| * | ||
| * Why not a `postinstall` script? Bun blocks postinstall by default — that | ||
| * would silently strip `pho` from `bun add -g` (our primary install path). | ||
| * | ||
| * Approach: walk up from the running script's path to the package root, then | ||
| * try the standard bin-dir layouts adjacent to it. First match wins. | ||
| * | ||
| * <root>/node_modules/.bin (local install) | ||
| * <prefix>/bin with <prefix>/lib/node_modules/... (npm/yarn/pnpm -g) | ||
| * ~/.bun/bin with ~/.bun/install/global/node_modules/... (bun -g) | ||
| * | ||
| * Cost is two stat calls per launch after `pho` exists. Errors are swallowed | ||
| * — `pho` is convenience, never load-bearing. | ||
| */ | ||
| export function ensurePhoAlias(): void { | ||
| try { | ||
| const me = process.argv[1]; | ||
| if (!me) return; | ||
|
|
||
| // Guard: only run when launched from an installed package layout. | ||
| // Source-mode runs (`bun run src/index.ts`) skip this entirely. | ||
| const installedShape = new RegExp( | ||
| `${escapeForRegex(sep)}node_modules${escapeForRegex(sep)}.+${escapeForRegex(sep)}dist${escapeForRegex(sep)}photon\\.js$`, | ||
| ); | ||
| if (!installedShape.test(me)) return; | ||
|
|
||
| // process.argv[1] resolves symlinks → me is `<pkg>/dist/photon.js`. | ||
| // pkgRoot is two levels up. | ||
| const pkgRoot = resolve(me, "..", ".."); | ||
|
|
||
| const candidates = [ | ||
| resolve(pkgRoot, "..", "..", ".bin"), // local: node_modules/.bin | ||
| resolve(pkgRoot, "..", "..", "..", "..", "bin"), // npm/yarn/pnpm global: <prefix>/bin | ||
| resolve(pkgRoot, "..", "..", "..", "..", "..", "bin"), // bun global: ~/.bun/bin | ||
| ]; | ||
|
|
||
| for (const dir of candidates) { | ||
| const photon = join(dir, "photon"); | ||
| const pho = join(dir, "pho"); | ||
| if (!existsSync(photon)) continue; | ||
|
|
||
| // lstatSync detects ANY entry at `pho` — including broken symlinks | ||
| // that existsSync would miss (existsSync follows the link target). | ||
| try { | ||
| lstatSync(pho); | ||
| return; // something already lives here; leave it alone | ||
| } catch { | ||
| // pho doesn't exist; safe to create | ||
| } | ||
|
|
||
| symlinkSync("./photon", pho); | ||
| return; | ||
|
lcandy2 marked this conversation as resolved.
|
||
| } | ||
| } catch { | ||
| // best-effort | ||
| } | ||
| } | ||
|
|
||
| function escapeForRegex(s: string): string { | ||
| return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.