-
Notifications
You must be signed in to change notification settings - Fork 21.3k
refactor(nix): use native Bun APIs and propagate errors #12694
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
Changes from 6 commits
9789de6
3a22551
3b8b5e8
7b880fb
3aeea9d
7ee5ba0
fec6dd2
bfb3a4a
538560c
c1e0cc3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,27 +1,30 @@ | ||
| import { lstat, mkdir, readdir, rm, symlink } from "fs/promises" | ||
| import { join, relative } from "path" | ||
|
|
||
| type SemverLike = { | ||
| valid: (value: string) => string | null | ||
| rcompare: (left: string, right: string) => number | ||
| } | ||
|
|
||
| type Entry = { | ||
| dir: string | ||
| version: string | ||
| label: string | ||
| } | ||
|
|
||
| async function isDirectory(path: string) { | ||
| try { | ||
| const info = await lstat(path) | ||
| return info.isDirectory() | ||
| } catch { | ||
| return false | ||
| } | ||
| } | ||
|
|
||
| const root = process.cwd() | ||
| const bunRoot = join(root, "node_modules/.bun") | ||
| const linkRoot = join(bunRoot, "node_modules") | ||
| const directories = (await readdir(bunRoot)).sort() | ||
|
|
||
| const versions = new Map<string, Entry[]>() | ||
|
|
||
| for (const entry of directories) { | ||
| const full = join(bunRoot, entry) | ||
| const info = await lstat(full) | ||
| if (!info.isDirectory()) { | ||
| if (!(await isDirectory(full))) { | ||
| continue | ||
| } | ||
| const parsed = parseEntry(entry) | ||
|
|
@@ -33,32 +36,10 @@ for (const entry of directories) { | |
| versions.set(parsed.name, list) | ||
|
jerome-benoit marked this conversation as resolved.
|
||
| } | ||
|
|
||
| const semverModule = (await import(join(bunRoot, "node_modules/semver"))) as | ||
| | SemverLike | ||
| | { | ||
| default: SemverLike | ||
| } | ||
| const semver = "default" in semverModule ? semverModule.default : semverModule | ||
| const selections = new Map<string, Entry>() | ||
|
|
||
| for (const [slug, list] of versions) { | ||
| list.sort((a, b) => { | ||
| const left = semver.valid(a.version) | ||
| const right = semver.valid(b.version) | ||
| if (left && right) { | ||
| const delta = semver.rcompare(left, right) | ||
| if (delta !== 0) { | ||
| return delta | ||
| } | ||
| } | ||
| if (left && !right) { | ||
| return -1 | ||
| } | ||
| if (!left && right) { | ||
| return 1 | ||
| } | ||
| return b.version.localeCompare(a.version) | ||
| }) | ||
|
Comment on lines
-36
to
-61
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sure, using the bun semver module is nice, but why remove the checks for non semver package versions? afaict, the behavior of semver.order is not specified for invalid semver strings and could lead to nondeterministic sorting: https://bun.com/docs/runtime/semver
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in fec6dd2 - added
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. are you sure Bun.semver.order throws when provided invalid semver string? why not
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| list.sort((a, b) => -Bun.semver.order(a.version, b.version)) | ||
| selections.set(slug, list[0]) | ||
| } | ||
|
|
||
|
|
@@ -77,10 +58,7 @@ for (const [slug, entry] of Array.from(selections.entries()).sort((a, b) => a[0] | |
| await mkdir(parent, { recursive: true }) | ||
| const linkPath = join(parent, leaf) | ||
| const desired = join(entry.dir, "node_modules", slug) | ||
| const exists = await lstat(desired) | ||
| .then((info) => info.isDirectory()) | ||
| .catch(() => false) | ||
| if (!exists) { | ||
| if (!(await isDirectory(desired))) { | ||
| continue | ||
| } | ||
| const relativeTarget = relative(parent, desired) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.