Skip to content

Commit

Permalink
thank fuck I finally seem to have a repl working
Browse files Browse the repository at this point in the history
  https://nodejs.org/api/repl.html#repl
  TypeStrong/ts-node#1007

  some real jank around ESM vs CommonJS - we seem to have to use .js
  file extensions in src/repl.ts ;; let's check if we CAN use .js
  everywhere in our .ts files

  at least that would be consistent.
  • Loading branch information
davidlee committed Aug 31, 2023
1 parent 52f16d4 commit 3afa686
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 30 deletions.
4 changes: 3 additions & 1 deletion package.json
Expand Up @@ -18,7 +18,9 @@
"prettier-check": "pnpm exec prettier --config .prettierrc -c 'src/**/*.ts' 'test/**/*.ts'",
"prettier-watch": "pnpm exec onchange 'src/**/*.ts' 'test/**/*.ts' -- pnpm exec prettier --config .prettierrc -w {{changed}}",
"prep": "pnpm run prettier & pnpm run lint && pnpm test",
"deps": "pnpm exec depcruise -c .dependency-cruiser.cjs -I src -T dot . | dot -T svg > graph.svg && open -a Arc graph.svg"
"deps": "pnpm exec depcruise -c .dependency-cruiser.cjs -I src -T dot . | dot -T svg > graph.svg && open -a Arc graph.svg",
"ex": "node --loader ts-node/esm src/*.ts",
"repl": "node --loader ts-node/esm src/repl.ts"
},
"husky": {
"hooks": {
Expand Down
57 changes: 28 additions & 29 deletions src/parser.ts
@@ -1,23 +1,16 @@
// import * as R from 'ramda'
// import { parseISO } from 'date-fns'
// import { parseJSON } from 'date-fns/fp'

export function argsFromArgv(argv: string[]): string[] {
return argv.slice(2) // .filter((arg) => !(arg === '--'))
}

enum TokenKind {
Command = 'commands',
Filter = 'filters',
Command = 'commands',
Filter = 'filters',
Modifier = 'modifiers',
Ids = 'filters.ids',
Ids = 'filters.ids',
}

export type CommandConfig = {
name: string
aliases: string[]
expect: TokenKind[]
subcommands: CommandConfig[]
name: string
aliases: string[]
expect: TokenKind[]
subcommands: CommandConfig[]
confirmation?: boolean
}

Expand Down Expand Up @@ -73,7 +66,6 @@ const CommandConfigs: CommandConfig[] = [
]
// no reason it should change
Object.freeze(CommandConfigs)
const defaultCommandName = 'list'

export type CommandConfigList = {
[key: string]: CommandConfig
Expand All @@ -84,15 +76,15 @@ export type TagSet = {
[key: string]: string[]
}

// schema?
// should use a typebox schema?
export type ParsedCommandArgs = {
filters: {
ids: number[]
tags: TagSet
ids: number[]
tags: TagSet
words: string[]
}
modifiers: {
tags: TagSet
tags: TagSet
words: string[]
}
}
Expand All @@ -116,12 +108,12 @@ function buildState(tokens: string[]): State {
command: [],
processedIndices: [],
filters: {
ids: [],
tags: {},
ids: [],
tags: {},
words: [],
},
modifiers: {
tags: {},
tags: {},
words: [],
},
} as State
Expand All @@ -134,19 +126,20 @@ function extractCommand(state: State): ParsedCommand {
return parsed
}

const DefaultCommandName = 'list'

// https://taskwarrior.org/docs/syntax/
// task <filter> <command> <modifications> <miscellaneous>

//
// first, find the first thing that looks like a command
// everything before it is a filter (ids, etc)
// everything after it is a modification

export function parse(tokens: string[]): ParsedCommand {
let state = buildState(tokens)
state = parseCommands(state)

if (state.command.length === 0)
state.command.push(defaultCommandName)
state.command.push(DefaultCommandName)

// how we interpret remaining tokens depends on whether they're
// before or after a command
Expand Down Expand Up @@ -187,16 +180,14 @@ function parseCommands(state: State): State {
// we've previously found a command, but matched no valid subcommand
break
}
return state // TODO rather than mutate the state, return an immutable update
return state // FIXME rather than mutate the state, return an immutable update
}

export function parseArgs(argv: string[]): ParsedCommand {
return parse(argsFromArgv(argv))
}

function commandAliases(
cmds: CommandConfig[] = CommandConfigs,
): CommandConfigList {
function commandAliases(cmds: CommandConfig[]=CommandConfigs): CommandConfigList {
const o: CommandConfigList = {}
cmds.map((c) =>
c.aliases.forEach((alias) => {
Expand Down Expand Up @@ -247,3 +238,11 @@ function recogniseIds(word: string): number[] | null {
}

// function recogniseTags()
// function recognisePriority()
// function recogniseParent()

// utility functions

export function argsFromArgv(argv: string[]): string[] {
return argv.slice(2) // .filter((arg) => !(arg === '--'))
}
28 changes: 28 additions & 0 deletions src/repl.ts
@@ -0,0 +1,28 @@
import * as repl from 'node:repl'
import * as E from './entry.js'
import { Value } from '@sinclair/typebox/value'

const replServer = repl.start('> ')
replServer.context.E = E
replServer.context.Value = Value


// import repl from "repl";
// import ts from "typescript";
// import * as tsnode from "ts-node";

// // Create a ts-node replService
// const replService: tsnode.ReplService = tsnode.createRepl()
// const service = tsnode.create({ ...replService.evalAwarePartialHost })
// service.ts = ts;
// replService.setService(service)

// // create a node-repl server
// const replServer = repl.start({
// prompt: "→º ",
// ignoreUndefined: true,
// eval: replService.nodeEval,
// });

// // setup environment
// replServer.setupHistory(".log", () => {})

0 comments on commit 3afa686

Please sign in to comment.