-
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 PackageManager and update extractPackageApiEffect
- Loading branch information
Showing
8 changed files
with
99 additions
and
75 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { Effect } from "effect"; | ||
import { temporaryDirectoryTask } from "tempy"; | ||
import { expect, test } from "vitest"; | ||
import { bunPackageManager } from "./bun-package-manager"; | ||
import type { InstallPackageOptions } from "./package-manager"; | ||
|
||
const bun = bunPackageManager(); | ||
|
||
const _installPackage = (options: InstallPackageOptions) => | ||
Effect.runPromise(bun.installPackage(options)); | ||
|
||
test("invalid package", async () => { | ||
await temporaryDirectoryTask(async (cwd) => { | ||
await expect(_installPackage({ pkg: "", cwd })).rejects.toThrow(); | ||
}); | ||
}); | ||
|
||
test("package with no production dependencies", async () => { | ||
await temporaryDirectoryTask(async (cwd) => { | ||
await expect(_installPackage({ pkg: "[email protected]", cwd })).resolves.toStrictEqual([ | ||
"[email protected]", | ||
]); | ||
}); | ||
}); | ||
|
||
test("package with some production dependencies", async () => { | ||
await temporaryDirectoryTask(async (cwd) => { | ||
await expect(_installPackage({ pkg: "[email protected]", cwd })).resolves.toContain( | ||
"[email protected]", | ||
); | ||
}); | ||
}); |
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,29 @@ | ||
import { Effect } from "effect"; | ||
import { execa } from "execa"; | ||
import { InstallPackageError, PackageManager } from "./package-manager"; | ||
|
||
/** @internal */ | ||
export const bunPackageManager = (bunPath = "bun") => | ||
PackageManager.of({ | ||
installPackage: ({ pkg, cwd }) => | ||
Effect.gen(function* (_) { | ||
// Run `bun add <pkg> --verbose`. | ||
// See https://bun.sh/docs/cli/add. | ||
const { stdout } = yield* _( | ||
Effect.tryPromise({ | ||
try: () => execa(bunPath, ["add", pkg, "--verbose"], { cwd }), | ||
catch: (e) => new InstallPackageError({ cause: e }), | ||
}), | ||
); | ||
|
||
// With verbose output on, bun prints one line per installed package | ||
// (e.g., "[email protected]"), including all installed dependencies. | ||
// These lines are between the two delimiting lines found here: | ||
// https://github.com/oven-sh/bun/blob/972a7b7080bd3066b54dcb43e9c91c5dfa26a69c/src/install/lockfile.zig#L5369-L5370. | ||
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; | ||
}), | ||
}); |
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
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,23 @@ | ||
import { Context, Data, Effect } from "effect"; | ||
|
||
/** @internal */ | ||
export type InstallPackageOptions = { | ||
pkg: string; | ||
cwd: string; | ||
}; | ||
|
||
/** @internal */ | ||
export class InstallPackageError extends Data.TaggedError("InstallPackageError")<{ | ||
cause?: unknown; | ||
}> {} | ||
|
||
/** @internal */ | ||
export class PackageManager extends Context.Tag("PackageManager")< | ||
PackageManager, | ||
{ | ||
readonly installPackage: ({ | ||
pkg, | ||
cwd, | ||
}: InstallPackageOptions) => Effect.Effect<string[], InstallPackageError>; | ||
} | ||
>() {} |