-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
79 lines (71 loc) · 1.75 KB
/
index.js
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
'use strict';
const logger = require('yeoman-environment/lib/util/log');
const inquirer = require('inquirer');
const events = require('events');
const _ = require('lodash');
const logMethods = [
'write',
'writeln',
'ok',
'error',
'skip',
'force',
'create',
'invoke',
'conflict',
'identical',
'info',
'table'
];
function DummyPrompt(answers, q) {
this.answers = answers;
this.question = q;
}
DummyPrompt.prototype.run = function() {
let answer = this.answers[this.question.name];
let isSet;
switch (this.question.type) {
case 'list':
isSet = answer !== undefined;
break;
case 'confirm':
isSet = answer || answer === false;
break;
default:
isSet = !!answer;
}
if (!isSet) {
answer = this.question.default;
if (answer === undefined && this.question.type === 'confirm') {
answer = true;
}
}
return Promise.resolve(answer);
};
function AutoAdapter(answers, silent) {
answers = answers || {};
this.promptModule = inquirer.createPromptModule();
Object.keys(this.promptModule.prompts).forEach(function(promptName) {
this.promptModule.registerPrompt(promptName, DummyPrompt.bind(DummyPrompt, answers));
}, this);
if (!silent){
this.log = logger();
} else {
const log = this.log = function(){};
_.extend(log, events.EventEmitter.prototype);
logMethods.forEach(function (methodName) {
log[methodName] = function(){ return log};
});
}
}
AutoAdapter.prototype.diff = function() {
return 'Diff issues, please run manually to resolve';
};
AutoAdapter.prototype.prompt = function(questions, cb) {
const promise = this.promptModule(questions);
promise.then(cb || undefined);
return promise;
};
module.exports = {
AutoAdapter: AutoAdapter
};