UNPKG

3.69 kBJavaScriptView Raw
1/**
2 * npmRecent.js list recent global installs.
3 *
4 */
5'use strict';
6
7// Dependencies
8
9function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }
10
11const columnify = require('columnify');
12const listToColumns = require('cli-columns');
13const iPipeTo = require('ipt');
14const {
15 spawn
16} = require('child_process');
17const StringUtil = require('./utils/stringUtil');
18const npmRoot = require('./npmRoot');
19
20/*
21 * npmRecent.js has only one exposed export function.
22 * i.e.npmRecent().recentTwenty().fuzzy();
23 * i.e.npmRecent().all().default();
24 * i.e.npmRecent().all().rawNoColor();
25*/
26module.exports = function () {
27
28 // First-level chain operations
29 return {
30 recentTwenty: (() => {
31 var _ref = _asyncToGenerator(function* () {
32 let list = (yield npmRoot()).getRecentTwentyModules();
33 return secondLevelChainOperation(list);
34 });
35
36 return function recentTwenty() {
37 return _ref.apply(this, arguments);
38 };
39 })(),
40 all: (() => {
41 var _ref2 = _asyncToGenerator(function* () {
42 let list = (yield npmRoot()).getAllModules();
43 return secondLevelChainOperation(list);
44 });
45
46 return function all() {
47 return _ref2.apply(this, arguments);
48 };
49 })()
50 };
51};
52
53/*
54 * secondLevelChainOperations deals with default(), fuzzy(), raw(), rawNoColor()
55 * Increase code reuse.
56 *
57 */
58function secondLevelChainOperation(list = []) {
59 list = columnify(list, {
60 showHeaders: false
61 }).split('\n'); // Convert the gigantic columnified string into a list
62
63 return { // Second-level chain operations
64 default() {
65 if (!list || list.length === 0) return;
66
67 // Break the list into multiple lists that span the entire terminal width
68 return console.log(listToColumns(list, {
69 sort: false // Reject the use of default alphabetic sorting by cli-columns
70 }));
71 },
72
73 fuzzy() {
74 if (!list || list.length === 0) return;
75
76 return iPipeTo(list, {
77 size: 20,
78 autocomplete: true,
79 message: ' '
80 }).then(keys => {
81 return keys.forEach((() => {
82 var _ref3 = _asyncToGenerator(function* (key) {
83 // Clean key
84 let cleansedKey = function () {
85 let head = key.split(' ')[0];
86 let result = StringUtil.getRidOfColors(head);
87 result = StringUtil.getRidOfQuotationMarks(result);
88 result = StringUtil.cleanTagName(result); // surl-cli@semantically-release => surl-cli
89 return result;
90 }();
91
92 spawn(`npm info ${cleansedKey} | less -r`, {
93 stdio: 'inherit',
94 shell: true
95 });
96 });
97
98 return function (_x) {
99 return _ref3.apply(this, arguments);
100 };
101 })());
102 }).catch(err => {
103 console.log(err, "Error building interactive interface");
104 });
105 },
106
107 /***** For API use *****/
108 raw: (() => {
109 var _ref4 = _asyncToGenerator(function* () {
110 if (!list || list.length === 0) return;
111
112 return list;
113 });
114
115 return function raw() {
116 return _ref4.apply(this, arguments);
117 };
118 })(),
119
120 rawNoColor: (() => {
121 var _ref5 = _asyncToGenerator(function* () {
122 if (!list || list.length === 0) return;
123
124 return list.map(function (key) {
125 let result = StringUtil.getRidOfColors(key);
126 result = StringUtil.getRidOfQuotationMarks(result);
127 return result;
128 });
129 });
130
131 return function rawNoColor() {
132 return _ref5.apply(this, arguments);
133 };
134 })()
135 };
136}
\No newline at end of file