Skip to content

Commit

Permalink
feat(poc): add jsx implementation 4 prompts
Browse files Browse the repository at this point in the history
  • Loading branch information
renejfc committed Mar 25, 2024
1 parent 7929ca4 commit 8526456
Show file tree
Hide file tree
Showing 15 changed files with 122 additions and 214 deletions.
5 changes: 3 additions & 2 deletions biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
"enabled": true,
"rules": {
"recommended": true,
"style": {
"noNonNullAssertion": "off"
"suspicious": {
"noExplicitAny": "off",
"noConfusingVoidType": "off"
}
}
},
Expand Down
Binary file modified bun.lockb
Binary file not shown.
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"url": "https://github.com/renejfc/conmmit.git"
},
"scripts": {
"start": "bun ./src/index.ts",
"start": "NODE_ENV=production bun ./src/index.tsx",
"dev": "bun ./src/index.tsx",
"build": "bun build --target=bun ./src/index.ts --outdir ./build",
"format": "biome format . --write",
"typecheck": "tsc",
Expand All @@ -35,11 +36,11 @@
},
"dependencies": {
"@clack/core": "^0.3.4",
"@clack/prompts": "^0.7.0",
"is-unicode-supported": "^2.0.0",
"picocolors": "^1.0.0"
},
"type": "module",
"bin": {
"conmmit": "./src/index.ts"
"conmmit": "./src/index.tsx"
}
}
69 changes: 0 additions & 69 deletions src/config.ts

This file was deleted.

105 changes: 0 additions & 105 deletions src/index.ts

This file was deleted.

3 changes: 3 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { Text } from '~/lib/prompts/'

const p = <Text message='Test' />
2 changes: 2 additions & 0 deletions src/lib/prompts/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./text"
export * from "./types"
6 changes: 6 additions & 0 deletions src/lib/prompts/runtime/jsx-dev-runtime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import type { Component } from "./types"

export function jsxDEV(component: Component, props: { children: Array<any>, [x: string]: any }): any {
console.log("Parsing JSX props:", props, "\n")
return component(props)
}
5 changes: 5 additions & 0 deletions src/lib/prompts/runtime/jsx-runtime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { Component } from "./types"

export function jsx(component: Component, props: { children: Array<any>, [x: string]: any }): any {
return component(props)
}
1 change: 1 addition & 0 deletions src/lib/prompts/runtime/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type Component = (props: unknown) => any
44 changes: 44 additions & 0 deletions src/lib/prompts/symbols.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import type { State } from "@clack/core"
import isUnicodeSupported from "is-unicode-supported"
import color from "picocolors"

const unicode = isUnicodeSupported()
const s = (c: string, fallback: string) => (unicode ? c : fallback)

export const S = {
STEP_ACTIVE: s("◆", "*"),
STEP_CANCEL: s("■", "x"),
STEP_ERROR: s("▲", "x"),
STEP_SUBMIT: s("◇", "o"),
BAR_START: s("┌", "T"),
BAR: s("│", "|"),
BAR_END: s("└", "—"),
RADIO_ACTIVE: s("●", ">"),
RADIO_INACTIVE: s("○", " "),
CHECKBOX_ACTIVE: s("◻", "[•]"),
CHECKBOX_SELECTED: s("◼", "[+]"),
CHECKBOX_INACTIVE: s("◻", "[ ]"),
PASSWORD_MASK: s("▪", "•"),
BAR_H: s("─", "-"),
CORNER_TOP_RIGHT: s("╮", "+"),
CONNECT_LEFT: s("├", "+"),
CORNER_BOTTOM_RIGHT: s("╯", "+"),
INFO: s("●", "•"),
SUCCESS: s("◆", "*"),
WARN: s("▲", "!"),
ERROR: s("■", "x"),
}

export const symbol = (state: State) => {
switch (state) {
case "initial":
case "active":
return color.cyan(S.STEP_ACTIVE)
case "cancel":
return color.red(S.STEP_CANCEL)
case "error":
return color.yellow(S.STEP_ERROR)
case "submit":
return color.green(S.STEP_SUBMIT)
}
}
45 changes: 45 additions & 0 deletions src/lib/prompts/text.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { TextPrompt } from "@clack/core"
import c from "picocolors"
import { S, symbol } from "./symbols"
import type { TextOptions } from "./types"

export function Text(opts: TextOptions) {
return new TextPrompt({
validate: opts.validate,
placeholder: opts.placeholder,
defaultValue: opts.defaultValue,
initialValue: opts.initialValue,
render() {
const title = `${c.gray(S.BAR)}\n${symbol(this.state)} ${opts.message}\n`

const placeholder = opts.placeholder
? c.inverse(opts.placeholder[0]) + c.dim(opts.placeholder.slice(1))
: c.inverse(c.hidden("_"))

const value = !this.value ? placeholder : this.valueWithCursor

switch (this.state) {
case "error":
return (
`${title.trim()}\n${c.yellow(S.BAR)} ${value}\n` +
`${c.yellow(S.BAR_END)} ${c.yellow(this.error)}\n`
)

case "submit":
return `${title}${c.gray(S.BAR)} ${c.dim(this.value || opts.placeholder)}`

case "cancel": {
const canceledValue = c.strikethrough(c.dim(this.value ?? ""))
const hasTrimmedValue = this.value?.trim()
return (
`${title}${c.gray(S.BAR)} ${canceledValue}` +
`${hasTrimmedValue ? `\n${c.gray(S.BAR)}` : ""}`
)
}

default:
return `${title}${c.cyan(S.BAR)} ${value}\n${c.cyan(S.BAR_END)}\n`
}
},
}).prompt()
}
7 changes: 7 additions & 0 deletions src/lib/prompts/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export type TextOptions = {
message: string
placeholder?: string
defaultValue?: string
initialValue?: string
validate?: (value: string) => string | void
}
33 changes: 0 additions & 33 deletions src/types.ts

This file was deleted.

4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"module": "ESNext",
"moduleDetection": "force",
"jsx": "react-jsx",
"jsxImportSource": "~/lib/prompts/runtime",
"allowJs": true,

// Bundler mode
Expand All @@ -26,9 +27,8 @@

// Custom
"baseUrl": ".",
"resolveJsonModule": true,
"paths": {
"~/*": ["src/*"]
}
},
}
}

0 comments on commit 8526456

Please sign in to comment.