From 2af211f1bd6a5c81ee7acd2ded2bfacf4dcfb2a4 Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Mon, 9 Jan 2023 19:38:28 +0530 Subject: feat: cli donezy --- src/context.ts | 14 ++++++-------- src/eval.ts | 4 ++++ src/index.ts | 34 +++++++++++++++++++++++----------- 3 files changed, 33 insertions(+), 19 deletions(-) (limited to 'src') 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 => 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' - -const main = async () => { - const ctx = createContext() - const resultType = ctx.entryPoint.getType() - const effects = resultType.isTuple() - ? resultType.getTupleElements() - : [resultType] - await evalList(ctx, effects) +import { cleanup, evalList } from './eval' + +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('', '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()) -- cgit v1.3.1