diff --git a/src/index.ts b/src/index.ts index 02942e3..f55f953 100644 --- a/src/index.ts +++ b/src/index.ts @@ -184,10 +184,13 @@ if ("vite" in template) { const runInstance = spawner(template.entry) - process.stdin.setRawMode(true) - process.stdin.resume() + if (process.stdin.isTTY) { + process.stdin.setRawMode(true) + process.stdin.resume() + } process.stdin.on("data", async (key) => { switch (key.toString()) { + case "q\n": case "q": case "\u0003": // ctrl-c codeInstance?.kill() diff --git a/tests/builtin.test.ts b/tests/builtin.test.ts new file mode 100644 index 0000000..1e2628b --- /dev/null +++ b/tests/builtin.test.ts @@ -0,0 +1,88 @@ +import { expect, test } from "bun:test" +import { access, writeFile } from "node:fs/promises" +import { join } from "node:path" + +const testFactory = ( + template: string, + entryFile: string, + initialText: string, +) => { + return async () => { + const child = Bun.spawn( + [ + "bun", + "run", + "src/index.ts", + "--template", + template, + "--no-code", + "--delete", + ], + { + stdin: "pipe", + stdout: "pipe", + }, + ) + + const randomString = Math.random().toString(36).slice(2) + + const reader = child.stdout.getReader() + let chunk = await reader.read() + + let path: string | undefined + + // read the stdout chunkwise + while (chunk.done === false) { + chunk = await reader.read() + if (chunk.value) { + const text = Buffer.from(chunk.value).toString() + + // save the path of the running project + if (text.includes("Project is running at:")) { + ;[, path] = text.split("\n") + } + + // after the default output is printed, update the file + if (text.includes(initialText)) { + expect(text).toContain(initialText) + + if (!path) throw new Error("path not found") + const filePath = join(path, entryFile) + await writeFile(filePath, `console.log("${randomString}")`) + } + + // after the file is updated, check if the output is correct + if (text.includes(randomString)) { + expect(text).toContain(randomString) + break + } + } + } + + child.stdin.write("q\n") + + await child.exited + expect(child.exitCode).toBe(0) + + if (!path) throw new Error("path not found") + expect(() => access(path)).toThrow() + } +} + +test( + "run TypeScript template", + testFactory( + "typescript", + "index.ts", + "Localpen TypeScript template running...", + ), +) + +test( + "run JavaScript template", + testFactory( + "javascript", + "index.js", + "Localpen JavaScript template running...", + ), +) diff --git a/tests/help.test.ts b/tests/help.test.ts index 4fdcef2..7c12132 100644 --- a/tests/help.test.ts +++ b/tests/help.test.ts @@ -42,6 +42,6 @@ test("print help message on wrong template", async () => { await child.exited - expect(text.includes(helpOutput)).toBe(true) - expect(text.includes(" not found")).toBe(true) + expect(text).toContain(helpOutput) + expect(text).toContain(" not found") })