Skip to content

Commit b4dae9a

Browse files
committed
Refactor to use individual prompts
1 parent 3ee3b37 commit b4dae9a

File tree

1 file changed

+54
-73
lines changed

1 file changed

+54
-73
lines changed

src/index.ts

Lines changed: 54 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,48 @@
22

33
import * as fs from 'node:fs'
44
import * as path from 'node:path'
5-
import prompts from 'prompts'
5+
import prompts, { type PromptObject } from 'prompts'
66
import ejs from 'ejs'
77

8+
async function prompt(options: Omit<PromptObject, 'name'>) {
9+
try {
10+
const result = await prompts(
11+
{
12+
...options,
13+
name: 'name'
14+
},
15+
{
16+
onCancel: () => {
17+
throw new Error('Cancelled')
18+
}
19+
}
20+
)
21+
22+
return result.name
23+
} catch (cancelled) {
24+
console.log(cancelled.message)
25+
process.exit(1)
26+
}
27+
}
28+
29+
async function textPrompt(message: string, initial?: string): Promise<string> {
30+
return prompt({
31+
type: 'text',
32+
message,
33+
initial
34+
})
35+
}
36+
37+
async function togglePrompt(message: string, initial = false, active = 'Yes', inactive = 'No'): Promise<boolean> {
38+
return prompt({
39+
type: 'toggle',
40+
message,
41+
initial,
42+
active,
43+
inactive
44+
})
45+
}
46+
847
type Config = {
948
scopedPackageName: string
1049
unscopedPackageName: string
@@ -30,84 +69,26 @@ type Config = {
3069
async function init() {
3170
const cwd = process.cwd()
3271

33-
let result: {
34-
packageName: string
35-
githubPath: string
36-
includeDocs: boolean
37-
includeGithubPages?: boolean
38-
includePlayground: boolean
39-
includeExamples: boolean
40-
} = {}
41-
42-
try {
43-
result = <any>await prompts(
44-
[
45-
{
46-
name: 'packageName',
47-
type: 'text',
48-
message: 'Package name',
49-
initial: '@skirtle/test-project'
50-
}, {
51-
name: 'githubPath',
52-
type: 'text',
53-
message: 'GitHub path, e.g. skirtles-code/test-project (optional)',
54-
initial: ''
55-
}, {
56-
name: 'includeDocs',
57-
type: 'toggle',
58-
message: 'Include VitePress for documentation?',
59-
initial: true,
60-
active: 'Yes',
61-
inactive: 'No'
62-
}, {
63-
name: 'includeGithubPages',
64-
type: (prev) => prev ? 'toggle' : null,
65-
message: 'Include GitHub Pages config for documentation?',
66-
initial: false,
67-
active: 'Yes',
68-
inactive: 'No'
69-
}, {
70-
name: 'includePlayground',
71-
type: 'toggle',
72-
message: 'Include playground application for development?',
73-
initial: true,
74-
active: 'Yes',
75-
inactive: 'No'
76-
}, {
77-
name: 'includeExamples',
78-
type: 'toggle',
79-
message: 'Include example code?',
80-
initial: true,
81-
active: 'Yes',
82-
inactive: 'No, just configs'
83-
}
84-
],
85-
{
86-
onCancel: () => {
87-
throw new Error('Cancelled')
88-
},
89-
},
90-
)
91-
} catch (cancelled) {
92-
console.log(cancelled.message)
93-
process.exit(1)
94-
}
95-
96-
const scopedPackageName = result.packageName
72+
const scopedPackageName = await textPrompt('Package name', '@skirtle/test-project')
9773

9874
// TODO: Tightening this check, e.g. for hyphen positions
9975
if (!/@[a-z0-9-]+\/[a-z0-9-]+/.test(scopedPackageName)) {
10076
console.log('Invalid package name: ' + scopedPackageName)
10177
process.exit(1)
10278
}
10379

104-
const githubPath = result.githubPath
80+
const githubPath = await textPrompt('GitHub path, e.g. skirtles-code/test-project (optional)')
10581

10682
if (githubPath && !/[\w-]+\/[\w-]+/.test(githubPath)) {
10783
console.log('Invalid GitHub path: ' + githubPath)
10884
process.exit(1)
10985
}
11086

87+
const includeDocs = await togglePrompt('Include VitePress for documentation?', true)
88+
const includeGithubPages = includeDocs && await togglePrompt('Include GitHub Pages config for documentation?')
89+
const includePlayground = await togglePrompt('Include playground application for development?', true)
90+
const includeExamples = await togglePrompt('Include example code?', true, 'Yes', 'No, just configs')
91+
11192
const unscopedPackageName = scopedPackageName.replace(/.*\//, '')
11293
const shortUnscopedPackageName = unscopedPackageName.replace(/^vue-/, '')
11394
const projectName = unscopedPackageName.replace(/-+/g, ' ').trim().split(' ').map(s => s[0].toUpperCase() + s.slice(1)).join(' ')
@@ -118,9 +99,9 @@ async function init() {
11899
const githubUrl = githubPath ? `https://github.com/${githubPath}` : ''
119100
const githubIssues = githubPath ? `${githubUrl}/issues` : ''
120101
const githubRepository = githubPath ? `git+${githubUrl}.git` : ''
121-
const githubPagesOrigin = githubUserName && result.includeGithubPages ? `https://${githubUserName}.github.io` : ''
122-
const docsBase = githubRepoName && result.includeGithubPages ? `/${githubRepoName}/` : '/'
123-
const homepageUrl = githubPagesOrigin && result.includeGithubPages ? `${githubPagesOrigin}${docsBase}` : githubUrl
102+
const githubPagesOrigin = githubUserName && includeGithubPages ? `https://${githubUserName}.github.io` : ''
103+
const docsBase = githubRepoName && includeGithubPages ? `/${githubRepoName}/` : '/'
104+
const homepageUrl = githubPagesOrigin && includeGithubPages ? `${githubPagesOrigin}${docsBase}` : githubUrl
124105

125106
const targetDirPath = path.join(cwd, targetDirName)
126107

@@ -149,10 +130,10 @@ async function init() {
149130
githubPagesOrigin,
150131
docsBase,
151132
homepageUrl,
152-
includeDocs: result.includeDocs,
153-
includeGithubPages: !!result.includeGithubPages,
154-
includePlayground: result.includePlayground,
155-
includeExamples: result.includeExamples
133+
includeDocs,
134+
includeGithubPages,
135+
includePlayground,
136+
includeExamples
156137
}
157138

158139
copyTemplate('base', config)

0 commit comments

Comments
 (0)