main
1// Copyright 2022 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15import process from 'node:process'
16import repl from 'node:repl'
17import { inspect } from 'node:util'
18import { ProcessOutput, defaults, chalk, path, os } from './core.ts'
19
20const HISTORY =
21 process.env.ZX_REPL_HISTORY ?? path.join(os.homedir(), '.zx_repl_history')
22
23export async function startRepl(history = HISTORY) {
24 defaults.verbose = false
25 const r = repl.start({
26 prompt: chalk.greenBright.bold('❯ '),
27 useGlobal: true,
28 preview: false,
29 writer(output: any) {
30 return output instanceof ProcessOutput
31 ? output.toString().trimEnd()
32 : inspect(output, { colors: true })
33 },
34 })
35 r.setupHistory(history, () => {})
36}