2dda2e10
Administrator
generator ignore
|
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
129
130
131
132
133
|
var chalk = require('chalk');
var Q = require('q');
var promptly = require('promptly');
var createError = require('../util/createError');
function JsonRenderer() {
this._nrLogs = 0;
}
JsonRenderer.prototype.end = function (data) {
if (this._nrLogs) {
process.stderr.write(']\n');
}
if (data) {
process.stdout.write(this._stringify(data) + '\n');
}
};
JsonRenderer.prototype.error = function (err) {
var message = err.message;
var stack;
err.id = err.code || 'error';
err.level = 'error';
err.data = err.data || {};
// Need to set message again because it is
// not enumerable in some cases
delete err.message;
err.message = message;
// Stack
stack = err.fstream_stack || err.stack || 'N/A';
err.stacktrace = (Array.isArray(stack) ? stack.join('\n') : stack);
this.log(err);
this.end();
};
JsonRenderer.prototype.log = function (log) {
if (!this._nrLogs) {
process.stderr.write('[');
} else {
process.stderr.write(', ');
}
process.stderr.write(this._stringify(log));
this._nrLogs++;
};
JsonRenderer.prototype.prompt = function (prompts) {
var promise = Q.resolve();
var answers = {};
var that = this;
prompts.forEach(function (prompt) {
var opts;
var funcName;
// Strip colors
prompt.message = chalk.stripColor(prompt.message);
// Prompt
opts = {
silent: true, // To not mess with JSON output
trim: false, // To allow " " to not assume the default value
default: prompt.default == null ? '' : prompt.default, // If default is null, make it '' so that it does not retry
validator: !prompt.validate ? null : function (value) {
var ret = prompt.validate(value);
if (typeof ret === 'string') {
throw ret;
}
return value;
},
};
// For now only "input", "confirm" and "password" are supported
switch (prompt.type) {
case 'input':
funcName = 'prompt';
break;
case 'confirm':
case 'password':
funcName = prompt.type;
break;
case 'checkbox':
funcName = 'prompt';
break;
default:
promise = promise.then(function () {
throw createError('Unknown prompt type', 'ENOTSUP');
});
return;
}
promise = promise.then(function () {
// Log
prompt.level = 'prompt';
that.log(prompt);
return Q.nfcall(promptly[funcName], '', opts)
.then(function (answer) {
answers[prompt.name] = answer;
});
});
if (prompt.type === 'checkbox') {
promise = promise.then(function () {
answers[prompt.name] = answers[prompt.name].split(',');
});
}
});
return promise.then(function () {
return answers;
});
};
// -------------------------
JsonRenderer.prototype._stringify = function (log) {
// To json
var str = JSON.stringify(log, null, ' ');
// Remove colors in case some log has colors..
str = chalk.stripColor(str);
return str;
};
module.exports = JsonRenderer;
|