UNPKG

14.2 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const tslib_1 = require("tslib");
4const fs = require("fs-extra");
5const lazy_get_decorator_1 = require("lazy-get-decorator");
6const lodash_1 = require("lodash");
7const rl$ = require("readline-sync");
8const IS_CI_1 = require("../const/IS_CI");
9const lastDirname_1 = require("../fns/lastDirname");
10const readJson_1 = require("../fns/readJson");
11const License_1 = require("../inc/License");
12const PackageManager_1 = require("../inc/PackageManager");
13const Colour_1 = require("./Colour");
14const Git_1 = require("./Git");
15const gh_repo_1 = require("./sync-request/gh-repo/gh-repo");
16let rl;
17if (IS_CI_1.IS_CI) {
18 rl = lodash_1.cloneDeep(rl$);
19 function throwError() {
20 throw new Error('Unable to prompt in test environment; Please set full configuration.');
21 }
22 for (const k of Object.keys(rl)) {
23 if (typeof rl[k] === 'function') {
24 rl[k] = throwError;
25 }
26 }
27}
28else {
29 rl = rl$;
30}
31const memoisedFnNames = [];
32function Memo(_target, prop) {
33 memoisedFnNames.push(prop);
34}
35class PromptableConfig {
36 constructor(data) {
37 this.data = data;
38 for (const fn of memoisedFnNames) {
39 this[fn] = lodash_1.memoize(this[fn]);
40 }
41 }
42 /** @internal */
43 static get readline() {
44 return rl;
45 }
46 get ghRepoFromMetadata() {
47 if (this.ghMetadata) {
48 return this.ghMetadata.repo;
49 }
50 return null;
51 }
52 get ghUserFromMetadata() {
53 if (this.ghMetadata) {
54 return this.ghMetadata.user;
55 }
56 return null;
57 }
58 get ghMetadata() {
59 try {
60 if (Git_1.Git.originUrl) {
61 const match = /([a-z0-9-_.]+)\/([a-z0-9-_.]+)\.git/i.exec(Git_1.Git.originUrl);
62 if (match) {
63 return {
64 repo: match[2],
65 user: match[1]
66 };
67 }
68 }
69 return null;
70 }
71 catch (_a) {
72 return null;
73 }
74 }
75 get(k) {
76 return this.data[k];
77 }
78 has(k, strict = true) {
79 return strict ? this.data[k] !== undefined : !!this.data[k];
80 }
81 promptedEmail(prop = 'email') {
82 return this.getPromptEmail(prop, 'What\'s your email? ');
83 }
84 promptedEncryptionPassword(prop = 'password') {
85 return this.getPromptHidden(prop, 'Encryption password: ');
86 }
87 promptedGhEmail(prop = 'ghEmail') {
88 return this.getPromptEmail(prop, 'What\'s GitHub your email? ');
89 }
90 promptedGhRepo(prop = 'ghRepo') {
91 let msg = 'What is your GitHub repo';
92 if (this.has(prop)) {
93 return this.get(prop);
94 }
95 else if (!IS_CI_1.IS_CI && this.ghRepoFromMetadata) {
96 if (rl.keyInYNStrict(`Is your GitHub repo ${Colour_1.Colour.cyan(this.ghRepoFromMetadata)}? `)) {
97 this.data[prop] = this.ghRepoFromMetadata;
98 return this.ghRepoFromMetadata;
99 }
100 else {
101 return this.getPrompt(prop, `${msg} then? `);
102 }
103 }
104 return this.getPrompt(prop, `${msg}? `);
105 }
106 promptedGhToken(prop = 'ghToken') {
107 return this.getPromptHidden(prop, [
108 'What\'s your global GitHub token used only by this CLI tool?',
109 `You can create one here: ${"https://github.com/settings/tokens/new" /* GH_TOK_URL */} `
110 ].join(' '));
111 }
112 promptedGhUser(prop = 'ghUser') {
113 let msg = 'What is your GitHub username';
114 if (this.has(prop)) {
115 return this.get(prop);
116 }
117 else if (!IS_CI_1.IS_CI && this.ghUserFromMetadata) {
118 if (rl.keyInYNStrict(`Is your GitHub username ${Colour_1.Colour.cyan(this.ghUserFromMetadata)}? `)) {
119 this.data[prop] = this.ghUserFromMetadata;
120 return this.ghUserFromMetadata;
121 }
122 else {
123 return this.getPrompt(prop, `${msg} then? `);
124 }
125 }
126 return this.getPrompt(prop, `${msg}? `);
127 }
128 promptedLicense(prop = 'license') {
129 let pjson;
130 if (this.has(prop)) {
131 return this.get(prop);
132 }
133 else if ((pjson = readJson_1.readJson()) && License_1.isLicense(pjson.license)) {
134 this.data[prop] = pjson.license;
135 return pjson.license;
136 }
137 return this.getPromptSelect(prop, 'What license do you with to use? ', License_1.LICENSE_VALUES);
138 }
139 promptedName(prop = 'name') {
140 return this.getPrompt(prop, 'What\'s your name? ');
141 }
142 promptedPkgMgr(prop = 'pkgMgr') {
143 if (this.has(prop)) {
144 return this.get(prop);
145 }
146 const files = fs.readdirSync(process.cwd(), 'utf8');
147 if (files.includes('yarn.lock')) {
148 return PackageManager_1.PackageManager.YARN;
149 }
150 else if (files.includes('package-lock.json')) {
151 return PackageManager_1.PackageManager.NPM;
152 }
153 else {
154 return this.getPromptSelect(prop, 'What package manager do you want to use? ', PackageManager_1.PACKAGE_MANAGERS);
155 }
156 }
157 promptedProjectDescription(prop = 'projectDesc') {
158 if (this.has(prop)) {
159 return this.get(prop);
160 }
161 const tok = this.promptedGhToken();
162 const repo = this.promptedGhRepo();
163 const user = this.promptedGhUser();
164 const ghProjRemote = gh_repo_1.getGhRepoData(tok, user, repo);
165 if (ghProjRemote && ghProjRemote.description) {
166 this.data[prop] = ghProjRemote.description;
167 return this.data[prop];
168 }
169 return this.getPrompt(prop, 'What\'s your project description? ');
170 }
171 promptedProjectKeywords(prop = 'projectKeywords') {
172 return this.getPromptArray(prop, 'What are your project keywords?');
173 }
174 promptedProjectName(prop = 'projectName') {
175 const ask = (opt) => rl.keyInYNStrict(`Is your project name ${Colour_1.Colour.cyan(opt)}? `);
176 let dir;
177 if (this.has(prop)) {
178 return this.get(prop);
179 }
180 else if (this.ghRepoFromMetadata && ask(this.ghRepoFromMetadata)) {
181 this.data[prop] = this.ghRepoFromMetadata;
182 return this.ghRepoFromMetadata;
183 }
184 else if (ask((dir = lastDirname_1.lastDirname()))) {
185 this.data[prop] = dir;
186 return dir;
187 }
188 else {
189 return this.getPrompt(prop, 'What is your project name then? ');
190 }
191 }
192 promptedReleaseGhToken(prop = 'releaseGhToken') {
193 return this.getPromptHidden(prop, [
194 'What\'s your release GitHub token?',
195 `You can create one here: ${"https://github.com/settings/tokens/new" /* GH_TOK_URL */} `
196 ].join(' '));
197 }
198 promptedReleaseNpmToken(prop = 'releaseNpmToken') {
199 return this.getPromptHidden(prop, 'What\'s your release NPM token? ');
200 }
201 promptedTravisTokenOrg(prop = 'travisTokenOrg') {
202 return this.getPromptHidden(prop, 'What\'s your travis-ci token? ');
203 }
204 promptedTravisTokenPro(prop = 'travisTokenPro') {
205 return this.getPromptHidden(prop, 'What\'s your travis-ci token? ');
206 }
207 promptedUserWebsite(prop = 'userWebsite') {
208 return this.getPromptEmail(prop, 'What\'s your name? ');
209 }
210 getPrompt(k, question, forbidEmpty = true, strict = true) {
211 return this.promptCommon(k, () => rl.question(question), forbidEmpty, strict);
212 }
213 getPromptArray(k, question, forbidEmpty = true) {
214 if (!lodash_1.isEmpty(this.get(k))) {
215 return this.get(k);
216 }
217 const out = [];
218 const run = () => {
219 console.log(question);
220 console.log('Enter an empty line when done');
221 let response;
222 do {
223 response = rl.question('').trim();
224 if (response) {
225 out.push(response);
226 }
227 } while (response);
228 if (forbidEmpty && !out.length) {
229 run();
230 }
231 };
232 run();
233 this.data[k] = out;
234 return out;
235 }
236 getPromptEmail(k, question, forbidEmpty = true, strict = true) {
237 return this.promptCommon(k, () => rl.questionEMail(question), forbidEmpty, strict);
238 }
239 getPromptHidden(k, question, forbidEmpty = true, strict = true) {
240 return this.promptCommon(k, () => rl.question(question, { hideEchoBack: true, cancel: true }), forbidEmpty, strict);
241 }
242 getPromptSelect(k, question, opts, strict = true) {
243 if (this.has(k, strict)) {
244 return this.data[k];
245 }
246 else {
247 const idx = rl.keyInSelect(opts, question, { cancel: false });
248 this.data[k] = opts[idx];
249 return this.data[k];
250 }
251 }
252 promptCommon(k, askFn, forbidEmpty, strict) {
253 if (this.has(k, strict)) {
254 return this.data[k];
255 }
256 else if (forbidEmpty) {
257 let v;
258 do {
259 v = askFn();
260 } while (!v);
261 this.data[k] = v;
262 return this.data[k];
263 }
264 else {
265 this.data[k] = askFn();
266 return this.data[k];
267 }
268 }
269}
270tslib_1.__decorate([
271 lazy_get_decorator_1.LazyGetter(true),
272 tslib_1.__metadata("design:type", Object),
273 tslib_1.__metadata("design:paramtypes", [])
274], PromptableConfig.prototype, "ghRepoFromMetadata", null);
275tslib_1.__decorate([
276 lazy_get_decorator_1.LazyGetter(true),
277 tslib_1.__metadata("design:type", Object),
278 tslib_1.__metadata("design:paramtypes", [])
279], PromptableConfig.prototype, "ghUserFromMetadata", null);
280tslib_1.__decorate([
281 lazy_get_decorator_1.LazyGetter(true),
282 tslib_1.__metadata("design:type", Object),
283 tslib_1.__metadata("design:paramtypes", [])
284], PromptableConfig.prototype, "ghMetadata", null);
285tslib_1.__decorate([
286 Memo,
287 tslib_1.__metadata("design:type", Function),
288 tslib_1.__metadata("design:paramtypes", [Object]),
289 tslib_1.__metadata("design:returntype", String)
290], PromptableConfig.prototype, "promptedEmail", null);
291tslib_1.__decorate([
292 Memo,
293 tslib_1.__metadata("design:type", Function),
294 tslib_1.__metadata("design:paramtypes", [Object]),
295 tslib_1.__metadata("design:returntype", String)
296], PromptableConfig.prototype, "promptedEncryptionPassword", null);
297tslib_1.__decorate([
298 Memo,
299 tslib_1.__metadata("design:type", Function),
300 tslib_1.__metadata("design:paramtypes", [Object]),
301 tslib_1.__metadata("design:returntype", String)
302], PromptableConfig.prototype, "promptedGhEmail", null);
303tslib_1.__decorate([
304 Memo,
305 tslib_1.__metadata("design:type", Function),
306 tslib_1.__metadata("design:paramtypes", [Object]),
307 tslib_1.__metadata("design:returntype", String)
308], PromptableConfig.prototype, "promptedGhRepo", null);
309tslib_1.__decorate([
310 Memo,
311 tslib_1.__metadata("design:type", Function),
312 tslib_1.__metadata("design:paramtypes", [Object]),
313 tslib_1.__metadata("design:returntype", String)
314], PromptableConfig.prototype, "promptedGhToken", null);
315tslib_1.__decorate([
316 Memo,
317 tslib_1.__metadata("design:type", Function),
318 tslib_1.__metadata("design:paramtypes", [Object]),
319 tslib_1.__metadata("design:returntype", String)
320], PromptableConfig.prototype, "promptedGhUser", null);
321tslib_1.__decorate([
322 Memo,
323 tslib_1.__metadata("design:type", Function),
324 tslib_1.__metadata("design:paramtypes", [Object]),
325 tslib_1.__metadata("design:returntype", String)
326], PromptableConfig.prototype, "promptedLicense", null);
327tslib_1.__decorate([
328 Memo,
329 tslib_1.__metadata("design:type", Function),
330 tslib_1.__metadata("design:paramtypes", [Object]),
331 tslib_1.__metadata("design:returntype", String)
332], PromptableConfig.prototype, "promptedName", null);
333tslib_1.__decorate([
334 Memo,
335 tslib_1.__metadata("design:type", Function),
336 tslib_1.__metadata("design:paramtypes", [Object]),
337 tslib_1.__metadata("design:returntype", String)
338], PromptableConfig.prototype, "promptedPkgMgr", null);
339tslib_1.__decorate([
340 Memo,
341 tslib_1.__metadata("design:type", Function),
342 tslib_1.__metadata("design:paramtypes", [Object]),
343 tslib_1.__metadata("design:returntype", String)
344], PromptableConfig.prototype, "promptedProjectDescription", null);
345tslib_1.__decorate([
346 Memo,
347 tslib_1.__metadata("design:type", Function),
348 tslib_1.__metadata("design:paramtypes", [Object]),
349 tslib_1.__metadata("design:returntype", String)
350], PromptableConfig.prototype, "promptedProjectKeywords", null);
351tslib_1.__decorate([
352 Memo,
353 tslib_1.__metadata("design:type", Function),
354 tslib_1.__metadata("design:paramtypes", [Object]),
355 tslib_1.__metadata("design:returntype", String)
356], PromptableConfig.prototype, "promptedProjectName", null);
357tslib_1.__decorate([
358 Memo,
359 tslib_1.__metadata("design:type", Function),
360 tslib_1.__metadata("design:paramtypes", [Object]),
361 tslib_1.__metadata("design:returntype", String)
362], PromptableConfig.prototype, "promptedReleaseGhToken", null);
363tslib_1.__decorate([
364 Memo,
365 tslib_1.__metadata("design:type", Function),
366 tslib_1.__metadata("design:paramtypes", [Object]),
367 tslib_1.__metadata("design:returntype", String)
368], PromptableConfig.prototype, "promptedReleaseNpmToken", null);
369tslib_1.__decorate([
370 Memo,
371 tslib_1.__metadata("design:type", Function),
372 tslib_1.__metadata("design:paramtypes", [Object]),
373 tslib_1.__metadata("design:returntype", String)
374], PromptableConfig.prototype, "promptedTravisTokenOrg", null);
375tslib_1.__decorate([
376 Memo,
377 tslib_1.__metadata("design:type", Function),
378 tslib_1.__metadata("design:paramtypes", [Object]),
379 tslib_1.__metadata("design:returntype", String)
380], PromptableConfig.prototype, "promptedTravisTokenPro", null);
381tslib_1.__decorate([
382 Memo,
383 tslib_1.__metadata("design:type", Function),
384 tslib_1.__metadata("design:paramtypes", [Object]),
385 tslib_1.__metadata("design:returntype", String)
386], PromptableConfig.prototype, "promptedUserWebsite", null);
387exports.PromptableConfig = PromptableConfig;