Skip to content

Commit 3be853b

Browse files
committed
fix(cli): create file on disk before adding source in ts-morph project to resolve bun issue
Closes: #536
1 parent daf0c3f commit 3be853b

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import {CliFs, inject} from "@tsed/cli-core";
2+
import {CliPlatformTest, FakeCliFs} from "@tsed/cli-testing";
3+
4+
import {ProjectClient} from "./ProjectClient.js";
5+
6+
describe("ProjectClient", () => {
7+
beforeEach(() =>
8+
CliPlatformTest.bootstrap({
9+
project: {
10+
rootDir: "./project-name",
11+
srcDir: "src"
12+
}
13+
})
14+
);
15+
afterEach(() => CliPlatformTest.reset());
16+
17+
it("should create a ts source file from string content", async () => {
18+
const project = new ProjectClient({
19+
rootDir: "./project-name"
20+
});
21+
22+
const source = await project.createSource("src/Server.ts", "export class Server {}");
23+
24+
expect(source?.getBaseName()).toBe("Server.ts");
25+
expect(source?.getText()).toContain("export class Server");
26+
expect(FakeCliFs.files.get("project-name/src/Server.ts")).toContain("export class Server");
27+
});
28+
29+
it("should create a ts source file from a structure", async () => {
30+
const project = new ProjectClient({
31+
rootDir: "./project-name"
32+
});
33+
34+
const source = await project.createSource("src/config/config.ts", {
35+
statements: ["export const config = {};"]
36+
});
37+
38+
expect(source?.getBaseName()).toBe("config.ts");
39+
expect(source?.getText()).toContain("export const config");
40+
41+
const fs = inject(CliFs);
42+
const content = await fs.readFile("project-name/src/config/config.ts", "utf8");
43+
44+
expect(content).toContain("export const config");
45+
});
46+
});

packages/cli/src/services/ProjectClient.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,16 @@ export class ProjectClient extends Project {
7272
path = join(this.rootDir, path);
7373

7474
if (path.endsWith(".ts") || path.endsWith(".mts")) {
75-
const source = this.createSourceFile(path, sourceFileText, options);
75+
await this.fs.mkdir(dirname(path));
76+
77+
if (typeof sourceFileText === "string") {
78+
await this.fs.writeFile(path, sourceFileText, {encoding: "utf-8"});
7679

77-
await source.save();
80+
return this.getSourceFile(path) || this.addSourceFileAtPath(path);
81+
}
82+
83+
const source = this.createSourceFile(path, sourceFileText, options);
84+
await this.fs.writeFile(path, source.getFullText(), {encoding: "utf-8"});
7885

7986
return source;
8087
}

0 commit comments

Comments
 (0)