From bc3b0d870ab1cb08f72232f28b50ccb38d33148e Mon Sep 17 00:00:00 2001 From: n4o847 <22975590+n4o847@users.noreply.github.com> Date: Mon, 7 Dec 2020 18:29:54 +0900 Subject: [PATCH 1/6] Don't copy `callee` source file --- src/index.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/index.ts b/src/index.ts index fbd00d4..c3c7237 100644 --- a/src/index.ts +++ b/src/index.ts @@ -13,12 +13,10 @@ interface RunResult { export async function run(fileName: string, { input = Buffer.of() }: Partial = {}): Promise { const { path: tmpDirPath } = await tmp.dir({ prefix: `compile-time-typescript` }); - const calleeFileName = `callee.ts`; const callerFileName = `caller.ts`; await Promise.all([ - fs.copyFile(fileName, path.join(tmpDirPath, calleeFileName)), fs.writeFile(path.join(tmpDirPath, callerFileName), ` - import Main from './${path.basename(calleeFileName, path.extname(calleeFileName))}'; + import Main from ${JSON.stringify(path.resolve(fileName).replace(/\.ts$/, ''))}; type Input = ${JSON.stringify(input.toString('binary'))}; type Output = Main; `), @@ -48,7 +46,6 @@ export async function run(fileName: string, { input = Buffer.of() }: Partial Date: Mon, 7 Dec 2020 19:36:57 +0900 Subject: [PATCH 2/6] Fix explanation --- README.md | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index bb939f2..575f991 100644 --- a/README.md +++ b/README.md @@ -30,24 +30,17 @@ Hello, world! ## How It Works -We can write a program like this: +When `ctts script.ts` is executed, compile-time-typescript creates a caller like this: ```typescript -type Main = `Hello, ${Input}\n`; -export default Main; -``` - -Compile-time-typescript saves the program as `callee.ts` and creates `caller.ts`: - -```typescript -import Main from './callee'; +import Main from '/path/to/script'; type Input = "HERE COMES THE INPUT"; type Output = Main; ``` So the program must have a default export of a generic type that takes a type parameter and constructs a string literal type. -Then compile-time-typescript type-checks `caller.ts` and extracts the type information of `Output`. +Then compile-time-typescript type-checks the caller and extracts the type information of `Output`. If `Output` is a string literal type, its content is printed. Otherwise, an error occurs. From 8fb7bae3cf2d71d308074cd04f3261f5a5ac6d22 Mon Sep 17 00:00:00 2001 From: n4o847 <22975590+n4o847@users.noreply.github.com> Date: Mon, 7 Dec 2020 21:48:13 +0900 Subject: [PATCH 3/6] Don't repeat `path.join(~)` --- src/index.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/index.ts b/src/index.ts index c3c7237..f97162c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -14,20 +14,21 @@ interface RunResult { export async function run(fileName: string, { input = Buffer.of() }: Partial = {}): Promise { const { path: tmpDirPath } = await tmp.dir({ prefix: `compile-time-typescript` }); const callerFileName = `caller.ts`; + const callerPath = path.join(tmpDirPath, callerFileName); await Promise.all([ - fs.writeFile(path.join(tmpDirPath, callerFileName), ` + fs.writeFile(callerPath, ` import Main from ${JSON.stringify(path.resolve(fileName).replace(/\.ts$/, ''))}; type Input = ${JSON.stringify(input.toString('binary'))}; type Output = Main; `), ]); const program = ts.createProgram({ - rootNames: [path.join(tmpDirPath, callerFileName)], + rootNames: [callerPath], options: { strict: true, }, }); - const source = program.getSourceFile(path.join(tmpDirPath, callerFileName))!; + const source = program.getSourceFile(callerPath)!; const checker = program.getTypeChecker(); const outputList: Buffer[] = []; @@ -46,7 +47,7 @@ export async function run(fileName: string, { input = Buffer.of() }: Partial Date: Mon, 7 Dec 2020 21:54:59 +0900 Subject: [PATCH 4/6] Remove unnecessary `Promise.all`s --- src/index.ts | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/src/index.ts b/src/index.ts index f97162c..b0b820d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -15,13 +15,11 @@ export async function run(fileName: string, { input = Buffer.of() }: Partial; - `), - ]); + await fs.writeFile(callerPath, ` + import Main from ${JSON.stringify(path.resolve(fileName).replace(/\.ts$/, ''))}; + type Input = ${JSON.stringify(input.toString('binary'))}; + type Output = Main; + `); const program = ts.createProgram({ rootNames: [callerPath], options: { @@ -46,9 +44,7 @@ export async function run(fileName: string, { input = Buffer.of() }: Partial Date: Mon, 7 Dec 2020 22:04:09 +0900 Subject: [PATCH 5/6] Use tmp.file instead of tmp.dir --- src/index.ts | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/index.ts b/src/index.ts index b0b820d..b9ef044 100644 --- a/src/index.ts +++ b/src/index.ts @@ -12,21 +12,19 @@ interface RunResult { } export async function run(fileName: string, { input = Buffer.of() }: Partial = {}): Promise { - const { path: tmpDirPath } = await tmp.dir({ prefix: `compile-time-typescript` }); - const callerFileName = `caller.ts`; - const callerPath = path.join(tmpDirPath, callerFileName); - await fs.writeFile(callerPath, ` + const { path: callerFileName, cleanup } = await tmp.file({ prefix: `compile-time-typescript`, postfix: `caller.ts` }); + await fs.writeFile(callerFileName, ` import Main from ${JSON.stringify(path.resolve(fileName).replace(/\.ts$/, ''))}; type Input = ${JSON.stringify(input.toString('binary'))}; type Output = Main; `); const program = ts.createProgram({ - rootNames: [callerPath], + rootNames: [callerFileName], options: { strict: true, }, }); - const source = program.getSourceFile(callerPath)!; + const source = program.getSourceFile(callerFileName)!; const checker = program.getTypeChecker(); const outputList: Buffer[] = []; @@ -44,8 +42,7 @@ export async function run(fileName: string, { input = Buffer.of() }: Partial Date: Mon, 7 Dec 2020 22:22:20 +0900 Subject: [PATCH 6/6] Use tmp.setGracefulCleanup --- src/index.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/index.ts b/src/index.ts index b9ef044..9ff3956 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,6 +3,8 @@ import { promises as fs } from 'fs'; import * as tmp from 'tmp-promise'; import * as path from 'path'; +tmp.setGracefulCleanup(); + interface RunOptions { input: Buffer; }