Skip to content

Commit

Permalink
feat: use yieldable errors
Browse files Browse the repository at this point in the history
  • Loading branch information
velut committed Mar 7, 2024
1 parent 36e8930 commit 265c62a
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 44 deletions.
11 changes: 5 additions & 6 deletions src/create-project.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Effect } from "effect";
import { Data, Effect } from "effect";
import {
ModuleKind,
ModuleResolutionKind,
Expand All @@ -12,10 +12,9 @@ export type CreateProjectOptions = {
};

/** @internal */
export class ProjectError {
readonly _tag = "ProjectError";
constructor(readonly cause?: unknown) {}
}
export class ProjectError extends Data.TaggedError("ProjectError")<{
readonly cause?: unknown;
}> {}

export const createProject = ({ indexFilePath, cwd }: CreateProjectOptions) =>
Effect.try({
Expand All @@ -40,5 +39,5 @@ export const createProject = ({ indexFilePath, cwd }: CreateProjectOptions) =>
project.resolveSourceFileDependencies();
return { project, indexFile };
},
catch: (e) => new ProjectError(e),
catch: (e) => new ProjectError({ cause: e }),
});
11 changes: 5 additions & 6 deletions src/install-package.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Effect } from "effect";
import { Data, Effect } from "effect";
import { execa } from "execa";

/** @internal */
Expand All @@ -9,10 +9,9 @@ export type InstallPackageOptions = {
};

/** @internal */
export class InstallPackageError {
readonly _tag = "InstallPackageError";
constructor(readonly cause?: unknown) {}
}
export class InstallPackageError extends Data.TaggedError(
"InstallPackageError",
)<{ cause?: unknown }> {}

/** @internal */
export const installPackage = ({
Expand All @@ -28,7 +27,7 @@ export const installPackage = ({
const bunAdd = ({ pkg, cwd, bunPath }: Required<InstallPackageOptions>) =>
Effect.tryPromise({
try: () => execa(bunPath, ["add", pkg, "--verbose"], { cwd }),
catch: (e) => new InstallPackageError(e),
catch: (e) => new InstallPackageError({ cause: e }),
});

const installedPackages = (stdout: string) => {
Expand Down
11 changes: 5 additions & 6 deletions src/package-declarations.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Effect } from "effect";
import { Data, Effect } from "effect";
import type { Project, SourceFile } from "ts-morph";
import { extractDeclarations } from "./extract-declarations";

Expand All @@ -10,10 +10,9 @@ export type PackageDeclarationsOptions = {
};

/** @internal */
export class PackageDeclarationsError {
readonly _tag = "PackageDeclarationsError";
constructor(readonly cause?: unknown) {}
}
export class PackageDeclarationsError extends Data.TaggedError(
"PackageDeclarationsError",
)<{ cause?: unknown }> {}

export const packageDeclarations = ({
pkgName,
Expand All @@ -30,5 +29,5 @@ export const packageDeclarations = ({
project,
pkgName,
}),
catch: (e) => new PackageDeclarationsError(e),
catch: (e) => new PackageDeclarationsError({ cause: e }),
});
11 changes: 5 additions & 6 deletions src/package-json.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { Effect } from "effect";
import { Data, Effect } from "effect";
import { readPackage } from "read-pkg";

/** @internal */
export class PackageJsonError {
readonly _tag = "PackageJsonError";
constructor(readonly cause?: unknown) {}
}
export class PackageJsonError extends Data.TaggedError("PackageJsonError")<{
cause?: unknown;
}> {}

/** @internal */
export const packageJson = (pkgDir: string) =>
Effect.tryPromise({
try: () => readPackage({ cwd: pkgDir }),
catch: (e) => new PackageJsonError(e),
catch: (e) => new PackageJsonError({ cause: e }),
});
20 changes: 11 additions & 9 deletions src/package-name.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import { Effect } from "effect";
import { Data, Effect } from "effect";
import validate from "validate-npm-package-name";

/** @internal */
export class PackageNameError {
readonly _tag = "PackageNameError";
}
export class PackageNameError extends Data.TaggedError("PackageNameError")<{
warnings?: string[];
errors?: string[];
}> {}

/** @internal */
export const packageName = (pkg: string) =>
Effect.suspend(() => {
Effect.gen(function* (_) {
const versionMarker = pkg.lastIndexOf("@");
const name = pkg.slice(0, versionMarker > 0 ? versionMarker : undefined);
if (!validate(name).validForNewPackages) {
return Effect.fail(new PackageNameError());
const pkgName = pkg.slice(0, versionMarker > 0 ? versionMarker : undefined);
const { validForNewPackages, warnings, errors } = validate(pkgName);
if (!validForNewPackages) {
return yield* _(new PackageNameError({ warnings, errors }));
}
return Effect.succeed(name);
return pkgName;
});
8 changes: 3 additions & 5 deletions src/package-types.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { Effect } from "effect";
import { Data, Effect } from "effect";
import type { NormalizedPackageJson } from "read-pkg";
import { exports } from "resolve.exports";

/** @internal */
export class PackageTypesError {
readonly _tag = "PackageTypesError";
}
export class PackageTypesError extends Data.TaggedError("PackageTypesError") {}

/**
`packageTypes` resolves the types entrypoint file (e.g., `index.d.ts`).
Expand All @@ -32,7 +30,7 @@ export const packageTypes = (
if (isRootSubpath && pkgJson.typings && isTypesFile(pkgJson.typings)) {
return pkgJson.typings;
}
return yield* _(Effect.fail(new PackageTypesError()));
return yield* _(new PackageTypesError());
});

const resolveExports = (
Expand Down
11 changes: 5 additions & 6 deletions src/work-dir.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Effect } from "effect";
import { Data, Effect } from "effect";
import { rm } from "node:fs/promises";
import { temporaryDirectory } from "tempy";

Expand All @@ -9,10 +9,9 @@ export type WorkDir = {
};

/** @internal */
export class WorkDirError {
readonly _tag = "WorkDirError";
constructor(readonly cause?: unknown) {}
}
export class WorkDirError extends Data.TaggedError("WorkDirError")<{
cause?: unknown;
}> {}

const acquire = Effect.try({
try: () => {
Expand All @@ -26,7 +25,7 @@ const acquire = Effect.try({
},
};
},
catch: (e) => new WorkDirError(e),
catch: (e) => new WorkDirError({ cause: e }),
});

const release = (workDir: WorkDir) => Effect.promise(() => workDir.close());
Expand Down

0 comments on commit 265c62a

Please sign in to comment.