-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathnew.ts
61 lines (55 loc) · 1.38 KB
/
new.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
// Create a new worker by template
import path from 'path'
import type { QuestionCollection } from 'inquirer'
import inquirer from 'inquirer'
import { parseArgs, renderTemplate } from './utils'
interface Args {
name: boolean
date: string
dest: string
}
const args = parseArgs<Args>({
maps: {
n: 'name',
d: 'date',
o: 'dest',
},
})
// worker 名称
const workerName = process.argv[2] || args.name
// worker 根目录
const workersRoot = 'workers'
const questions: QuestionCollection = [
{
type: 'input',
name: 'name',
message: 'Worker name:',
validate: (input: string) => {
if (!input)
return 'Worker name is required'
if (!/^[a-z0-9-]+$/.test(input))
return 'Worker name must be lowercase letters, numbers, and dashes'
return true
},
default: args.name,
when: () => !workerName,
},
{
type: 'input',
name: 'date',
message: 'Date:',
default: args.date || new Date().toISOString().split('T')[0],
},
{
type: 'input',
name: 'dest',
message: 'Destination:',
default: args.dest || workersRoot,
},
]
const answers = await inquirer.prompt(questions)
answers.name = answers.name || workerName
const root = process.cwd()
const templateRoot = path.resolve(root, 'template')
const destRoot = path.resolve(root, answers.dest, answers.name)
renderTemplate(templateRoot, destRoot, answers)