Skip to content

Commit f1f0c1d

Browse files
committed
feat: add remote mode
1 parent 2293f75 commit f1f0c1d

File tree

4 files changed

+82
-22
lines changed

4 files changed

+82
-22
lines changed

index.ts

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,14 @@
22

33
import path from 'node:path'
44
import 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'
76
import color from 'picocolors'
8-
import degit from 'degit'
97
import { banner, info } from './utils/intro'
108
import { checkDuplicateDir } from './utils/checkDuplicateDir'
119
import { stinger } from './utils/stinger'
1210
import { choices } from './utils/choices'
13-
import { bugs } from './package.json'
1411
import { onCancel } from './utils/clack'
12+
import { download } from './utils/download'
1513

1614
async 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

utils/choices.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import type { ChoiceList } from './types'
22

33
export const choices: ChoiceList = {
44
operate: [
5-
{ label: '默认模版', value: 'default' },
6-
{ label: '远程仓库', value: 'repo' },
5+
{ label: 'Template', value: 'template' },
6+
{ label: 'Remote', value: 'remote' },
77
],
88
type: [
99
{ label: 'Mobile', value: 'mobile' },

utils/download.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import path from 'node:path'
2+
import process from 'node:process'
3+
import { exec } from 'node:child_process'
4+
import { log, note, outro, spinner } from '@clack/prompts'
5+
import color from 'picocolors'
6+
import degit from 'degit'
7+
import { bugs } from '../package.json'
8+
import type { NoteOption } from './types'
9+
10+
export async function download({ url, projectName, clackType = 'template', message, title = 'Next steps.' }: NoteOption) {
11+
const loading = spinner()
12+
loading.start('Downloading')
13+
14+
const emitter = await degit(url, {
15+
cache: false,
16+
force: true,
17+
verbose: true,
18+
})
19+
const target: string = path.join(projectName || '.', '')
20+
21+
emitter.clone(target).then(async () => {
22+
const directory = path.resolve(process.cwd(), path.join('.', target))
23+
await exec('git init', { cwd: directory })
24+
loading.stop(color.green(('Succeed!')))
25+
clackType === 'template' ? note(message, title) : log.step(message)
26+
outro(`Problems? ${color.underline(color.cyan(`${bugs.url}`))}`)
27+
})
28+
}

utils/types.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,14 @@ export interface Choice {
2222
export interface ChoiceList {
2323
[key: string]: Choice[]
2424
}
25+
26+
/**
27+
* download params contain clack note
28+
*/
29+
export interface NoteOption {
30+
url: string
31+
projectName: string
32+
clackType: string
33+
message?: string
34+
title?: string
35+
}

0 commit comments

Comments
 (0)