Skip to content

Commit

Permalink
refactor: remove adapter function wrappers from Effect gen functions
Browse files Browse the repository at this point in the history
  • Loading branch information
velut committed Apr 25, 2024
1 parent 8887ccd commit 53f5a68
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 23 deletions.
12 changes: 5 additions & 7 deletions src/bun-package-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@ import { InstallPackageError, PackageManager } from "./package-manager";
export const bunPackageManager = (bunPath = "bun") =>
PackageManager.of({
installPackage: ({ pkg, cwd }) =>
Effect.gen(function* (_) {
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 }),
}),
);
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.
Expand Down
18 changes: 9 additions & 9 deletions src/extract-package-api-effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ export const extractPackageApiEffect = ({
subpath = ".",
maxDepth = 5,
}: Omit<ExtractPackageApiOptions, "bunPath">) =>
Effect.gen(function* (_) {
Effect.gen(function* () {
const startTime = performance.now();
const pkgName = yield* _(packageName(pkg));
const { path: cwd } = yield* _(workDir);
const pm = yield* _(PackageManager);
const packages = yield* _(pm.installPackage({ pkg, cwd }));
const pkgName = yield* packageName(pkg);
const { path: cwd } = yield* workDir;
const pm = yield* PackageManager;
const packages = yield* pm.installPackage({ pkg, cwd });
const pkgDir = join(cwd, "node_modules", pkgName);
const pkgJson = yield* _(packageJson(pkgDir));
const types = yield* _(packageTypes(pkgJson, subpath));
const pkgJson = yield* packageJson(pkgDir);
const types = yield* packageTypes(pkgJson, subpath);
const indexFilePath = join(pkgDir, types);
const { project, indexFile } = yield* _(createProject({ indexFilePath, cwd }));
const { project, indexFile } = yield* createProject({ indexFilePath, cwd });
const overview = packageOverview(indexFile);
const declarations = yield* _(packageDeclarations({ pkgName, project, indexFile, maxDepth }));
const declarations = yield* packageDeclarations({ pkgName, project, indexFile, maxDepth });
const pkgApi: PackageApi = {
name: pkgJson.name,
version: pkgJson.version,
Expand Down
4 changes: 2 additions & 2 deletions src/package-name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ export class PackageNameError extends Data.TaggedError("PackageNameError")<{

/** @internal */
export const packageName = (pkg: string) =>
Effect.gen(function* (_) {
Effect.gen(function* () {
const versionMarker = pkg.lastIndexOf("@");
const pkgName = pkg.slice(0, versionMarker > 0 ? versionMarker : undefined);
const { validForNewPackages, warnings, errors } = validate(pkgName);
if (!validForNewPackages) {
return yield* _(new PackageNameError({ warnings, errors }));
return yield* new PackageNameError({ warnings, errors });
}
return pkgName;
});
6 changes: 3 additions & 3 deletions src/package-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ export class PackageTypesError extends Data.TaggedError("PackageTypesError") {}
@internal
*/
export const packageTypes = (pkgJson: Partial<NormalizedPackageJson>, subpath: string) =>
Effect.gen(function* (_) {
const resolvedPaths = yield* _(resolveExports(pkgJson, subpath));
Effect.gen(function* () {
const resolvedPaths = yield* resolveExports(pkgJson, subpath);
const firstPath = resolvedPaths[0];
if (firstPath && isTypesFile(firstPath)) {
return firstPath;
Expand All @@ -27,7 +27,7 @@ export const packageTypes = (pkgJson: Partial<NormalizedPackageJson>, subpath: s
if (isRootSubpath && pkgJson.typings && isTypesFile(pkgJson.typings)) {
return pkgJson.typings;
}
return yield* _(new PackageTypesError());
return yield* new PackageTypesError();
});

const resolveExports = (pkgJson: Partial<NormalizedPackageJson>, subpath: string) => {
Expand Down
4 changes: 2 additions & 2 deletions src/work-dir.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import { workDir } from "./work-dir";

const _workDir = () =>
Effect.runPromise(
Effect.gen(function* (_) {
const { path } = yield* _(workDir);
Effect.gen(function* () {
const { path } = yield* workDir;
return path;
}).pipe(Effect.scoped),
);
Expand Down

0 comments on commit 53f5a68

Please sign in to comment.