Skip to content

Commit

Permalink
feat: add remote mode
Browse files Browse the repository at this point in the history
  • Loading branch information
leedom92 committed Apr 4, 2024
1 parent 2293f75 commit f1f0c1d
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 22 deletions.
61 changes: 41 additions & 20 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@

import path from 'node:path'
import process from 'node:process'
import { exec } from 'node:child_process'
import { intro, note, outro, select, spinner, text } from '@clack/prompts'
import { intro, select, text } from '@clack/prompts'
import color from 'picocolors'
import degit from 'degit'
import { banner, info } from './utils/intro'
import { checkDuplicateDir } from './utils/checkDuplicateDir'
import { stinger } from './utils/stinger'
import { choices } from './utils/choices'
import { bugs } from './package.json'
import { onCancel } from './utils/clack'
import { download } from './utils/download'

async function init() {
console.clear()
Expand All @@ -36,6 +34,39 @@ async function init() {
}) as string
onCancel(name)

const operate = await select({
message: 'Select operation:',
options: choices['operate'],
}) as string
onCancel(operate)

operate === 'template' ? defaultAction(name, operate) : remoteRepo(name, operate)
}

// select remote repo
async function remoteRepo(projectName: string, clackType: string) {
const repoLink = await text({
message: 'Input the repo link you want:',
placeholder: 'leedom92/vue-h5-template',
validate: (value) => {
if (!value) {
return 'Please input the repo link!'
}
},
}) as string
onCancel(repoLink)

const directory: string = path.resolve(process.cwd(), path.join(projectName || '.', ''))
await download({
url: repoLink,
projectName,
clackType,
message: `Please refer to ${color.underline(color.cyan(`${directory}/README.md`))} to start the project.`,
})
}

// select default template
async function defaultAction(projectName: string, clackType: string) {
const type = await select({
message: 'Select template type:',
options: choices['type'],
Expand All @@ -48,22 +79,12 @@ async function init() {
}) as string
onCancel(url)

const s = spinner()
s.start('Downloading')

const emitter = degit(url, {
cache: false,
force: true,
verbose: true,
})
const target: string = path.join(name || '.', '')

emitter.clone(target).then(async () => {
const directory = path.resolve(process.cwd(), path.join('.', target))
await exec('git init', { cwd: directory })
s.stop(color.green(('Succeed!')))
note(`cd ${target}\npnpm install\npnpm dev`, 'Next steps.')
outro(`Problems? ${color.underline(color.cyan(`${bugs.url}`))}`)
const target: string = path.join(projectName || '.', '')
await download({
url,
projectName,
clackType,
message: `cd ${target}\npnpm install\npnpm dev`,
})
}

Expand Down
4 changes: 2 additions & 2 deletions utils/choices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import type { ChoiceList } from './types'

export const choices: ChoiceList = {
operate: [
{ label: '默认模版', value: 'default' },
{ label: '远程仓库', value: 'repo' },
{ label: 'Template', value: 'template' },
{ label: 'Remote', value: 'remote' },
],
type: [
{ label: 'Mobile', value: 'mobile' },
Expand Down
28 changes: 28 additions & 0 deletions utils/download.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import path from 'node:path'
import process from 'node:process'
import { exec } from 'node:child_process'
import { log, note, outro, spinner } from '@clack/prompts'
import color from 'picocolors'
import degit from 'degit'
import { bugs } from '../package.json'
import type { NoteOption } from './types'

export async function download({ url, projectName, clackType = 'template', message, title = 'Next steps.' }: NoteOption) {
const loading = spinner()
loading.start('Downloading')

const emitter = await degit(url, {
cache: false,
force: true,
verbose: true,
})
const target: string = path.join(projectName || '.', '')

emitter.clone(target).then(async () => {
const directory = path.resolve(process.cwd(), path.join('.', target))
await exec('git init', { cwd: directory })
loading.stop(color.green(('Succeed!')))
clackType === 'template' ? note(message, title) : log.step(message)
outro(`Problems? ${color.underline(color.cyan(`${bugs.url}`))}`)
})
}
11 changes: 11 additions & 0 deletions utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,14 @@ export interface Choice {
export interface ChoiceList {
[key: string]: Choice[]
}

/**
* download params contain clack note
*/
export interface NoteOption {
url: string
projectName: string
clackType: string
message?: string
title?: string
}

0 comments on commit f1f0c1d

Please sign in to comment.