Skip to content

Commit 07e6e5d

Browse files
committed
refactor: split createProject
1 parent 30b795a commit 07e6e5d

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

src/create-project.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Result, err, ok } from "neverthrow";
2+
import process from "node:process";
23
import {
34
ModuleKind,
45
ModuleResolutionKind,
@@ -22,13 +23,24 @@ export const createProject = ({
2223
indexFilePath,
2324
cwd,
2425
}: CreateProjectOptions): Result<ProjectContainer, ProjectError> => {
25-
let startDir;
2626
try {
2727
// By default, ts-morph creates the project in the current working directory.
2828
// We must change it to the temporary directory where the packages are installed,
2929
// otherwise TypeScript will pick up type definitions from our local `node_modules`.
30-
startDir = process.cwd();
30+
const startDir = process.cwd();
3131
process.chdir(cwd);
32+
const res = _createProject(indexFilePath);
33+
process.chdir(startDir);
34+
return res;
35+
} catch (e) {
36+
return err(new ProjectError("failed to change directories", { cause: e }));
37+
}
38+
};
39+
40+
const _createProject = (
41+
indexFilePath: string,
42+
): Result<ProjectContainer, ProjectError> => {
43+
try {
3244
const project = new Project({
3345
compilerOptions: {
3446
// See https://github.com/dsherret/ts-morph/issues/938
@@ -47,11 +59,5 @@ export const createProject = ({
4759
});
4860
} catch (e) {
4961
return err(new ProjectError("failed to create project", { cause: e }));
50-
} finally {
51-
try {
52-
if (startDir) {
53-
process.chdir(startDir);
54-
}
55-
} catch {}
5662
}
5763
};

0 commit comments

Comments
 (0)