@@ -3,10 +3,9 @@ import os from "os"
3
3
import path from "path"
4
4
import { parseTimeString } from "@/utils/parse-time-string"
5
5
import { githubConfigFromUrl } from "@/utils/transform-url"
6
+ import { cancel , confirm , intro , isCancel , log , spinner } from "@clack/prompts"
6
7
import chalk from "chalk"
7
8
import { Command } from "commander"
8
- import inquirer from "inquirer"
9
- import ora , { Ora } from "ora"
10
9
import simpleGit from "simple-git"
11
10
import { z } from "zod"
12
11
import { fromError } from "zod-validation-error"
@@ -42,10 +41,13 @@ export const clone = new Command()
42
41
url,
43
42
target,
44
43
branch : options . branch ,
45
- overwrite : options . overwrite || options . force ,
44
+ overwrite : options . overwrite ,
45
+ force : options . force ,
46
46
watch : options . watch ,
47
47
} )
48
48
49
+ options . overwrite = options . overwrite || options . force
50
+
49
51
if ( options . watch ) {
50
52
if ( typeof options . watch === "boolean" ) options . watch = "1m"
51
53
@@ -59,7 +61,7 @@ export const clone = new Command()
59
61
target,
60
62
} )
61
63
62
- console . log (
64
+ intro (
63
65
`${ chalk . bold ( config . owner ) } /${ chalk . bold ( config . repository ) } ${ chalk . green (
64
66
`<${ config . type } :${ config . branch } >` ,
65
67
) } ${
@@ -73,14 +75,9 @@ export const clone = new Command()
73
75
} `,
74
76
) } `
75
77
} `,
76
- "\n" ,
77
78
)
78
79
79
- const spinner = ora ( ) . start ( )
80
-
81
80
try {
82
- spinner . stop ( )
83
-
84
81
const targetPath = path . resolve ( config . target )
85
82
86
83
if ( options . watch ) options . overwrite = true
@@ -100,44 +97,44 @@ export const clone = new Command()
100
97
? "The target directory is not empty. Do you want to overwrite the files?"
101
98
: "The target file already exists. Do you want to overwrite the file?"
102
99
103
- const { overwrite } = await inquirer . prompt ( [
104
- {
105
- type : "confirm" ,
106
- name : "overwrite" ,
107
- message ,
108
- default : false ,
109
- } ,
110
- ] )
100
+ const overwrite = await confirm ( {
101
+ message ,
102
+ } )
103
+
104
+ if ( isCancel ( overwrite ) ) {
105
+ cancel ( "Operation cancelled." )
106
+ process . exit ( 0 )
107
+ }
111
108
112
109
if ( ! overwrite ) {
113
- throw new Error ( "Chose not to overwrite files" )
110
+ log . info ( "Chose not to overwrite files." )
111
+ process . exit ( 0 )
114
112
}
115
113
116
- spinner . info (
117
- "You can use -o | --overwrite or -f | --force flag to skip this prompt" ,
114
+ log . info (
115
+ "You can use -o | --overwrite or -f | --force flag to skip this prompt next time. " ,
118
116
)
119
117
}
120
118
121
- await cloneAction ( spinner , config , options , targetPath )
119
+ await cloneAction ( config , options , targetPath )
122
120
123
121
if ( options . watch ) {
124
122
const watchInterval = parseTimeString ( options . watch )
125
123
setInterval (
126
- async ( ) => await cloneAction ( spinner , config , options , targetPath ) ,
124
+ async ( ) => await cloneAction ( config , options , targetPath ) ,
127
125
watchInterval ,
128
126
)
129
127
}
130
128
} catch ( err ) {
131
- console . log ( "\n" )
132
- spinner . fail ( "Level 1: An error occurred" )
129
+ log . error ( "Level 1: An error occurred" )
133
130
134
131
if ( err instanceof z . ZodError ) {
135
132
const validationError = fromError ( err )
136
- console . error ( "\nValidation Error: " + validationError . toString ( ) )
133
+ log . error ( "Validation Error: " + validationError . toString ( ) )
137
134
} else if ( err instanceof Error ) {
138
- console . error ( "\nError : " + err . message )
135
+ log . error ( "Error : " + err . message )
139
136
} else {
140
- console . error ( "\nUnexpected Error: " + JSON . stringify ( err , null , 2 ) )
137
+ log . error ( "Unexpected Error: " + JSON . stringify ( err , null , 2 ) )
141
138
}
142
139
143
140
process . exit ( 1 )
@@ -163,7 +160,6 @@ async function copyDir(src: string, dest: string) {
163
160
}
164
161
165
162
const cloneAction = async (
166
- spinner : Ora ,
167
163
config : {
168
164
token : string
169
165
owner : string
@@ -177,7 +173,11 @@ const cloneAction = async (
177
173
} ,
178
174
targetPath : string ,
179
175
) => {
176
+ const s = spinner ( )
177
+
180
178
try {
179
+ const start = performance . now ( )
180
+
181
181
const git = simpleGit ( )
182
182
183
183
if ( process . platform === "win32" ) {
@@ -191,7 +191,7 @@ const cloneAction = async (
191
191
)
192
192
193
193
if ( ! options . watch )
194
- spinner . start (
194
+ s . start (
195
195
`Picking ${ config . type } ${ config . type === "repository" ? " without .git" : " from repository" } ` ,
196
196
)
197
197
@@ -206,6 +206,7 @@ const cloneAction = async (
206
206
const sourcePath = path . join ( tempDir , config . path )
207
207
208
208
const sourceStat = await fs . promises . stat ( sourcePath )
209
+
209
210
if ( sourceStat . isDirectory ( ) ) {
210
211
await fs . promises . mkdir ( targetPath , { recursive : true } )
211
212
await copyDir ( sourcePath , targetPath )
@@ -218,21 +219,24 @@ const cloneAction = async (
218
219
targetPath + "/" + config . path . split ( "/" ) . pop ( ) ,
219
220
)
220
221
}
222
+
221
223
if ( ! options . watch ) {
222
- spinner . succeed (
223
- `Picked ${ config . type } ${ config . type === "repository" ? " without .git!" : " from repository!" } ` ,
224
+ s . stop (
225
+ `Picked ${ config . type } ${ config . type === "repository" ? " without .git!" : " from repository" } in ${ (
226
+ ( performance . now ( ) - start ) /
227
+ 1000
228
+ ) . toFixed ( 2 ) } seconds!`,
224
229
)
225
- } else spinner . succeed ( "Synced at " + new Date ( ) . toLocaleTimeString ( ) )
230
+ } else log . success ( "Synced at " + new Date ( ) . toLocaleTimeString ( ) )
226
231
227
232
await fs . promises . rm ( tempDir , { recursive : true , force : true } )
228
233
} catch ( err ) {
229
- console . log ( "\n" )
230
- spinner . fail ( "Level 2: An error occurred!" )
234
+ s . stop ( "Level 2: An error occurred while cloning!" )
231
235
232
236
if ( err instanceof Error ) {
233
- console . error ( "\nError : " + err . message )
237
+ log . error ( "Error : " + err . message )
234
238
} else {
235
- console . error ( "\nUnexpected Error: " + JSON . stringify ( err , null , 2 ) )
239
+ log . error ( "Unexpected Error: " + JSON . stringify ( err , null , 2 ) )
236
240
}
237
241
238
242
process . exit ( 1 )
0 commit comments