-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
325 lines (243 loc) · 8.56 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
///////////////////////////////////////////
//@author: Mandeep Bisht
///////////////////////////////////////////
'use strict'
/*****************
* Require Statements
*/
const util = require('util');
const { exec } = require('child_process');
const https = require('https');
const readline = require('readline')
const inquirer = require('inquirer');
//inquirer checkbox plugin
const checkbox = require('inquirer-checkbox-plus-prompt');
//inquirer table plugin
const table = require("inquirer-table-prompt");
//Spinner
const ora = require('ora');
const chalk = require('chalk');
const open = require('open');
// Global variable uses to map package name to the links
const mapping = {};
/*****************
* Function to search and select packages
*/
function getPackage() {
inquirer.registerPrompt('checkbox-plus', checkbox);
return inquirer.prompt([{
type: 'checkbox-plus',
name: 'package',
message: '🔍️ ' + chalk.blue.bold('Enter Package Name:'),
pageSize: 5,
highlight: true,
searchable: true,
validate: function (answer) {
return true;
},
source: function (answersSoFar, input) {
input = input || '';
if (input == '') {
return Promise.resolve([]);
}
return new Promise(function (resolve, reject) {
/////////////////////////////////////////
// Making a https get request to npms api
const req = https.get(`https://api.npms.io/v2/search/suggestions?q=${input}`, (resp) => {
let data = '';
/////////////////////////////////////
// Receiving Data
resp.on('data', (chunk) => {
data += chunk;
});
/////////////////////////////////////
//Data received
resp.on('end', () => {
//Parsing json data
data = JSON.parse(data);
const real_data = data.map((el) => {
mapping[el.package.name + '@' + el.package.version] = {
'homepage': el.package.links.homepage,
'repository': el.package.links.repository,
'npm': el.package.links.npm
};
return el.package.name + '@' + el.package.version;
});
return resolve(real_data);
});
});
/////////////////////////////////////////
// In case of error dont show anything
req.on('error', function (err) {
//Return Empty Array
return resolve([]);
});
req.end();
});
}
}]);
}
/*****************
* Function to select scope
*/
function getScope(row_options) {
inquirer.registerPrompt("table", table);
return inquirer
.prompt([
{
type: "table",
name: "scope",
message: '🎭️ ' + chalk.blue.bold("Choose Scope: "),
columns: [
{
name: "Local",
value: ""
},
{
name: "Global",
value: "--global"
}
],
rows: row_options
}
]);
}
/*****************
* Function to select dependency options
*/
function getDepOption(row_options) {
return inquirer
.prompt([
{
type: "table",
name: "dependencies",
message: '💀️ ' + chalk.blue.bold("Choose Installation Option:"),
columns: [
{
name: "save-prod",
value: "--save-prod"
},
{
name: "save-dev",
value: "--save-dev"
},
{
name: "save-optional",
value: "--save-optional"
},
{
name: "no-save",
value: "--no-save"
}
],
rows: row_options
}
]);
}
/*****************
* Function to listen to keypress event
*/
function keypress_listen(packages) {
/////////////////////////////////////////
// enabling stdin to emit events
readline.emitKeypressEvents(process.stdin);
process.stdin.setRawMode(true);
/////////////////////////////////////////
// listen to keypress event
process.stdin.on('keypress', async (str, key) => {
// Open homepage of packages (ctrl + w)
if (key.ctrl && key.name === 'w') {
for (let i = 0; i < packages.length; ++i) {
if (mapping[packages[i]].homepage) {
await open(mapping[packages[i]].homepage);
}
}
}
// Open repository of packages (ctrl + r)
if (key.ctrl && key.name === 'r') {
for (let i = 0; i < packages.length; ++i) {
if (mapping[packages[i]].repository) {
await open(mapping[packages[i]].repository);
}
}
}
// Open repository for particular package (ctrl + #)
if (key.meta && key.name > '0' && key.name <= '9') {
let index = parseInt(key.name) - 1;
if (index < packages.length && mapping[packages[index]].repository) {
await open(mapping[packages[index]].repository);
}
}
});
}
/*****************
* Main function
*/
async function main() {
////////////////////////////////////////
// Function to get selected package
const packages = (await getPackage()).package;
const number_of_packages = packages.length;
////////////////////////////////////////
// I no package is selected quit
if (number_of_packages === 0) {
console.log('🤷♂️️ ' + chalk.yellow.underline('No package selected '));
process.exit(0);
}
keypress_listen(packages);
const row_options = [];
console.log(chalk.yellow('\n>>>>>'));
console.log(chalk.blue.bold.underline('Selected Packages :'));
for (let i = 0; i < number_of_packages; ++i) {
console.log(chalk.green(' + ' + packages[i]));
row_options.push({ name: packages[i], value: i });
}
console.log(chalk.yellow('>>>>>\n'));
////////////////////////////////////////
// Function to get scope options(local or global)
const scopes = (await getScope(row_options)).scope;
////////////////////////////////////////
// Function to get dependencies option
const dep_option = (await getDepOption(row_options)).dependencies;
////////////////////////////////////////
//Promosifing exec command
const exec_command = util.promisify(exec);
for (let i = 0; i < number_of_packages; ++i) {
console.log(chalk.yellow('\n>>>>>'));
const command = `npm install ${packages[i]} ${scopes[i] ? scopes[i] : ''} ${dep_option[i] ? dep_option[i] : ''}`;
console.log(chalk.blue.bold(`Executing : `) + command);
const spinner = ora(`Installing ${packages[i]}`).start();
setTimeout(() => {
spinner.color = 'red';
spinner.text = `Installing ${packages[i]} 🤺️`;
}, 500);
try {
////////////////////////////////////////////
// Execute command
const { stdout, stderr } = await exec_command(command);
////////////////////////////////////////////
// If error occures
if (stderr) {
spinner.warn(`Warning ${packages[i]}`);
console.log(chalk.yellow(stderr))
}
////////////////////////////////////////////
// In case of success
else {
spinner.succeed(`Installed ${packages[i]}`);
console.log(chalk.green(stdout));
}
} catch (err) {
spinner.fail(`Error installing ${packages[i]}`);
console.log(chalk.red(err.stderr));
}
console.log(chalk.yellow('>>>>>\n'));
}
process.exit(0);
}
/*****************
* Export Statement
*/
module.exports = {
main
}