Skip to content

Commit

Permalink
Use readline to read input for the REPL loop
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesmengo committed Jul 23, 2024
1 parent 57638df commit f410f52
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
2 changes: 2 additions & 0 deletions packages/theme/src/cli/services/console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,7 @@ export async function initializeRepl(
storefrontPassword: password,
expiresAt: new Date(),
}
process.stdin.ref()

return replLoop(themeSession, themeId, url)
}
42 changes: 29 additions & 13 deletions packages/theme/src/cli/utilities/repl.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,40 @@
import {DevServerSession} from './theme-environment/types.js'
import {presentValue} from './repl/presenter.js'
import {evaluate} from './repl/evaluater.js'
import {presentValue} from './repl/presenter.js'
import {AbortSilentError} from '@shopify/cli-kit/node/error'
import {consoleWarn, outputContent, outputDebug, outputInfo, outputToken} from '@shopify/cli-kit/node/output'
import {renderTextPrompt} from '@shopify/cli-kit/node/ui'
import {createInterface} from 'readline'

export async function replLoop(themeSession: DevServerSession, themeId: string, url: string) {
try {
const inputValue = await renderTextPrompt({message: 'Enter a value'})
if (hasDelimiter(inputValue)) {
consoleWarn(
"Liquid Console doesn't support Liquid delimiters such as '{{ ... }}' or '{% ... %}'.\nPlease use 'collections.first' instead of '{{ collections.first }}'.",
)
return replLoop(themeSession, themeId, url)
}

const evaluatedValue = await evaluate(themeSession, inputValue, themeId, url)
presentValue(evaluatedValue)
process.stdin.ref()

return replLoop(themeSession, themeId, url)
const rl = createInterface({
input: process.stdin,
output: process.stdout,
})
rl.on('line', (inputValue) => {
try {
if (hasDelimiter(inputValue)) {
consoleWarn(
"Liquid Console doesn't support Liquid delimiters such as '{{ ... }}' or '{% ... %}'.\nPlease use 'collections.first' instead of '{{ collections.first }}'.",
)
return rl.prompt()
}
evaluate(themeSession, inputValue, themeId, url)
.then((evaluatedValue) => {
presentValue(evaluatedValue)
return rl.prompt()
})
.catch((error) => {
throw error
})
} catch (error) {
shutdownReplSession(error)
throw new AbortSilentError()
}
})
rl.prompt()
} catch (error) {
shutdownReplSession(error)
throw new AbortSilentError()
Expand Down

0 comments on commit f410f52

Please sign in to comment.