-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cli.js
executable file
·195 lines (171 loc) · 3.86 KB
/
cli.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
#!/usr/bin/env node
'use strict'
const co = require('co')
const minimist = require('minimist')
const toArray = require('object2array')
const isTaken = require('is-taken')
const update = require('update-notifier')
const blessed = require('blessed')
const giturl = require('giturl')
const pkg = require('./package')
require('colorful').toxic()
let screen
let spin
function isModule(name) {
return name !== '.' && name !== './'
}
function getRepoURL(repo) {
if (repo.repository) {
return giturl.parse(repo.repository.url)
}
return `https://npmjs.org/package/${repo.name}`
}
function leave(msg, code) {
if (screen) {
screen.destroy()
}
console.log(msg)
process.exit(code || 0)
}
const argv = minimist(process.argv.slice(2), {
alias: {
h: 'help',
v: 'version',
r: 'registry',
c: 'cn',
d: 'dev'
}
})
// update notify
update({pkg}).notify()
if (argv.help) {
console.log(`
Usage:
decheck <moduleName> [moduleVersion]
-d/--dev: Check the devDependencies
-r/--registry: Set custom npm registry
-c/--cn: Set npm registry to China mirror
-v/--version: Print version
-h/--help: Print help
`)
process.exit()
}
if (argv.version) {
console.log(pkg.version)
process.exit()
}
let moduleName = argv._[0] || '.'
const moduleVersion = argv._[1]
const field = argv.dev ? 'devDependencies' : 'dependencies'
let registry = argv.registry || 'https://registry.npmjs.org/'
if (argv.cn) {
registry = 'https://registry.npm.taobao.org/'
}
co(function* () {
let deps
let version
let pkg
let packageName = moduleName
if (!isModule(moduleName)) {
try {
pkg = require(process.cwd() + '/package.json')
packageName = pkg.name
version = pkg.version
deps = pkg[field]
} catch (e) {}
}
screen = blessed.screen({
smartCSR: true,
autoPadding: true,
warnings: true
})
spin = blessed.loading({
parent: screen,
hidden: true,
border: {
type: 'line'
},
top: 'center',
left: 'center',
width: 'half',
height: 'shrink',
label: ` {green-fg}${packageName}{/green-fg} `,
tags: true,
keys: true
})
spin.load('Retriving data...')
if (isModule(moduleName)) {
pkg = yield isTaken(moduleName, {version: moduleVersion})
if (pkg) {
if (pkg.version) {
version = pkg.version
deps = pkg[field]
} else {
version = pkg['dist-tags'] && pkg['dist-tags']['latest']
deps = pkg.versions[version][field]
}
} else {
leave(`Sorry, package ${moduleName} is not registered on npm.`, 1)
}
}
if (!deps) {
leave(`Sorry, but this package has no ${field}`, 1)
}
deps = toArray(deps)
const depsData = yield deps.map(dep => {
return isTaken(dep.key, {
version: dep.value,
registry,
timeout: 10000
})
})
spin.stop()
// display screen
screen.title = packageName
const box = blessed.box({
parent: screen,
top: 'center',
left: 'center',
width: '80%',
height: '80%',
scrollable: true,
scrollbar: {
ch: ' ',
inverse: true
},
vi: true,
padding: 1,
keys: true,
content: `${`${packageName}${version ? `(${version})` : ''} has ${deps.length} ${field}`.yellow}\n${pkg.description ? pkg.description.white + '\n' : ''}` + depsData.map(dep => {
return `
${dep.name.white.bold} ${'v'.gray}${dep.version.gray}
${dep.description.cyan}
${getRepoURL(dep).gray}
`
}).join(''),
tags: true,
border: {
type: 'line'
},
alwaysScroll: true,
style: {
fg: 'black',
bg: 'black',
border: {
fg: '#f0f0f0'
},
scrollbar: {
bg: '#ccc',
fg: 'blue'
}
}
})
screen.append(box)
screen.key(['escape', 'q', 'C-c'], (ch, key) => {
return process.exit(0)
})
box.focus()
screen.render()
}).catch(e => {
leave(e.stack)
})