22
33import path from 'node:path'
44import process from 'node:process'
5- import { exec } from 'node:child_process'
6- import { intro , note , outro , select , spinner , text } from '@clack/prompts'
5+ import { intro , select , text } from '@clack/prompts'
76import color from 'picocolors'
8- import degit from 'degit'
97import { banner , info } from './utils/intro'
108import { checkDuplicateDir } from './utils/checkDuplicateDir'
119import { stinger } from './utils/stinger'
1210import { choices } from './utils/choices'
13- import { bugs } from './package.json'
1411import { onCancel } from './utils/clack'
12+ import { download } from './utils/download'
1513
1614async function init ( ) {
1715 console . clear ( )
@@ -36,6 +34,39 @@ async function init() {
3634 } ) as string
3735 onCancel ( name )
3836
37+ const operate = await select ( {
38+ message : 'Select operation:' ,
39+ options : choices [ 'operate' ] ,
40+ } ) as string
41+ onCancel ( operate )
42+
43+ operate === 'template' ? defaultAction ( name , operate ) : remoteRepo ( name , operate )
44+ }
45+
46+ // select remote repo
47+ async function remoteRepo ( projectName : string , clackType : string ) {
48+ const repoLink = await text ( {
49+ message : 'Input the repo link you want:' ,
50+ placeholder : 'leedom92/vue-h5-template' ,
51+ validate : ( value ) => {
52+ if ( ! value ) {
53+ return 'Please input the repo link!'
54+ }
55+ } ,
56+ } ) as string
57+ onCancel ( repoLink )
58+
59+ const directory : string = path . resolve ( process . cwd ( ) , path . join ( projectName || '.' , '' ) )
60+ await download ( {
61+ url : repoLink ,
62+ projectName,
63+ clackType,
64+ message : `Please refer to ${ color . underline ( color . cyan ( `${ directory } /README.md` ) ) } to start the project.` ,
65+ } )
66+ }
67+
68+ // select default template
69+ async function defaultAction ( projectName : string , clackType : string ) {
3970 const type = await select ( {
4071 message : 'Select template type:' ,
4172 options : choices [ 'type' ] ,
@@ -48,22 +79,12 @@ async function init() {
4879 } ) as string
4980 onCancel ( url )
5081
51- const s = spinner ( )
52- s . start ( 'Downloading' )
53-
54- const emitter = degit ( url , {
55- cache : false ,
56- force : true ,
57- verbose : true ,
58- } )
59- const target : string = path . join ( name || '.' , '' )
60-
61- emitter . clone ( target ) . then ( async ( ) => {
62- const directory = path . resolve ( process . cwd ( ) , path . join ( '.' , target ) )
63- await exec ( 'git init' , { cwd : directory } )
64- s . stop ( color . green ( ( 'Succeed!' ) ) )
65- note ( `cd ${ target } \npnpm install\npnpm dev` , 'Next steps.' )
66- outro ( `Problems? ${ color . underline ( color . cyan ( `${ bugs . url } ` ) ) } ` )
82+ const target : string = path . join ( projectName || '.' , '' )
83+ await download ( {
84+ url,
85+ projectName,
86+ clackType,
87+ message : `cd ${ target } \npnpm install\npnpm dev` ,
6788 } )
6889}
6990
0 commit comments