-
-
Notifications
You must be signed in to change notification settings - Fork 794
/
confirm.ts
63 lines (51 loc) · 1.64 KB
/
confirm.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import path from 'path'
import prompts from 'prompts'
import { file } from './core'
import { Context } from './types'
/**
* Confirm destination.
*/
export default async (ctx: Context): Promise<void> => {
// resolve dest path
ctx.dest = path.resolve(ctx.project)
// exist
const exists = await file.exists(ctx.dest)
// dist not exists
if (exists === false) return
// force mode
if (ctx.options.force != null && ctx.options.force) {
return await file.remove(ctx.dest)
}
// destination is file
if (exists !== 'dir') throw new Error(`Cannot create ${ctx.project}: File exists.`)
// is empty dir
if (await file.isEmpty(ctx.dest)) return
// is current working directory
const isCurrent = ctx.dest === process.cwd()
// // require node >= v8.3.0
// console.clear()
// confirm & choose next
const { choose }: { choose?: string } = await prompts([
{
name: 'sure',
type: 'confirm',
message: isCurrent ? 'Create in current directory?' : 'Target directory already exists. Continue?'
},
{
name: 'choose',
type: (prev: boolean) => prev ? 'select' : null,
message: `${isCurrent ? 'Current' : 'Target'} directory is not empty. How to continue?`,
hint: ' ',
choices: [
{ title: 'Merge', value: 'merge' },
{ title: 'Overwrite', value: 'overwrite' },
{ title: 'Cancel', value: 'cancel' }
]
}
])
// Otherwise is cancel task
if (choose == null || choose === 'cancel') throw new Error('You have cancelled this task.')
// Overwrite require empty dest
if (choose === 'overwrite') await file.remove(ctx.dest)
// Merge not require any action
}