-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathadapter.ts
99 lines (78 loc) · 2.24 KB
/
adapter.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
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
import * as util from 'util';
import { window, OutputChannel, ViewColumn } from 'vscode';
import PromptFactory from '../prompts/factory';
import runAsync from '../utils/run-async';
const logger = require('yeoman-environment/lib/util/log');
const diff = require('diff');
const isFn = require('is-fn');
export default class CodeAdapter {
public log = logger();
private outChannel: OutputChannel;
private outBuffer: string = '';
constructor() {
let self = this;
this.outChannel = window.createOutputChannel('Yeoman');
this.outChannel.clear();
this.outChannel.show();
// TODO Do not overwrite these methods
console.error = console.log = function() {
const line = util.format.apply(util, arguments);
self.outBuffer += `${line}\n`;
self.outChannel.appendLine(line);
return this;
};
this.log.write = function() {
const line = util.format.apply(util, arguments);
self.outBuffer += line;
self.outChannel.append(line);
return this;
};
}
public prompt(questions, callback) {
let answers = {};
callback = callback || function() {};
const promise = questions.reduce((promise, question) => {
return promise
.then(() => {
if (question.when === undefined) {
return true;
} else if (isFn(question.when)) {
return runAsync(question.when)(answers);
}
return question.when;
})
.then(askQuestion => {
if (askQuestion) {
const prompt = PromptFactory.createPrompt(question, answers);
return prompt.render().then(result => answers[question.name] = question.filter ? question.filter(result) : result);
}
});
}, Promise.resolve());
return promise
.then(() => {
this.outChannel.clear();
this.outChannel.append(this.outBuffer);
callback(answers);
return answers;
});
}
public diff(actual, expected) {
this.outChannel.clear();
let result = diff.diffLines(actual, expected);
result.map(part => {
let prefix = ' ';
if (part.added === true) {
prefix = '+';
} else if (part.removed === true) {
prefix = '-';
}
part.value = part.value.split('\n').map(line => {
if (line.trim().length === 0) {
return line;
}
return `${prefix}${line}`
}).join('\n');
this.outChannel.append(part.value);
});
}
}