-
Notifications
You must be signed in to change notification settings - Fork 28
/
prompts.mjs
128 lines (125 loc) · 3.17 KB
/
prompts.mjs
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
"use strict";
import { git } from "./git.mjs";
import path from "path";
import prompt from "prompt";
// Configure prompt
prompt.message = " 👉 ";
prompt.delimiter = "";
// Utility function to convert first letter to uppercase.
function upperCaseFirstLetter(word) {
if (typeof word !== "string") {
throw new TypeError("Expected string");
}
if (!word) {
return word;
}
return word.charAt(0).toUpperCase() + word.slice(1);
}
// User prompt tasks
export const Prompts = {
askQuestion(promptOps) {
return new Promise((resolve, reject) => {
prompt.get(promptOps, (err, res) => {
if (err) {
return reject(new Error(" 🙅 User canceled."));
}
resolve(res.question);
});
});
},
askRepoName() {
const promptOps = {
description: "Name of Git repository:",
default: path.basename(process.cwd()),
type: "string",
before(value) {
return value.trim();
},
};
return this.askQuestion(promptOps);
},
askProjectName(repo) {
const promptOps = {
description: "Name of project:",
default: `The ${upperCaseFirstLetter(repo)} API`,
type: "string",
before(value) {
return value.trim();
},
};
return this.askQuestion(promptOps);
},
async askUserName() {
const user = await git.getConfigData("config user.name");
const promptOps = {
description: "Name of Primary Editor of the spec:",
default: user.trim(),
type: "string",
before(value) {
return value.trim();
},
};
return this.askQuestion(promptOps);
},
askAffiliation(hint = "") {
const promptOps = {
description: `Company affiliation(e.g., ${
upperCaseFirstLetter(hint) || "Monsters"
} Inc.):`,
default: upperCaseFirstLetter(hint),
type: "string",
before(value) {
return value.trim();
},
};
return this.askQuestion(promptOps);
},
askAffiliationURL(emailHint = "") {
const [, hint] = emailHint.match(/(?:@)(.+)/);
const promptOps = {
description: "Company URL:",
type: "string",
before(value) {
return value.trim();
},
};
if (hint) {
promptOps.default = `https://${hint}`;
}
return this.askQuestion(promptOps);
},
async askEmail() {
const email = await git.getConfigData("config user.email");
const promptOps = {
description: "Email (optional):",
default: email.trim(),
type: "string",
};
return this.askQuestion(promptOps);
},
askWhichGitBranch() {
const promptOps = {
description: "Main git branch for the spec:",
default: "main",
pattern: /^[\w\-]+$/,
type: "string",
message: "Name must be only letters and dashes",
before(value) {
return value.trim();
},
};
return this.askQuestion(promptOps);
},
askWhichPreProcessor() {
const promptOps = {
description: "Spec preprocessor (respec or bikeshed):",
default: "respec",
type: "string",
pattern: /^(respec|bikeshed|bs)$/i,
before(value) {
return value.trim().toLowerCase();
},
};
return this.askQuestion(promptOps);
},
};