This repository has been archived by the owner on Aug 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 105
/
utils.js
225 lines (201 loc) · 5.77 KB
/
utils.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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
var fs = require('fs'),
async = require('async'),
logger = require('./logger'),
prompt = require('prompt'),
urlFormat = require('url').format,
urlParse = require('url').parse;
/**
* Pads a string to minlength by appending spaces.
*
* @param {String} str
* @param {Number} minlength
* @return {String}
* @api public
*/
exports.padRight = function (str, minlength) {
while (str.length < minlength) {
str = str + ' ';
}
return str;
};
exports.longest = function (arr) {
return arr.reduce(function (a, x) {
if (x.length > a) {
return x.length;
}
return a;
}, 0);
};
/**
* Reads the version property from Jam's package.json
*/
exports.getJamVersion = async.memoize(function (callback) {
exports.readJSON(__dirname + '/../package.json', function (err, pkg) {
if (err) {
return callback(err);
}
return callback(null, pkg.version);
});
});
/**
* Read a file from the filesystem and parse as JSON
*
* @param {String} path
* @param {Function} callback
* @api public
*/
exports.readJSON = function (path, callback) {
fs.readFile(path, function (err, content) {
var val;
if (err) {
return callback(err);
}
try {
val = JSON.parse(content.toString());
}
catch (e) {
var stack = e.stack.split('\n').slice(0, 1);
stack = stack.concat(['\tin ' + path]);
e.stack = stack.join('\n');
return callback(e, null);
}
callback(null, val);
});
};
/**
* Used by commands wanting to add auth info to a URL. It will only prompt for
* a password if the URL has a username, but no password associated with it.
* Optionally you can force an auth prompt if the url has no auth data at all
* by setting force to true.
*/
exports.completeAuth = function (url, force, callback) {
var parsed = urlParse(url);
if (parsed.auth) {
// if only a username has been specified, ask for password
if (parsed.auth.split(':').length === 1) {
return exports.getAuth(url, callback);
}
}
else if (force) {
// no auth info, but auth required
return exports.getAuth(url, callback);
}
callback(null, url);
};
exports.catchAuthError = function (fn, url, extra_args, callback) {
fn.apply(null, [url].concat(extra_args).concat(function (err) {
if (err && err.response && err.response.statusCode === 401) {
logger.error(err.message || err.toString());
exports.getAuth(url, function (err, url) {
if (err) {
return callback(err);
}
console.log('');
exports.catchAuthError(fn, url, extra_args, callback);
});
}
else {
callback.apply(this, arguments);
}
}));
};
exports.getPassword = function (callback) {
process.stdout.write('Password: ');
if (!prompt.started) {
prompt.start();
}
prompt.readLineHidden(callback);
};
exports.getUsername = function (callback) {
process.stdout.write('Username: ');
if (!prompt.started) {
prompt.start();
}
prompt.readLine(callback);
};
exports.getAuth = function (url, callback) {
var parsed = urlParse(url);
// if a username has been specified, only ask for password
if (parsed.auth && parsed.auth.split(':').length === 1) {
console.log('Please provide credentials for: ' + url);
exports.getPassword(function (err, password) {
if (err) {
return callback(err);
}
delete parsed.host;
parsed.auth += ':' + password;
console.log('');
callback(null, urlFormat(parsed));
});
}
else {
delete parsed.auth;
delete parsed.host;
var noauth = exports.noAuthURL(url);
console.log('Please provide credentials for: ' + noauth);
exports.getUsername(function (err, username) {
if (err) {
return callback(err);
}
exports.getPassword(function (err, password) {
if (err) {
return callback(err);
}
parsed.auth = username + ':' + password;
callback(null, urlFormat(parsed));
});
});
}
};
/**
* Used by commands wanting to report a URL on the command-line without giving
* away auth info.
*/
exports.noAuthURL = function (url) {
var parts = urlParse(url);
delete parts.auth;
delete parts.host;
return urlFormat(parts);
};
exports.ISODateString = function (d) {
function pad(n){
return n < 10 ? '0' + n : n;
}
return d.getUTCFullYear() + '-' +
pad(d.getUTCMonth() + 1) + '-' +
pad(d.getUTCDate()) + 'T' +
pad(d.getUTCHours()) + ':' +
pad(d.getUTCMinutes()) + ':' +
pad(d.getUTCSeconds()) + 'Z';
};
exports.getConfirmation = function (msg, callback) {
function trim(str) {
var trimmed = str.replace(/^\s*/, '');
return trimmed.replace(/\s*$/, '');
}
if (!prompt.started) {
prompt.start();
}
var val;
async.until(
function () {
var valid = (val === '' || val === 'y' || val === 'n');
if (!valid) {
process.stdout.write(msg + ' [Y/n]: ');
}
return valid;
},
function (cb) {
prompt.readLine(function (err, line) {
if (err) {
return cb(err);
}
val = trim(line).toLowerCase();
cb();
});
},
function (err) {
callback(err, val === '' || val === 'y');
}
);
};