-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathcli.js
More file actions
72 lines (67 loc) · 1.96 KB
/
cli.js
File metadata and controls
72 lines (67 loc) · 1.96 KB
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
import authenticate from './commands/authenticate.js'
import create from './commands/create.js'
import devinstall from './commands/devinstall.js'
import help from './commands/help.js'
import install from './commands/install.js'
import ls from './commands/ls.js'
import register from './commands/register.js'
import rename from './commands/rename.js'
import search from './commands/search.js'
import uninstall from './commands/uninstall.js'
import unregister from './commands/unregister.js'
import update from './commands/update.js'
import version from './commands/version.js'
import logger from './logger.js'
import { isNPM } from './integration/PluginManagement/npm.js'
import './econnreset.js'
const commands = {
authenticate,
create,
devinstall,
help,
install,
ls,
register,
rename,
search,
uninstall,
unregister,
update,
version
}
const translationTable = [
{ pattern: /^-v$|^--version$/i, replacement: 'version' },
{ pattern: /^upgrade$/i, replacement: 'update' }
]
class CLI {
withOptions (argv = process.argv || ['node', 'path']) {
const parameters = argv.slice(2).map(param => {
const translation = translationTable.find(item => item.pattern.test(param))
return translation ? translation.replacement : param
})
const name = parameters.length
? String.prototype.toLowerCase.call(parameters.shift())
: ''
this.command = {
name,
parameters
}
return this
}
async execute () {
try {
logger.info(`using ${await isNPM() ? 'NPM' : 'BOWER'} registry...`)
if (!commands[this.command.name]) {
const e = new Error(`Unknown command "${this.command.name}", please check the documentation.`)
logger?.log(e.message)
throw e
}
const commandArguments = [logger].concat(this.command.parameters)
await commands[this.command.name](...commandArguments)
} catch (err) {
console.error(err)
process.exit(err ? 1 : 0)
}
}
}
export default new CLI()