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.
diff --git a/src/index.ts b/src/index.ts
index fbd00d4..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;
}
@@ -12,24 +14,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 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))}';
- type Input = ${JSON.stringify(input.toString('binary'))};
- type Output = Main;
- `),
- ]);
+ 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: [path.join(tmpDirPath, callerFileName)],
+ rootNames: [callerFileName],
options: {
strict: true,
},
});
- const source = program.getSourceFile(path.join(tmpDirPath, callerFileName))!;
+ const source = program.getSourceFile(callerFileName)!;
const checker = program.getTypeChecker();
const outputList: Buffer[] = [];
@@ -47,11 +44,7 @@ export async function run(fileName: string, { input = Buffer.of() }: Partial