-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathutil.js
executable file
·772 lines (699 loc) · 22 KB
/
util.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
'use strict';
const _ = require('lodash');
const lev = require('leven');
const mkdirp = require('mkdirp');
const chalk = require('chalk');
const strip = require('strip-ansi');
const path = require('path');
const os = require('os');
const app = require('../');
const request = require('request');
// Colors by class.
const colors = {
'method': 'green',
'property': 'blue',
'object': 'yellow',
'doc': 'white',
'lib': 'white',
'unbuilt-lib': 'gray',
'remainder': 'gray'
};
// Fancy names by class.
const names = {
'method': 'Methods',
'property': 'Properties',
'object': 'Objects',
'doc': 'Docs',
'lib': 'Libraries',
'unbuilt-lib': 'Downloadable Libraries',
'remainder': 'Other'
};
const util = {
/**
* Handles tabbed auto-completion based on
* the doc index. Works perfectly. Looks ugly
* as hell. Hey: It works.
*
* @param {String} text
* @param {Integer} iteration
* @param {Object} index
* @return {Array} [mode, data]
* @api public
*/
autocomplete(text, iteration, index, matchFn) {
const commands = util.command.prepare(text, {}, index);
const lastWord = String(commands[commands.length - 1]).trim();
const otherWords = commands.slice(0, commands.length - 1);
const words = String(text).trim().split(' ').length;
// Find the deepest point on the index that
// matches the given commands. i.e.
// "earth usa cali" against
// { "earth": { "usa": { "cali": { "foo": "bar" } }}}
// would return { "foo": "bar" }
let levels = 0;
const possibleObjects = util.matchAgainstIndex(_.clone(commands), index, function () {
levels++;
});
const formatted = this.formatAutocomplete(possibleObjects);
const possibilities = Object.keys(possibleObjects);
const match = matchFn(String(lastWord).trim(), possibilities);
let mode = 'default';
let response;
if (match && levels !== otherWords.length + 1) {
const space = (possibilities.indexOf(String(match).trim()) > -1) ? ' ' : '';
response = `${String(`${otherWords.join(` `)} ${match}`).trim()}${space}`;
} else {
const space = (levels === otherWords.length + 1) ? ' ' : '';
const original = `${String(commands.join(' ')).trim()}${space}`;
if (iteration > 1 && possibilities.length > 1) {
response = [formatted];
} else if (iteration > 1 && possibilities.length === 1 && (otherWords.length !== levels)) {
response = `${String(`${original}${possibilities[0]}`).trim()} `;
} else if (levels === 1 && words === 1 && Object.keys(possibleObjects).length === 0 && iteration > 1) {
// In this scenario, the user has chosen an autodoc
// lib that hasn't been downloaded yet, and has tabbed.
// We tell the user what he can do.
if (iteration < 3) {
const times = chalk.cyan(`1 more time`);
mode = 'pre-build';
response = chalk.blue(`\n This library has not been built. \n To build, press ${chalk.cyan(`[tab]`)} ${times}, or press ${chalk.cyan(`[enter]`)}.`);
} else if (iteration === 3) {
mode = 'build';
response = original;
} else {
response = original;
}
} else {
response = original;
}
}
return ({
mode,
response
});
},
/**
* Takes an array of index items to be displayed
* under tabbed autocompletion. Gathers their '__class'
* from the index ('method', 'property', 'doc', etc.)
* and separates them into groups based on this.
* If worthwhile, draws and color-separates classes
* into fancy columns so the data is really, really
* easy to digest.
*
* @param {Array} possibilities
* @return {String}
* @api public
*/
formatAutocomplete(possibilities) {
const self = this;
const cats = ['method', 'property', 'object', 'doc', 'lib', 'unbuilt-lib'];
const data = {};
let all = Object.keys(possibilities) || [];
function filter(objs, type) {
const results = {};
for (const item in objs) {
if (objs[item].__class === type) {
results[item] = objs[item];
}
}
if (type === 'doc') {
// Get fancy by reading the __seq
// property of the docs in the index,
// and then sort them by that.
let res = [];
for (const item in results) {
if (results.hasOwnProperty(item)) {
res.push([item, results[item].__seq]);
}
}
res = res.sort(function (a, b) {
return a[1] - b[1];
}).map(function (itm) {
return itm[0];
});
return res;
}
return Object.keys(results);
}
// If the object has children, add a slash.
const newPoss = {};
for (const item in possibilities) {
if (possibilities.hasOwnProperty(item)) {
let keys = Object.keys(possibilities[item]);
keys = keys.filter(function (key) {
return String(key).slice(0, 2) !== '__';
});
if (keys.length > 0) {
newPoss[`${item}/`] = _.clone(possibilities[item]);
} else {
newPoss[item] = possibilities[item];
}
}
}
// Build an array of each class ('method', 'doc', etc.),
// filed under the `data` object.
let matches = [];
for (let i = 0; i < cats.length; ++i) {
data[cats[i]] = filter(newPoss, cats[i]);
matches = matches.concat(data[cats[i]]);
}
// Data.remainer takes care of any items that don't
// have a `__class` attribute in the index.
data.remainder = all.filter(function (item) {
return !(matches.indexOf(item) > -1 || matches.indexOf(`${item}/`) > -1);
});
// All has been made inconsistent due to adding in '/'es
// to the others. Reset it.
all = [];
for (let i = 0; i < cats.length; ++i) {
all = all.concat(data[cats[i]]);
}
const totalWidth = all.join(' ').length + 4;
// Get the widest item of them all
// (mirror, mirror on the wall).
let maxWidth = 0;
all.forEach(function (item) {
const width = String(item).length;
maxWidth = (width > maxWidth) ? width : maxWidth;
});
maxWidth += 3;
// The headers aren't measured for width, and
// so if the thinnest property is less than the
// "Properties" header, it's goinna look ugly.
maxWidth = (maxWidth < 12) ? 12 : maxWidth;
// Determine how many display columns get allocated
// per data class ('method', 'property', etc.),
// based on how many children each data class has.
const numColumns = Math.floor((process.stdout.columns - 2) / maxWidth);
const dataColumns = {};
let totalAllocated = 0;
let maxItem;
let max = 0;
for (const item in data) {
if (data[item].length > 0) {
dataColumns[item] = Math.floor((data[item].length / all.length) * numColumns) || 1;
totalAllocated += dataColumns[item];
max = (dataColumns[item] > max) ? dataColumns[item] : max;
maxItem = (dataColumns[item] === max) ? item : maxItem;
}
}
// Do correction on the above figures to ensure we don't
// top over the max column amount.
const columnOverflow = totalAllocated - numColumns;
if (columnOverflow > 0) {
dataColumns[maxItem] -= columnOverflow;
}
const types = Object.keys(dataColumns);
const onlyDocs = (types.length === 1 && types[0] === 'doc');
// Methods and Properties go alphabetical.
// Docs go in exact sequences.
data.method.sort();
data.property.sort();
// Final formatting section.
let fnl = '';
// If we are only documents, do one straight
// line. If we otherwise fit on one line, roll
// with that. Otherwise, do fancy columns.
if (onlyDocs) {
let docs = data.doc;
const max = process.stdout.rows - 5;
const total = docs.length;
docs = docs.slice(0, max);
if (docs.length > 0) {
const clr = colors.doc;
let set = `\n ${docs.join('\n ')}\n`;
set = (clr) ? chalk[clr](set) : set;
fnl += set;
}
if (total !== docs.length) {
fnl += `${chalk.grey(` ${(total - docs.length)}`)} more...\n`;
}
} else if (totalWidth <= process.stdout.columns) {
for (const item in data) {
if (data.hasOwnProperty(item)) {
const arr = data[item];
if (arr.length > 0) {
const clr = colors[item];
let set = `${arr.join(' ')} `;
set = (clr) ? chalk[clr](set) : set;
fnl += set;
}
}
}
fnl = String(fnl).trim();
fnl = `\n ${String(fnl).trim()}\n`;
} else {
// This takes a class, such as `method`,
// and draws x number of columns for that
// item based on the allocated number of
// column (`dataColumns[class]`). Returns
// a \n-broken chunk of text.
function drawClassBlock(item) {
let ctr = 1;
const arr = data[item];
const columns = dataColumns[item];
const width = maxWidth - 2;
const color = colors[item];
let lines = '';
let line = '';
let longestLine = 0;
function endLine() {
const lineWidth = strip(line).length;
longestLine = (lineWidth > longestLine) ? lineWidth : longestLine;
lines += `${line}\n`;
line = '';
ctr = 1;
}
for (let i = 0; i < arr.length; ++i) {
let item = `${self.pad(arr[i], width)} `;
item = (color) ? chalk[color](item) : item;
line += item;
if (ctr >= columns) {
endLine();
} else {
ctr++;
}
}
if (line !== '') {
endLine();
}
lines = lines.split('\n').map(function (ln) {
return self.pad(ln, longestLine);
}).join('\n');
const title = self.pad(names[item], longestLine);
const divider = `${chalk.gray(self.pad('', longestLine - 2, '-'))} `;
lines = `${chalk.white(chalk.bold(title))}\n${divider}\n${lines}`;
return lines;
}
// Throw all blocks into an array, and
// note how many rows down the longest block
// goes.
let combined = [];
let longest = 0;
for (const item in dataColumns) {
if (dataColumns.hasOwnProperty(item)) {
const lines = drawClassBlock(item).split('\n');
longest = (lines.length > longest) ? lines.length : longest;
combined.push(lines);
}
}
let maxHeight = process.stdout.rows - 4;
maxHeight = (maxHeight > 24) ? 24 : maxHeight;
// Match pad all other blocks with white-space
// lines at the bottom to match the length of
// the longest block. In other words, make the
// blocks... blocks.
combined = combined.map(function (lines) {
const lineLength = strip(lines[0]).length;
for (let i = lines.length; i < longest; ++i) {
lines.push(self.pad('', lineLength));
}
const numRealLines = lines.filter(function (line) {
return (strip(line).trim() !== '');
}).length;
// If we've exceeded the max height and have
// content, do a fancy `...` and cut the rest
// of the content.
if (numRealLines > maxHeight && String(lines[maxHeight - 1]).trim() !== '') {
let ellip = `${(numRealLines - maxHeight)} more ...`;
ellip = chalk.gray((ellip.length > lineLength) ? '...' : ellip);
lines = lines.slice(0, maxHeight - 1);
lines.push(self.pad(ellip, lineLength));
}
return lines;
});
longest = (maxHeight < longest) ? maxHeight + 1 : longest;
// Now play Tetris. Join the blocks.
for (let i = 0; i < longest; ++i) {
for (let j = 0; j < combined.length; ++j) {
if (combined[j][i]) {
fnl += combined[j][i];
}
}
fnl += '\n';
}
// Interject a two-space pad to the left of
// the blocks, and do some cleanup at the end.
fnl = String(fnl.split('\n').map(function (ln) {
return ` ${ln}`;
}).join('\n').replace(/ +$/, '').replace(/\n$/g, ''));
}
return fnl;
},
autocompletionHelper(root, array, text, iteration) {
const txt = String(text).trim();
const arr = array.filter(function (itm) {
return (itm.slice(0, txt.length) === txt);
});
arr.unshift('\n');
arr.push('\n');
const match = this.match(txt, array);
if (iteration > 1) {
return arr;
}
if (match) {
return `theme ${match}`;
}
return undefined;
},
/**
* Takes an existing array of words
* and matches it against the index.
* Whenever a word can be standardized
* with the index, such as on casing,
* it cleans up the word and returns it.
* For example,
* ['the', 'veryquick ', 'fox'] will become
* ['the', 'veryQuick', 'fox']
* based on the index.
*
* @param {Array} arr
* @param {Object} idx
* @param {Function} each
* @param {Array} results
* @return {Array} results
* @api public
*/
standardizeAgainstIndex(arr, idx, each, results) {
results = results || [];
each = each || function () {};
let word = arr.shift();
// Use a levenshtein distance algorithm
// to look for appriximate matches. If we feel
// safe enough, automagically adopt the match.
if (String(word).trim().length > 0) {
const res = util.levenshteinCompare(word, idx);
if (res.distance === 0) {
word = res.key;
} else if (res.distance === 1 && res.difference > 3) {
word = res.key;
} else if (res.distance === 2 && res.difference > 5 && String(res.key).length > 5) {
word = res.key;
}
}
let response;
if (idx[word]) {
each(arr, idx[word]);
results.push(word);
response = util.standardizeAgainstIndex(arr, idx[word], each, results);
} else {
if (word) {
results.push(word);
}
response = results;
}
return response;
},
parseCommandsFromPath(path) {
const parts = String(path).split('docs/');
let commands = '';
if (parts.length > 1) {
parts.shift();
commands = parts.join('docs/');
} else {
commands = path;
}
return String(commands).split('/');
},
levenshteinCompare(word, obj) {
const keys = Object.keys(obj);
const results = {
firstKey: undefined,
firstDistance: 1000,
secondKey: undefined,
secondDistance: 1000
};
for (let i = 0; i < keys.length; ++i) {
if (keys[i] === 'index') {
continue;
}
const distance = lev(String(word).trim().toLowerCase(), String(keys[i]).trim().toLowerCase());
if (distance < results.firstDistance) {
results.firstDistance = distance;
results.firstKey = keys[i];
} else if (distance < results.secondDistance) {
results.secondDistance = distance;
results.secondKey = keys[i];
}
}
return ({
key: results.firstKey,
distance: results.firstDistance,
difference: results.secondDistance - results.firstDistance
});
},
/**
* Takes an existing array of words
* and matches it against the index, returning
* all available commands for the next
* command, having matched x commands so far.
* For example,
* ['the', 'quick', 'brown'] will return
* ['fox', 'dog', 'goat']
* based on the index, as the index has
* three .md files in the `brown` folder.
*
* @param {Array} arr
* @param {Object} idx
* @param {Function} each
* @return {Array} results
* @api public
*/
matchAgainstIndex(arr, idx, each) {
each = each || function () {};
const word = arr.shift();
let result;
if (idx[word]) {
each(arr, idx[word]);
result = util.matchAgainstIndex(arr, idx[word], each);
} else {
const items = {};
for (const item in idx) {
if (idx.hasOwnProperty(item) && String(item).slice(0, 2) !== '__' && String(item) !== 'index') {
const match = (String(word || '').toLowerCase() === String(item).slice(0, String(word || '').length).toLowerCase());
if (match) {
items[item] = idx[item];
}
}
}
result = items;
}
return result;
},
each(nodes, fn, parents) {
const self = this;
parents = parents || [];
for (const node in nodes) {
if (nodes.hasOwnProperty(node)) {
fn(node, nodes, parents);
if (_.isObject(nodes[node])) {
const parent = _.clone(parents);
parent.push(node);
self.each(nodes[node], fn, parent);
}
}
}
},
fetchRemote(path, cb) {
let proxy;
if (app.clerk.prefs.get('proxy') === 'on') {
const address = app.clerk.prefs.get('proxy-address');
const port = app.clerk.prefs.get('proxy-port');
const user = String(app.clerk.prefs.get('proxy-user') || '').trim();
const pass = String(app.clerk.prefs.get('proxy-pass') || '').trim();
if (user === '' || pass === '') {
proxy = `http://${address}:${port}`;
} else {
proxy = `http://${user}:${pass}@${address}:${port}`;
}
}
request.defaults({proxy});
request.get(path, function (err, response, body) {
if (!err) {
if (body === 'Not Found') {
cb('Not Found', undefined);
} else {
cb(undefined, body, response);
}
} else {
cb(err, '');
throw new Error(err);
}
});
},
pad(str, width, delimiter) {
width = Math.floor(width);
delimiter = delimiter || ' ';
const len = Math.max(0, width - strip(str).length);
return str + Array(len + 1).join(delimiter);
},
separator(str) {
const windows = os.platform() === 'win32';
const hl = windows ? '-' : '─';
if (str.split('\n').length <= process.stdout.rows) {
const padding = util.pad('', process.stdout.columns, chalk.blue(hl));
str = `\n${padding}\n${str}`;
}
return str;
},
/**
* Kind of like mkdirp, but without another depedency.
*
* @param {String} dir
* @return {Util}
* @api public
*/
mkdirSafe(dir) {
return mkdirp.sync(dir);
},
extensions: {
__basic: '.md',
__detail: '.detail.md',
__install: '.install.md'
},
command: {
/**
* Takes a raw string entered by the user,
* sanitizes it and returns it as an array
* of words.
*
* @param {String} str
* @return {Array}
* @api public
*/
prepare(str, options, index) {
options = options || {};
const all = [];
let commands = (_.isArray(str)) ?
str :
String(str).trim().split(' ');
commands = commands.join(' ')
.replace(/\//g, ' ')
.replace(/\\/g, ' ')
.replace(/ +/g, ' ')
.split(' ');
for (let i = 0; i < commands.length; ++i) {
const parts = commands[i].split('.');
for (let j = 0; j < parts.length; ++j) {
const word = String(parts[j])
.trim()
.replace(/\)/g, '')
.replace(/\(/g, '')
.replace(/\;/g, '');
all.push(word);
}
}
const standardized = util.standardizeAgainstIndex(_.clone(all), index);
return standardized;
},
/**
* Takes a raw string and converts it into
* a ready URL root to try loading.
*
* @param {String} str
* @return {String}
* @api public
*/
buildPath(str, options, index) {
const all = util.command.prepare(str, options, index);
const indexObject = util.command.getIndex(_.clone(all), index);
const response = {
path: undefined,
exists: false,
suggestions: undefined,
index: undefined
};
if (!indexObject) {
response.exists = false;
} else if (_.isArray(indexObject)) {
const sugg = util.autocomplete(str, 2, index, function (word, options) {
return options;
}).response;
if (_.isArray(sugg)) {
response.suggestions = sugg;
} else {
response.suggestions = ['', sugg];
}
} else {
response.index = indexObject;
response.exists = true;
}
const path = all.join('/');
response.path = path;
return response;
},
/**
* Returns the deepest index object
* for a given array of commands.
*
* @param {Array} arr
* @param {Object} idx
* @param {Array} results
* @return {Boolean} valid
* @api public
*/
getIndex(arr, idx) {
const word = arr.shift();
let result;
if (idx[word]) {
result = util.command.getIndex(arr, idx[word]);
} else if (!word) {
if (idx.index) {
if (_.isObject(idx.index)) {
idx.index.__isIndexFile = true;
}
result = idx.index;
} else if (idx.__basic) {
result = idx;
} else if (idx.__class === 'unbuilt-lib') {
// If we are an unbuilt library, accept it.
result = idx;
} else {
result = Object.keys(idx);
}
}
return result;
},
/**
* Takes the end string of command,
* 'splice' in 'js array splice',
* reads its index JSON, and compares
* these to the passed in options in order
* to determine the valid .md structure, i.e.
* splice.md, splice.detail.md, splice.install.md,
* etc. etc. etc.
*
* @param {Array} arr
* @param {Object} idx
* @param {Array} results
* @return {Boolean} valid
* @api public
*/
buildExtension(path, index, options) {
let result;
if (_.isObject(index) && index.__isIndexFile === true) {
path += '/index';
}
if (options.detail && index.__detail) {
result = `${path}.detail.md`;
} else if (options.install && index.__install) {
result = `${path}.install.md`;
} else {
result = `${path}.md`;
}
return result;
}
},
path: {
getDocRoot(pathString) {
const normalized = path.normalize(pathString);
const parts = String(normalized).split(`docs${path.sep}`);
if (parts[1] === undefined) {
throw new Error(`Invalid path passed into util.getDocRoot: "${pathString}". Parsed path: ${normalized}.`);
}
return parts[1];
}
}
};
module.exports = util;