1
1
import { mkdir , readFile , writeFile } from 'node:fs/promises'
2
2
import { existsSync , statSync } from 'node:fs'
3
3
import { basename , dirname , resolve } from 'node:path'
4
- import chalk from 'chalk'
5
4
import { execa , execaSync } from 'execa'
6
- import {
7
- cancel ,
8
- confirm ,
9
- intro ,
10
- isCancel ,
11
- log ,
12
- outro ,
13
- spinner ,
14
- } from '@clack/prompts'
15
5
16
6
import { CONFIG_FILE } from './constants.js'
17
7
import {
@@ -25,7 +15,7 @@ import { readConfigFile, writeConfigFile } from './config-file.js'
25
15
26
16
import type { PersistedOptions } from './config-file.js'
27
17
28
- import type { Framework , Options } from './types.js'
18
+ import type { Environment , Framework , Options } from './types.js'
29
19
30
20
function isDirectory ( path : string ) {
31
21
return statSync ( path ) . isDirectory ( )
@@ -69,22 +59,25 @@ export async function addToApp(
69
59
} : {
70
60
silent ?: boolean
71
61
} = { } ,
62
+ environment : Environment ,
72
63
) {
73
64
const persistedOptions = await readConfigFile ( process . cwd ( ) )
74
65
if ( ! persistedOptions ) {
75
- console . error ( `${ chalk . red ( 'There is no .cta.json file in your project.' ) }
76
-
77
- This is probably because this was created with an older version of create-tsrouter-app.` )
66
+ environment . error (
67
+ 'There is no .cta.json file in your project.' ,
68
+ 'This is probably because this was created with an older version of create-tsrouter-app.' ,
69
+ )
78
70
return
79
71
}
80
72
81
73
if ( ! silent ) {
82
- intro ( `Adding ${ addOns . join ( ', ' ) } to the project...` )
74
+ environment . intro ( `Adding ${ addOns . join ( ', ' ) } to the project...` )
83
75
}
84
76
85
77
if ( await hasPendingGitChanges ( ) ) {
86
- log . error (
87
- `${ chalk . red ( 'You have pending git changes.' ) } Please commit or stash them before adding add-ons.` ,
78
+ environment . error (
79
+ 'You have pending git changes.' ,
80
+ 'Please commit or stash them before adding add-ons.' ,
88
81
)
89
82
return
90
83
}
@@ -116,14 +109,12 @@ This is probably because this was created with an older version of create-tsrout
116
109
}
117
110
118
111
if ( overwrittenFiles . length > 0 && ! silent ) {
119
- log . warn (
120
- `${ chalk . yellow ( 'The following will be overwritten:' ) } \n${ overwrittenFiles . join ( '\n' ) } ` ,
112
+ environment . warn (
113
+ 'The following will be overwritten:' ,
114
+ overwrittenFiles . join ( '\n' ) ,
121
115
)
122
- const shouldContinue = await confirm ( {
123
- message : 'Do you want to continue?' ,
124
- } )
125
- if ( isCancel ( shouldContinue ) ) {
126
- cancel ( 'Operation cancelled.' )
116
+ const shouldContinue = await environment . confirm ( 'Do you want to continue?' )
117
+ if ( ! shouldContinue ) {
127
118
process . exit ( 0 )
128
119
}
129
120
}
@@ -171,7 +162,7 @@ This is probably because this was created with an older version of create-tsrout
171
162
const realEnvironment = createDefaultEnvironment ( )
172
163
writeConfigFile ( realEnvironment , process . cwd ( ) , newOptions )
173
164
174
- const s = silent ? null : spinner ( )
165
+ const s = silent ? null : environment . spinner ( )
175
166
s ?. start ( `Installing dependencies via ${ newOptions . packageManager } ...` )
176
167
await realEnvironment . execute (
177
168
newOptions . packageManager ,
@@ -181,6 +172,6 @@ This is probably because this was created with an older version of create-tsrout
181
172
s ?. stop ( `Installed dependencies` )
182
173
183
174
if ( ! silent ) {
184
- outro ( 'Add-ons added successfully!' )
175
+ environment . outro ( 'Add-ons added successfully!' )
185
176
}
186
177
}
0 commit comments