-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add installed packages to the context
- Loading branch information
Showing
3 changed files
with
69 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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 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,48 @@ | ||
import { ok } from "neverthrow"; | ||
import { temporaryDirectoryTask } from "tempy"; | ||
import { expect, test } from "vitest"; | ||
import { InstallPackageError } from "./errors"; | ||
import { installPackage } from "./install-package"; | ||
|
||
test("invalid package", async () => { | ||
await temporaryDirectoryTask(async (dir) => { | ||
process.chdir(dir); | ||
const res = await installPackage(""); | ||
expect(res.isErr()).toBe(true); | ||
expect(res._unsafeUnwrapErr() instanceof InstallPackageError).toBe(true); | ||
}); | ||
}); | ||
|
||
test("package with no production dependencies", async () => { | ||
await temporaryDirectoryTask(async (dir) => { | ||
process.chdir(dir); | ||
const installedPackages = await installPackage("verify-hcaptcha@1.0.0"); | ||
expect(installedPackages).toStrictEqual(ok(["verify-hcaptcha@1.0.0"])); | ||
}); | ||
}); | ||
|
||
test("package with some production dependencies", async () => { | ||
await temporaryDirectoryTask(async (dir) => { | ||
process.chdir(dir); | ||
const installedPackages = await installPackage("query-registry@2.6.0"); | ||
expect(installedPackages).toStrictEqual( | ||
ok([ | ||
"builtins@5.0.1", | ||
"isomorphic-unfetch@3.1.0", | ||
"lru-cache@6.0.0", | ||
"make-error@1.3.6", | ||
"node-fetch@2.7.0", | ||
"query-registry@2.6.0", | ||
"semver@7.5.4", | ||
"tiny-lru@8.0.2", | ||
"tr46@0.0.3", | ||
"unfetch@4.2.0", | ||
"url-join@4.0.1", | ||
"validate-npm-package-name@4.0.0", | ||
"webidl-conversions@3.0.1", | ||
"whatwg-url@5.0.0", | ||
"yallist@4.0.0", | ||
]), | ||
); | ||
}); | ||
}); |
This file contains 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 |
---|---|---|
@@ -1,9 +1,23 @@ | ||
import { $ } from "execa"; | ||
import { execa } from "execa"; | ||
import { ResultAsync } from "neverthrow"; | ||
import { InstallPackageError } from "./errors"; | ||
|
||
export const installPackage = (pkg: string) => | ||
export const installPackage = ( | ||
pkg: string, | ||
): ResultAsync<string[], InstallPackageError> => | ||
ResultAsync.fromPromise( | ||
$`bun add ${pkg}`, | ||
execa("bun", ["add", pkg, "--verbose"]), | ||
(e) => new InstallPackageError("failed to install package", { cause: e }), | ||
); | ||
).map(({ stdout }) => { | ||
// With verbose output on, bun prints one line per installed package | ||
// (for example, "foo@1.0.0"), including all installed dependencies. | ||
// These lines are between the two delimiting lines found here: | ||
// https://github.com/oven-sh/bun/blob/main/src/install/lockfile.zig#L5354-L5355. | ||
const lines = stdout.split("\n"); | ||
const beginHash = lines.findIndex((line) => | ||
line.startsWith("-- BEGIN SHA512/256"), | ||
); | ||
const endHash = lines.findIndex((line) => line.startsWith("-- END HASH")); | ||
const installedPackages = lines.slice(beginHash + 1, endHash); | ||
return installedPackages; | ||
}); |