Skip to content

Commit

Permalink
feat(JS): Interpreter now requires command
Browse files Browse the repository at this point in the history
Running the interpreter.ts from the CLI now requires a command as the
first argument, either 'execute' or 'compile'
  • Loading branch information
beneboy committed Sep 4, 2019
1 parent 5b791d5 commit d9d275f
Showing 1 changed file with 30 additions and 14 deletions.
44 changes: 30 additions & 14 deletions ts/interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ interface StringDict {
}

interface CliArgs {
command: string

inputFile: string

outputFile: string
Expand All @@ -78,29 +80,42 @@ export type ExecutableCode = CodeChunkExecution | CodeExpression
* Process argv to get the source and destination for the document being executed
*/
function getCliArgs(): CliArgs {
const { _, ...params } = minimist(process.argv.slice(2), {})
const { _, ...parameterValues } = minimist(process.argv.slice(2), {})

if (_.length === 0) throw new Error('No command supplied')

const command = _[0]

switch (command) {
case 'execute':
case 'compile':
break
default:
throw new Error(`Unknown command ${command}`)
}

let inFile = '-'
let outFile = '-'
let inputFile = '-'
let outputFile = '-'

if (_.length === 0) {
if (_.length === 1) {
// read from stdin and output to stdout
}

if (_.length >= 1) {
// read from provided file arg which might be -, output to stdout
inFile = _[0]
if (_.length >= 2) {
// read from provided file arg (which might be -), then output to stdout
inputFile = _[1]

if (_.length >= 2) {
// read from file, out to file. but they might be -
outFile = _[1]
if (_.length >= 3) {
// read from file, output to a file. but the args might both be -
outputFile = _[2]
}
}

return {
inputFile: inFile,
outputFile: outFile,
parameterValues: params
command,
inputFile,
outputFile,
parameterValues
}
}

Expand Down Expand Up @@ -1030,7 +1045,8 @@ async function main(): Promise<void> {

parseItem(article, parameters, code)

execute(code, decodeParameters(parameters, cliArgs.parameterValues))
if (cliArgs.command === 'execute')
execute(code, decodeParameters(parameters, cliArgs.parameterValues))

outputArticle(cliArgs.outputFile, article)
}

0 comments on commit d9d275f

Please sign in to comment.