Skip to content

Commit 9f60c09

Browse files
committed
feat: restore startDir on error
1 parent a2d06a8 commit 9f60c09

File tree

2 files changed

+54
-23
lines changed

2 files changed

+54
-23
lines changed

src/extract-package-api.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,32 @@ import {
77
import { extractPackageApi } from "./extract-package-api";
88

99
test("invalid package name", async () => {
10+
const startDir = process.cwd();
1011
const res = await extractPackageApi({ pkg: "" });
1112
expect(res.isErr()).toBe(true);
1213
expect(res._unsafeUnwrapErr() instanceof PackageNameError).toBe(true);
14+
expect(process.cwd()).toBe(startDir);
1315
});
1416

1517
test("package not found", async () => {
18+
const startDir = process.cwd();
1619
const res = await extractPackageApi({ pkg: "@jsdocs-io/not-found" });
1720
expect(res.isErr()).toBe(true);
1821
expect(res._unsafeUnwrapErr() instanceof InstallPackageError).toBe(true);
22+
expect(process.cwd()).toBe(startDir);
1923
});
2024

2125
test("package types not found", async () => {
26+
const startDir = process.cwd();
2227
const res = await extractPackageApi({ pkg: "unlicensed@0.4.0" });
2328
expect(res.isErr()).toBe(true);
2429
expect(res._unsafeUnwrapErr() instanceof PackageTypesError).toBe(true);
30+
expect(process.cwd()).toBe(startDir);
2531
});
2632

2733
test("package successfully analyzed", async () => {
34+
const startDir = process.cwd();
2835
const res = await extractPackageApi({ pkg: "short-time-ago@2.0.0" });
2936
expect(res.isOk()).toBe(true);
37+
expect(process.cwd()).toBe(startDir);
3038
});

src/extract-package-api.ts

Lines changed: 46 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ResultAsync, ok, okAsync } from "neverthrow";
1+
import { ResultAsync, err, ok, okAsync } from "neverthrow";
22
import { performance } from "node:perf_hooks";
33
import { join } from "pathe";
44
import { changeDir } from "./change-dir";
@@ -168,35 +168,53 @@ export const extractPackageApi = ({
168168
)
169169
.andThen((ctx) => changeDir(ctx.rootDir).map(() => ctx))
170170
.andThen((ctx) =>
171-
installPackage(ctx.pkg).map((installedPackages) => ({
172-
...ctx,
173-
nodeModulesDir: join(ctx.rootDir, "node_modules"),
174-
pkgDir: join(ctx.rootDir, "node_modules", ctx.pkgName),
175-
installedPackages,
176-
})),
171+
installPackage(ctx.pkg)
172+
.map((installedPackages) => ({
173+
...ctx,
174+
nodeModulesDir: join(ctx.rootDir, "node_modules"),
175+
pkgDir: join(ctx.rootDir, "node_modules", ctx.pkgName),
176+
installedPackages,
177+
}))
178+
.orElse((e) => {
179+
changeDir(ctx.startDir);
180+
return err(e);
181+
}),
177182
)
178183
.andThen((ctx) =>
179-
packageJson(ctx.pkgDir).map((pkgJson) => ({
180-
...ctx,
181-
pkgJson,
182-
})),
184+
packageJson(ctx.pkgDir)
185+
.map((pkgJson) => ({
186+
...ctx,
187+
pkgJson,
188+
}))
189+
.orElse((e) => {
190+
changeDir(ctx.startDir);
191+
return err(e);
192+
}),
183193
)
184194
.andThen((ctx) =>
185-
packageTypes(ctx.pkgJson, ctx.pkgSubpath).map((pkgTypes) => ({
186-
...ctx,
187-
pkgTypes,
188-
typesFilePath: join(ctx.pkgDir, pkgTypes),
189-
})),
195+
packageTypes(ctx.pkgJson, ctx.pkgSubpath)
196+
.map((pkgTypes) => ({
197+
...ctx,
198+
pkgTypes,
199+
typesFilePath: join(ctx.pkgDir, pkgTypes),
200+
}))
201+
.orElse((e) => {
202+
changeDir(ctx.startDir);
203+
return err(e);
204+
}),
190205
)
191206
.andThen((ctx) =>
192-
createProject(ctx.typesFilePath).map(
193-
({ project, indexFile, sourceFiles }) => ({
207+
createProject(ctx.typesFilePath)
208+
.map(({ project, indexFile, sourceFiles }) => ({
194209
...ctx,
195210
project,
196211
indexFile,
197212
sourceFiles,
213+
}))
214+
.orElse((e) => {
215+
changeDir(ctx.startDir);
216+
return err(e);
198217
}),
199-
),
200218
)
201219
.andThen((ctx) =>
202220
ok(packageOverview(ctx.indexFile)).map((pkgOverview) => ({
@@ -210,10 +228,15 @@ export const extractPackageApi = ({
210228
project: ctx.project,
211229
indexFile: ctx.indexFile,
212230
maxDepth: ctx.maxDepth,
213-
}).map((pkgDeclarations) => ({
214-
...ctx,
215-
pkgDeclarations,
216-
})),
231+
})
232+
.map((pkgDeclarations) => ({
233+
...ctx,
234+
pkgDeclarations,
235+
}))
236+
.orElse((e) => {
237+
changeDir(ctx.startDir);
238+
return err(e);
239+
}),
217240
)
218241
.andThen((ctx) =>
219242
changeDir(ctx.startDir)

0 commit comments

Comments
 (0)