Skip to content

Commit 822f7cf

Browse files
committed
feat: update createProject
1 parent 011232f commit 822f7cf

File tree

4 files changed

+45
-21
lines changed

4 files changed

+45
-21
lines changed

src/create-project.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ import fs from "node:fs/promises";
33
import { join } from "pathe";
44
import { temporaryDirectoryTask } from "tempy";
55
import { expect, test } from "vitest";
6-
import { createProject, type CreateProjectOptions } from "./create-project.ts";
6+
import { createProject } from "./create-project.ts";
7+
import type { CreateProjectOptions } from "./types.ts";
78

8-
const _createProject = (options: CreateProjectOptions) => Effect.runPromise(createProject(options));
9+
const _createProject = (opts: CreateProjectOptions) => Effect.runPromise(createProject(opts));
910

1011
test("no cwd", async () => {
1112
await temporaryDirectoryTask(async (dir) => {

src/create-project.ts

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,40 @@
1-
import { Data, Effect } from "effect";
1+
import { Effect } from "effect";
22
import { ModuleKind, ModuleResolutionKind, Project, ScriptTarget } from "ts-morph";
3+
import { ProjectError } from "./errors.ts";
4+
import type { CreateProjectOptions, CreateProjectReturn } from "./types.ts";
35

4-
export type CreateProjectOptions = {
5-
indexFilePath: string;
6-
cwd: string;
7-
};
8-
9-
/** @internal */
10-
export class ProjectError extends Data.TaggedError("ProjectError")<{
11-
readonly cause?: unknown;
12-
}> {}
13-
14-
export const createProject = ({ indexFilePath, cwd }: CreateProjectOptions) =>
15-
Effect.try({
16-
try: () => {
6+
export function createProject({ indexFilePath, cwd }: CreateProjectOptions) {
7+
return Effect.try({
8+
try: (): CreateProjectReturn => {
179
const project = new Project({
1810
compilerOptions: {
11+
// Include esnext types.
1912
// See https://github.com/dsherret/ts-morph/issues/938
2013
// and https://github.com/microsoft/TypeScript/blob/master/lib/lib.esnext.full.d.ts
2114
lib: ["lib.esnext.full.d.ts"],
15+
16+
// Set modern target and module resolutions options.
2217
target: ScriptTarget.ESNext,
2318
module: ModuleKind.ESNext,
2419
moduleResolution: ModuleResolutionKind.Bundler,
25-
// By default, ts-morph creates a project rooted in the current working directory.
26-
// We must change the `typeRoots` directory to the temporary directory
27-
// where the packages are installed, otherwise TypeScript will discover
28-
// `@types` packages from our local `node_modules` directory.
20+
21+
// By default, `ts-morph` creates a project rooted in the current working directory
22+
// (that is, where this library or an app using it is running).
23+
// We must change the `typeRoots` directory to the temporary directory where
24+
// the analyzed package is installed, otherwise TypeScript will discover
25+
// `@types` packages from our local `node_modules` directory giving
26+
// inconsistent analysis results due to available type definitions.
2927
// See https://www.typescriptlang.org/tsconfig#typeRoots.
3028
typeRoots: [cwd],
3129
},
3230
});
31+
32+
// Add index file and resolve module imports.
3333
const indexFile = project.addSourceFileAtPath(indexFilePath);
3434
project.resolveSourceFileDependencies();
35+
3536
return { project, indexFile };
3637
},
37-
catch: (e) => new ProjectError({ cause: e }),
38+
catch: (err) => new ProjectError({ cause: err }),
3839
});
40+
}

src/errors.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@ export class PackageJsonError extends Data.TaggedError("PackageJsonError")<{ cau
1313

1414
/** `PackageTypesError` occurs when the types entry point file for a package cannot be resolved. */
1515
export class PackageTypesError extends Data.TaggedError("PackageTypesError") {}
16+
17+
/** `ProjectError` occurs when the `ts-morph` project cannot be created. */
18+
export class ProjectError extends Data.TaggedError("ProjectError")<{ readonly cause?: unknown }> {}

src/types.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,3 +288,21 @@ export interface WorkDir {
288288
/** Cleanup function to remove the directory. */
289289
close: () => Promise<void>;
290290
}
291+
292+
/** `CreateProjectOptions` contains the options for calling {@link createProject}. */
293+
export interface CreateProjectOptions {
294+
/** Path to the types entry point file. */
295+
indexFilePath: string;
296+
297+
/** Directory where the analyzed package is installed. */
298+
cwd: string;
299+
}
300+
301+
/** `CreateProjectReturn` represents the return value of {@link createProject}. */
302+
export interface CreateProjectReturn {
303+
/** `Project` created with `ts-morph`. */
304+
project: Project;
305+
306+
/** `SourceFile` created with `ts-morph` representing the index file. */
307+
indexFile: SourceFile;
308+
}

0 commit comments

Comments
 (0)