-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
362 lines (313 loc) · 10.5 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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
'use strict'
var path = require('path')
var parentRequire = require('parent-require')
var figlet = require('figlet')
var chalk = require('chalk')
module.exports = new Yargonaut()
function Yargonaut () {
var self = this
var yargs = hack()
var fonts = {}
var styles = {}
var fontWorkaround = {}
var styleWorkaround = {}
var ocdf = null
var defaultFont = 'Standard'
var yargsKeys = {
// supporting fonts and styles
'Commands:': { transform: wholeString, error: false },
'Options:': { transform: wholeString, error: false },
'Examples:': { transform: wholeString, error: false },
'Positionals:': { transform: wholeString, error: false },
'Not enough non-option arguments: got %s, need at least %s': { transform: upToFirstColon, error: true },
'Too many non-option arguments: got %s, maximum of %s': { transform: upToFirstColon, error: true },
'Missing argument value: %s': { transform: upToFirstColon, error: true, plural: 'Missing argument values: %s' },
'Missing required argument: %s': { transform: upToFirstColon, error: true, plural: 'Missing required arguments: %s' },
'Unknown argument: %s': { transform: upToFirstColon, error: true, plural: 'Unknown arguments: %s' },
'Invalid values:': { transform: wholeString, error: true },
'Argument check failed: %s': { transform: upToFirstColon, error: true },
'Implications failed:': { transform: wholeString, error: true },
'Not enough arguments following: %s': { transform: upToFirstColon, error: true },
'Invalid JSON config file: %s': { transform: upToFirstColon, error: true },
'Did you mean %s?': { transform: wholeString, error: true },
'Arguments %s and %s are mutually exclusive': { transform: wholeString, error: true },
// supporting styles only (by default)
'boolean': { transform: null, error: null },
'count': { transform: null, error: null },
'string': { transform: null, error: null },
'array': { transform: null, error: null },
'required': { transform: null, error: null },
'default:': { transform: null, error: null },
'choices:': { transform: null, error: null },
'aliases:': { transform: null, error: null },
'generated-value': { transform: null, error: null },
'Argument: %s, Given: %s, Choices: %s': { transform: null, error: true }
}
// chainable config API methods
self.help = function (font) {
applyFont(font, self.getHelpKeys())
return self
}
self.errors = function (font) {
applyFont(font, self.getErrorKeys())
return self
}
self.font = function (font, key) {
var keys = key ? [].concat(key) : self.getAllKeys()
applyFont(font, keys, key !== undefined)
return self
}
self.helpStyle = function (style) {
applyStyle(style, self.getHelpKeys())
return self
}
self.errorsStyle = function (style) {
applyStyle(style, self.getErrorKeys())
return self
}
self.style = function (style, key) {
var keys = key ? [].concat(key) : self.getAllKeys()
applyStyle(style, keys)
return self
}
self.transformWholeString = function (key) {
var keys = key ? [].concat(key) : self.getAllKeys()
applyTransform(wholeString, keys)
return self
}
self.transformUpToFirstColon = function (key) {
var keys = key ? [].concat(key) : self.getAllKeys()
applyTransform(upToFirstColon, keys)
return self
}
self.ocd = function (f) {
if (typeof f === 'function') ocdf = f
return self
}
// getter API methods
self.getAllKeys = function () {
return Object.keys(yargsKeys)
}
self.getHelpKeys = function () {
return self.getAllKeys().filter(function (key) {
return yargsKeys[key].error === false
})
}
self.getErrorKeys = function () {
return self.getAllKeys().filter(function (key) {
return yargsKeys[key].error === true
})
}
// methods for playing with fonts
self.asFont = function (text, font, throwErr) {
try {
return figlet.textSync(text, { font: font || defaultFont })
} catch (err) {
if (throwErr) throw err
return text
}
}
self.listFonts = function () {
return figlet.fontsSync()
}
self.printFont = function (font, text, throwErr) {
font = font || defaultFont
console.log('Font: ' + font + '\n' + self.asFont(text || font, font, throwErr))
}
self.printFonts = function (text, throwErr) {
self.listFonts().forEach(function (f) {
self.printFont(f, text, throwErr)
})
}
self.figlet = function () {
return figlet
}
self.chalk = function () {
return chalk
}
// private transforms
function wholeString (str) {
return { render: str, nonRender: '' }
}
function upToFirstColon (str) {
var firstColon = str.indexOf(':') + 1
return { render: str.substring(0, firstColon), nonRender: '\n ' + str.substring(firstColon) }
}
// private config methods
function applyStyle (style, keys) {
if (typeof style !== 'string') return
keys.forEach(function (k) {
styles[k] = style
})
if (yargs && yargs.updateLocale) applyStyleWorkaround(keys)
}
function applyFont (font, keys, force) {
if (typeof font !== 'string') font = defaultFont
keys.forEach(function (k) {
fonts[k] = font
if (!yargsKeys[k]) yargsKeys[k] = { transform: wholeString, error: null }
else if (force && yargsKeys[k].transform === null) yargsKeys[k].transform = wholeString
})
if (yargs && yargs.updateLocale) applyFontWorkaround(keys)
}
function applyStyleWorkaround (keys) {
var u = {}
keys.forEach(function (k) {
// cache non-font styled text in styleWorkaround
if (yargsKeys[k] && yargsKeys[k].plural) {
styleWorkaround[k] = {
one: doStyle(k, k, styles[k]),
other: doStyle(yargsKeys[k].plural, yargsKeys[k].plural, styles[k])
}
} else {
styleWorkaround[k] = doStyle(k, k, styles[k])
}
u[k] = styleWorkaround[k]
// style font-rendered text if defined
if (fontWorkaround[k]) {
if (typeof fontWorkaround[k] === 'string') u[k] = doStyle(k, fontWorkaround[k], styles[k])
else {
u[k] = {
one: doStyle(k, fontWorkaround[k].one, styles[k]),
other: doStyle(yargsKeys[k].plural, fontWorkaround[k].other, styles[k])
}
}
}
})
yargs.updateLocale(u)
}
function applyFontWorkaround (keys) {
var u = {}
keys.forEach(function (k) {
// cache non-styled font text in fontWorkaround
if (yargsKeys[k] && yargsKeys[k].plural) {
fontWorkaround[k] = {
one: doRender(k, k, fonts[k]),
other: doRender(yargsKeys[k].plural, yargsKeys[k].plural, fonts[k])
}
} else {
fontWorkaround[k] = doRender(k, k, fonts[k])
}
u[k] = fontWorkaround[k]
// render styled text if defined
if (styleWorkaround[k]) {
if (typeof styleWorkaround[k] === 'string') u[k] = doRender(k, styleWorkaround[k], fonts[k])
else {
u[k] = {
one: doRender(k, styleWorkaround[k].one, fonts[k]),
other: doRender(yargsKeys[k].plural, styleWorkaround[k].other, fonts[k])
}
}
}
})
yargs.updateLocale(u)
}
function applyTransform (transform, keys) {
keys.forEach(function (k) {
if (!yargsKeys[k]) yargsKeys[k] = { transform: transform, error: null }
else yargsKeys[k].transform = transform
})
}
// private logic to intercept y18n methods
function doStyle (key, orig, style) {
var chain = chalk
style.split('.').forEach(function (s) {
if (chain[s]) chain = chain[s]
})
return typeof chain === 'function' ? chain(orig) : orig
}
function doRender (key, orig, font) {
var fn = yargsKeys[key] ? yargsKeys[key].transform : upToFirstColon
if (fn === null) return orig
var split = fn(orig)
return self.asFont(split.render, font) + split.nonRender
}
function hack () {
var lookingFor = []
lookingFor.push(path.join('yargs', 'node_modules', 'y18n', 'index.js')) // npm 2 = nested
lookingFor.push(path.join('y18n', 'index.js')) // npm 3 = flat
var found = findInModuleCache(lookingFor)
if (found) {
// console.log('yargonaut: You loaded yargs before yargonaut, only default locale supported')
return parentRequireSafe('yargs')
}
var Y18n = parentRequireFirstOf(lookingFor)
found = findInModuleCache(lookingFor)
if (!found) {
// console.log('yargonaut: You\'re using a version of yargs that does not support string customization')
return parentRequireSafe('yargs')
}
require.cache[found].exports = function (opts) {
var z18n = new Y18n(opts)
var singular = z18n.__
var plural = z18n.__n
z18n.__ = function () {
var orig = singular.apply(z18n, arguments)
var key = arguments[0]
if (fonts[key] && yargsKeys[key]) {
return tweak(
key,
orig,
doRender(key, orig, fonts[key]),
figlet,
fonts[key]
)
}
return tweak(key, orig, orig, figlet, fonts[key])
}
z18n.__n = function () {
var orig = plural.apply(z18n, arguments)
var key = arguments[0]
if (fonts[key] && yargsKeys[key]) {
return tweak(
key,
orig,
doRender(key, orig, fonts[key]),
figlet,
fonts[key]
)
}
return tweak(key, orig, orig, figlet, fonts[key])
}
function tweak (key, origString, newString, figlet, font) {
if (styles[key]) newString = doStyle(key, newString, styles[key])
return ocdf ? ocdf(key, origString, newString, figlet, font) : newString
}
for (var key in z18n) {
if (typeof z18n[key] === 'function') {
z18n[key] = z18n[key].bind(z18n)
}
}
return z18n
}
return null
}
function findInModuleCache (lookingFor) {
var found = null
for (var i = 0, files = Object.keys(require.cache); i < files.length; i++) {
for (var j = 0; j < lookingFor.length; j++) {
if (~files[i].lastIndexOf(lookingFor[j])) {
found = files[i]
break
}
}
if (found) break
}
return found
}
function parentRequireFirstOf (array) {
var success = null
for (var x = 0; x < array.length; x++) {
success = parentRequireSafe(array[x])
if (success) break
}
return success
}
function parentRequireSafe (m) {
try {
return parentRequire(m)
} catch (err) {
return null
}
}
}