Skip to content
Source
Sign inSign up
TanStack/cli
Open History to see the last commit

ui-environment.ts

74 lines1.7 KB

1import {
2 cancel,
3 confirm,
4 intro,
5 isCancel,
6 log,
7 outro,
8 spinner,
9} from '@clack/prompts'
10import chalk from 'chalk'
11​
12import { createDefaultEnvironment } from '@tanstack/cta-engine'
13​
14import type { Environment } from '@tanstack/cta-engine'
15​
16export function createUIEnvironment(
17 appName: string,
18 silent: boolean,
19): Environment {
20 const defaultEnvironment = createDefaultEnvironment()
21​
22 let newEnvironment = {
23 ...defaultEnvironment,
24 appName,
25 }
26​
27 if (!silent) {
28 newEnvironment = {
29 ...newEnvironment,
30 intro: (message: string) => {
31 intro(message)
32 },
33 outro: (message: string) => {
34 outro(message)
35 },
36 info: (title?: string, message?: string) => {
37 log.info(
38 `${title ? chalk.red(title) : ''}${message ? '\n' + chalk.green(message) : ''}`,
39 )
40 },
41 error: (title?: string, message?: string) => {
42 log.error(
43 `${title ? `${title}: ` : ''}${message ? '\n' + message : ''}`,
44 )
45 },
46 warn: (title?: string, message?: string) => {
47 log.warn(`${title ? `${title}: ` : ''}${message ? '\n' + message : ''}`)
48 },
49 confirm: async (message: string) => {
50 const shouldContinue = await confirm({
51 message,
52 })
53 if (isCancel(shouldContinue)) {
54 cancel('Operation cancelled.')
55 process.exit(0)
56 }
57 return shouldContinue
58 },
59 spinner: () => {
60 const s = spinner()
61 return {
62 start: (message: string) => {
63 s.start(message)
64 },
65 stop: (message: string) => {
66 s.stop(message)
67 },
68 }
69 },
70 }
71 }
72​
73 return newEnvironment