diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/context.ts | 14 | ||||
| -rw-r--r-- | src/eval.ts | 4 | ||||
| -rw-r--r-- | src/index.ts | 32 |
3 files changed, 32 insertions, 18 deletions
diff --git a/src/context.ts b/src/context.ts index 7759215..e0c0fa5 100644 --- a/src/context.ts +++ b/src/context.ts @@ -5,7 +5,11 @@ import { Ctx } from './types' const RESULT_TYPE_NAME = '__$result' -export const createContext = (): Ctx => { +export interface CtxOptions { + filePath: string +} + +export const createContext = (options: CtxOptions): Ctx => { const project = new Project({ compilerOptions: { target: ScriptTarget.ES3, @@ -14,13 +18,7 @@ export const createContext = (): Ctx => { const typeChecker = project.getTypeChecker() - const [filePath] = process.argv.slice(2) - - if (!filePath) { - throw new Error('Must specify ts file to run') - } - - const sourceFile = project.addSourceFileAtPath(path.resolve(filePath)) + const sourceFile = project.addSourceFileAtPath(path.resolve(options.filePath)) const entryPoint = sourceFile.getExportedDeclarations().get('main')?.[0] diff --git a/src/eval.ts b/src/eval.ts index dc33fbe..6b9ade0 100644 --- a/src/eval.ts +++ b/src/eval.ts @@ -11,6 +11,10 @@ const rl = readline.createInterface({ terminal: false, }) +export const cleanup = () => { + rl.close() +} + const readLineFromStdin = (): Promise<string> => new Promise((res) => rl.on('line', res)) diff --git a/src/index.ts b/src/index.ts index 65349fc..01420c8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,15 +1,27 @@ +import { program } from 'commander' import { createContext } from './context' -import { evalList } from './eval' +import { cleanup, evalList } from './eval' -const main = async () => { - const ctx = createContext() - const resultType = ctx.entryPoint.getType() - const effects = resultType.isTuple() - ? resultType.getTupleElements() - : [resultType] - await evalList(ctx, effects) +const main = () => { + program + .name('ts-types-lang') + .description(`A runtime for typescript's type system that turns it into a general purpose, purely functional programming language!`) + + program + .command('run') + .description('Run a typescript .ts file') + .argument('<file>', 'Typescript file to run') + .action(async (filePath) => { + const ctx = createContext({ filePath }) + const resultType = ctx.entryPoint.getType() + const effects = resultType.isTuple() + ? resultType.getTupleElements() + : [resultType] + await evalList(ctx, effects) + }) + + return program.parseAsync() } main() - .then(() => process.exit(0)) - .catch((e) => (console.error(e), process.exit(1))) + .finally(() => cleanup()) |
