-
Notifications
You must be signed in to change notification settings - Fork 0
/
drCLI.js
260 lines (247 loc) · 7.43 KB
/
drCLI.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
#!/usr/bin/env node
const fs = require('fs')
const path = require('path')
const { Command } = require('commander')
const inquirer = require('inquirer')
const createDoctor = require('./doctor')
const createReporter = require('./reporter')
const MAPFILE_NAME = 'dltracker.json'
const program = new Command()
const { version: pkgVersion } = require('./package.json')
program.version(pkgVersion)
let currPath
let dr
function reevaluateState(dir) {
return createDoctor(dir)
.then(doctor => {
dr = doctor
currData = doctor.data()
if (currData.length) {
const reporter = createReporter(doctor)
return reporter.report()
}
console.log("\nThis download set is in good health!\nNo changes needed.")
return false
})
}
function requestConfirmation(msg) {
const qName = 'yesNoQ'
return inquirer.prompt([
{
type: 'confirm', name: qName,
message: msg
}
])
.then(answers => answers[qName])
}
function mainMenu() {
const menuName = 'actions'
const choices = [
{ key: 'l', name: 'List current errors', value: 'l' },
{ key: 'x', name: 'eXit', value: 'x' }
]
const missingList = dr.getErroredPkgs('semver', 'ENOENT')
.concat(dr.getErroredPkgs('git', 'ENOENT'))
.concat(dr.getErroredPkgs('url', 'ENOENT'))
if (missingList.length) {
choices.splice(
-1, 0, { key: 'm', name: 'remove records of Missing tarballs', value: 'm' }
)
}
const zeroList = dr.getErroredPkgs('semver', 'EFZEROLEN')
.concat(dr.getErroredPkgs('git', 'EFZEROLEN'))
.concat(dr.getErroredPkgs('url', 'EFZEROLEN'))
if (zeroList.length) {
choices.splice(
-1, 0, { key: 'z', name: 'remove records of Zero-length tarballs', value: 'z' }
)
}
const noFilenameList = dr.getErroredPkgs('semver', 'ENODATA')
.concat(dr.getErroredPkgs('git', 'ENODATA'))
.concat(dr.getErroredPkgs('url', 'ENODATA'))
if (noFilenameList.length) {
choices.splice(
-1, 0, { key: 'f', name: 'remove records missing Filename', value: 'f' }
)
}
const orphanedRefList = dr.getOrphanedRefs('tag')
.concat(dr.getOrphanedRefs('git'))
if (orphanedRefList.length) {
choices.splice(
-1, 0, { key: 'o', name: 'remove records of Orphaned tags/refs', value: 'o' }
)
}
const noVersionList = dr.getErroredPkgs('tag', 'ENODATA')
.concat(dr.getErroredPkgs('git', 'ENODATA'))
if (noVersionList.length) {
choices.splice(
-1, 0, { key: 'v', name: 'remove tag/git ref records with no Version', value: 'v' }
)
}
if (dr.isChanged()) {
choices.splice(
-1, 0, { key: 's', name: 'Save changes', value: 's' }
)
}
return inquirer.prompt([
{
type: 'expand', name: menuName, message: 'Action:',
choices: choices
}
])
.then(answers => {
const menuChoice = answers[menuName]
let result
switch (menuChoice) {
case 'l':
result = Promise.resolve(createReporter(dr).report())
break;
case 'm':
result = requestConfirmation(
'Confirm: remove records for all missing tarballs?'
)
.then(yes => {
if (yes) {
dr.markResolved(missingList)
}
return true
})
break;
case 'z':
result = requestConfirmation(
'Confirm: remove records for all zero-length tarballs?'
)
.then(yes => {
if (yes) {
dr.markResolved(zeroList)
console.log(
"The records are removed.\nYou should delete the zero-length files."
)
}
return true
})
break;
case 'f':
result = requestConfirmation(
'Confirm: remove records that have no filename?'
)
.then(yes => {
if (yes) {
dr.markResolved(noFilenameList)
}
return true
})
break;
case 'o':
result = requestConfirmation(
'Confirm: remove records for all orphaned tags/refs?'
)
.then(yes => {
if (yes) {
dr.markResolved(orphanedRefList)
}
return true
})
break;
case 'v':
result = requestConfirmation(
'Confirm: remove tag/git ref records that have no version?'
)
.then(yes => {
if (yes) {
dr.markResolved(noVersionList)
}
return true
})
break;
case 's':
result = dr.saveState()
.catch(err => {
console.error("Failed to save changes:", err.message)
process.exitCode = 3;
throw err
})
.then(() => {
console.log("\nChanges saved.\n")
return reevaluateState(currPath)
.catch(err => {
console.error("Failed to refresh the tracker:", err.message)
process.exitCode = 4;
throw err
})
})
.catch(err => {
if (!process.exitCode) process.exitCode = 5;
console.error("Exiting.")
return false
})
break;
case 'x':
if (dr.isChanged()) {
result = requestConfirmation(
'There are unsaved changes. Do you really want to exit?'
)
.then(abort => abort ? false : true)
}
break;
}
return result
})
.then(stay =>
stay ? mainMenu() : null
)
}
program
.option('-r, --report-only', 'report problems and exit immediately')
.arguments('[where]')
.action(function(where) {
if (!where)
console.warn('No location given. Using current directory.')
where = where ? path.resolve(where) : path.resolve(MAPFILE_NAME)
if (!where.endsWith(MAPFILE_NAME))
where = path.join(where, MAPFILE_NAME)
currPath = path.parse(where).dir
// First we check that a dltracker.json exists in the given directory,
// because the tracker will not tell us; it will automatically try to
// rebuild one based on the tarballs found there.
fs.stat(where, function (err, stats) {
if (err) {
if (err.code == 'ENOENT')
console.error(`Failed to find ${MAPFILE_NAME} at given path.`)
else if (err.code == 'EACCES' || err.code == 'EPERM')
console.error('You do not have permission to read this directory.')
else
console.error(err.code ? err.code + ':' : '', err.message)
process.exitCode = 1;
return
}
if (!stats.isFile()) {
// ERROR_FILE_EXISTS == 0x00000050 EEXIST
// ERROR_BAD_ARGUMENTS == 0x000000A0 "One or more arguments are not correct."
// ERROR_BAD_PATHNAME == 0x000000A1 "The specified path is invalid."
// there is an errno named EISDIR (according to nodejs.org)
console.error(`${MAPFILE_NAME} at this location is not a file.`)
process.exitCode = 0xA0;
return
}
return Promise.resolve(program.reportOnly)
.then(reportOnly => {
const resultPromise = reevaluateState(currPath)
return reportOnly ? false : resultPromise
})
.catch(err => {
if (err.code == 'EACCES' || err.code == 'EPERM') {
console.error(`You don't have permission to access ${MAPFILE_NAME} in this directory.`)
process.exitCode = 2;
}
else {
console.error(err)
console.error(err.code ? err.code + ':' : '', err.message)
process.exitCode = 0xFF;
}
return false
})
.then(stay => stay ? mainMenu() : null)
})
})
program.parse(process.argv)