forked from carvilsi/snoopm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·213 lines (194 loc) · 6.94 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
/**
* sNooPM
* snooping around code based on npm
*/
'use strict';
const fs = require('fs');
const exec = require('child_process').exec;
const logger = require('./logger');
const path = require('path');
const request = require('request');
const colors = require('colors');
const Table = require('cli-table');
const Spinner = require('cli-spinner').Spinner;
// const npmView = 'npm view --json=true ';
const npmView = 'npm view --json=true --fetch-retry-maxtimeout=45000 ';
var urls = [],
logOutputFormat = 'default',
options = {},
table = new Table(),
urlsPromises = [],
spinner;
var readPackage = (packageData) => {
try {
parseCommandLine();
if (this.options.dev) {
if (!packageData.devDependencies) {
logger.warn('This package has not devDependencies');
process.exit();
}
Object.keys(packageData.devDependencies).forEach((key)=>{
urlsPromises.push(getUrlOfPackage(key));
});
} else {
if (!packageData.dependencies) {
logger.warn('This package has not dependencies. Try with -d');
process.exit();
}
Object.keys(packageData.dependencies).forEach((key)=>{
urlsPromises.push(getUrlOfPackage(key));
});
}
Promise.all(urlsPromises)
.then((res) => {
if (typeof this.options.lines == "undefined") {
spinner.stop(true);
console.log(table.toString());
} else {
spinner.stop(true);
}
});
} catch (e) {
logger.error(e.stack);
process.exit();
}
}
var getUrlOfPackage = (packageName) => {
return new Promise((resolve, reject) => {
exec(npmView.concat(packageName), (error, stdout, stderr) => {
if (!error && !stderr) {
writeDown(JSON.parse(stdout));
resolve(JSON.parse(stdout));
} else
if (error) {
logger.error('Error reading package: ' + packageName);
reject(error);
}
});
});
}
var parseCommandLine = () => {
if (this.options.verbose && this.options.color) {
this.logOutputFormat = 'verbose-noColor';
table = new Table({
head:['name','Ver.','URL','Description']
});
} else
if (this.options.verbose && !this.options.color) {
this.logOutputFormat = 'verbose';
table = new Table();
} else
if (!this.options.verbose && this.options.color) {
this.logOutputFormat = 'default-noColor';
table = new Table({
head:['name','URL','Description']
});
} else
if (this.options.verbose && this.options.dev) {
table = new Table();
} else
if (this.options.verbose && this.options.dev && this.options.color) {
table = new Table({
head:['name','Ver.','URL','Description']
});
}
else {
table = new Table();
}
}
var requesting = (url) => {
request(url, (error, response, body) => {
if (error) {
logger.error('Error requesting package.json');
} else {
try {
readPackage(JSON.parse(body));
} catch (e) {
logger.error('Invalid package.json provided by url');
}
}
});
}
var writeDown = (depData) => {
switch (this.logOutputFormat) {
case 'verbose':
if (this.options.lines) {
console.log(colors.gray(depData.name).concat(' ; ').concat(depData['dist-tags'].latest).concat(' ; ').concat(typeof depData.homepage == 'undefined'?'¬.¬ --> unknown, not available?':colors.underline(depData.homepage).concat(' ; ').concat(colors.cyan(depData.description))));
} else {
table.push([colors.gray(depData.name),depData['dist-tags'].latest,typeof depData.homepage == 'undefined'?'¬.¬ --> unknown, not available?':colors.underline(typeof depData.homepage == 'undefined'?'¬.¬ --> unknown, not available?':depData.homepage),colors.cyan(depData.description.length>70?depData.description.substring(0,70).concat('...'):depData.description)]);
}
break;
case 'default-noColor':
if (this.options.lines) {
console.log(depData.name.concat(' ; ').concat(typeof depData.homepage == 'undefined'?'¬.¬ --> unknown, not available?':depData.homepage).concat(' ; ').concat(depData.description));
} else {
table.push([depData.name,typeof depData.homepage == 'undefined'?'¬.¬ --> unknown, not available?':depData.homepage,depData.description.length>70?depData.description.substring(0,70).concat('...'):depData.description]);
}
break;
case 'verbose-noColor':
if (this.options.lines) {
console.log(depData.name.concat(' ; ').concat(depData['dist-tags'].latest).concat(' ; ').concat(depData.homepage).concat(' ; ').concat(depData.description));
} else {
table.push([depData.name.toString(),depData['dist-tags'].latest.toString(),typeof depData.homepage == 'undefined'?'¬.¬ --> unknown, not available?':depData.homepage,depData.description.length>70?depData.description.substring(0,70).concat('...'):depData.description]);
}
break;
default:
if (this.options.lines) {
console.log(colors.gray(depData.name).concat(' ; ').concat(colors.underline(depData.homepage).concat(' ; ').concat(colors.cyan(depData.description))));
} else {
table.push([colors.gray(depData.name),colors.underline(depData.homepage),colors.cyan(depData.description.length>70?depData.description.substring(0,70).concat('...'):depData.description)]);
}
break;
}
}
var snoopm = (options) => {
try {
spinner = new Spinner('snooping.. %s');
spinner.setSpinnerString('==^^^^==||__');
this.options = options;
if (typeof this.options.lines == "undefined") {
spinner.start();
}
if (this.options.args.length === 0 ||
this.options.args[0] == '.') {
readPackage(require(process.cwd().concat('/package.json')));
} else {
if (path.basename(this.options.args[0]).trim() === 'package.json') {
if (this.options.args[0].indexOf('.') === 0 ||
this.options.args[0].indexOf('/') === 0
) {
readPackage(require(this.options.args[0]));
}
/**
* url to json raw provided
*/
if (this.options.args[0].indexOf('http') === 0) {
var url = this.options.args[0];
if (url.indexOf('github.com') > 0) {
url = url.replace('github.com','raw.githubusercontent.com');
url = url.replace('blob/','');
}
requesting(url);
}
}
/**
* trying yo read package.json from git hub repository and master branch
*/
else if (this.options.args[0].indexOf('http') === 0) {
var url = this.options.args[0];
var urlArr = url.split('\/');
if (url.indexOf('github.com') > 0 && urlArr.length === 5) {
url = url.replace('github.com','raw.githubusercontent.com');
requesting(url.concat('/').concat('master').concat('/').concat('package.json'));
}
} else {
throw new Error('no valid path or no valid url provided');
}
}
return snoopm;
} catch (e) {
logger.error('Error 41');
logger.error(e);
}
}
module.exports = snoopm;