mirror of
https://github.com/vvsviridov/enm-cli.git
synced 2025-10-23 08:22:21 +00:00
update check
This commit is contained in:
178
bin/enm-cli.js
178
bin/enm-cli.js
@@ -4,6 +4,10 @@ const { Command, Option } = require('commander')
|
||||
const pkg = require('../package.json')
|
||||
const inquirer = require('inquirer')
|
||||
const path = require('path')
|
||||
const { exec } = require('child_process')
|
||||
const { promisify } = require('util')
|
||||
const semver = require('semver')
|
||||
const chalk = require('chalk')
|
||||
|
||||
const { isEmpty } = require('../util/validation')
|
||||
|
||||
@@ -17,106 +21,130 @@ const ShellTerminal = require('../lib/applications/ShellTerminal/ShellTerminal')
|
||||
const logError = require('../util/logError')
|
||||
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
process.on('uncaughtException', (exception) => console.log(exception))
|
||||
process.on('uncaughtException', (exception) => console.log(exception))
|
||||
}
|
||||
|
||||
|
||||
const executeCommand = promisify(exec)
|
||||
|
||||
async function checkVersion() {
|
||||
try {
|
||||
const { stdout } = await executeCommand(`npm view ${pkg.name} version`)
|
||||
if (semver.lt(pkg.version, stdout.trim())) {
|
||||
console.log(`
|
||||
The version ${chalk.red(pkg.version)} of ${chalk.bold(pkg.name)} is out of date.
|
||||
The latest version is ${chalk.green(stdout.trim())}
|
||||
To update, run:
|
||||
|
||||
${chalk.bold('npm update -g ' + pkg.name)}
|
||||
`)
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`Error occurred while check for update: ${error.message}`)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
const applications = [
|
||||
{
|
||||
id: 'tplg',
|
||||
appClass: TopologyBrowser,
|
||||
name: 'Topology Browser',
|
||||
},
|
||||
{
|
||||
id: 'prvn',
|
||||
appClass: AutoProvisioning,
|
||||
name: 'Auto Provisioning',
|
||||
},
|
||||
{
|
||||
id: 'bulk',
|
||||
appClass: BulkImport,
|
||||
name: 'Bulk Import',
|
||||
},
|
||||
{
|
||||
id: 'shll',
|
||||
appClass: ShellTerminal,
|
||||
name: 'Shell Terminal',
|
||||
},
|
||||
{
|
||||
id: 'tplg',
|
||||
appClass: TopologyBrowser,
|
||||
name: 'Topology Browser',
|
||||
},
|
||||
{
|
||||
id: 'prvn',
|
||||
appClass: AutoProvisioning,
|
||||
name: 'Auto Provisioning',
|
||||
},
|
||||
{
|
||||
id: 'bulk',
|
||||
appClass: BulkImport,
|
||||
name: 'Bulk Import',
|
||||
},
|
||||
{
|
||||
id: 'shll',
|
||||
appClass: ShellTerminal,
|
||||
name: 'Shell Terminal',
|
||||
},
|
||||
]
|
||||
const appIds = applications.map(item => item.id)
|
||||
|
||||
const program = new Command()
|
||||
program
|
||||
.version(pkg.version)
|
||||
.addOption(new Option('-l, --login <letters>', 'ENM User Login').env('LOGIN'))
|
||||
.addOption(new Option('-p, --password <letters>', 'ENM User Password').env('PASSWORD'))
|
||||
.addOption(new Option('-a, --application <letters>', 'Start specified application')
|
||||
.choices(appIds)
|
||||
)
|
||||
.requiredOption('-u, --url <valid URL>', 'ENM Url')
|
||||
.parse(process.argv)
|
||||
.version(pkg.version)
|
||||
.addOption(new Option('-l, --login <letters>', 'ENM User Login').env('LOGIN'))
|
||||
.addOption(new Option('-p, --password <letters>', 'ENM User Password').env('PASSWORD'))
|
||||
.addOption(new Option('-a, --application <letters>', 'Start specified application')
|
||||
.choices(appIds)
|
||||
)
|
||||
.requiredOption('-u, --url <valid URL>', 'ENM Url')
|
||||
.parse(process.argv)
|
||||
|
||||
|
||||
const options = program.opts()
|
||||
|
||||
|
||||
async function promptUsername() {
|
||||
const input = await inquirer.prompt([
|
||||
{
|
||||
type: 'input',
|
||||
name: 'value',
|
||||
prefix: '👤',
|
||||
message: 'Type ENM login:',
|
||||
validate: isEmpty,
|
||||
}
|
||||
])
|
||||
return input.value
|
||||
const input = await inquirer.prompt([
|
||||
{
|
||||
type: 'input',
|
||||
name: 'value',
|
||||
prefix: '👤',
|
||||
message: 'Type ENM login:',
|
||||
validate: isEmpty,
|
||||
}
|
||||
])
|
||||
return input.value
|
||||
}
|
||||
|
||||
|
||||
async function promptPassword() {
|
||||
const input = await inquirer.prompt([
|
||||
{
|
||||
type: 'password',
|
||||
name: 'value',
|
||||
prefix: '🔑',
|
||||
message: 'Type ENM password:',
|
||||
validate: isEmpty,
|
||||
}
|
||||
])
|
||||
return input.value
|
||||
const input = await inquirer.prompt([
|
||||
{
|
||||
type: 'password',
|
||||
name: 'value',
|
||||
prefix: '🔑',
|
||||
message: 'Type ENM password:',
|
||||
validate: isEmpty,
|
||||
}
|
||||
])
|
||||
return input.value
|
||||
}
|
||||
|
||||
|
||||
async function selectApplication() {
|
||||
let selectedApp
|
||||
if (options.application && appIds.includes(options.application)) {
|
||||
selectedApp = options.application
|
||||
} else {
|
||||
const input = await inquirer.prompt([
|
||||
{
|
||||
type: 'list',
|
||||
name: 'application',
|
||||
prefix: '💾',
|
||||
message: 'Select Application:',
|
||||
choices: applications.map(item => ({ name: item.name, value: item.id, short: item.id }))
|
||||
}])
|
||||
selectedApp = input.application
|
||||
}
|
||||
const { appClass } = applications.find(item => item.id === selectedApp)
|
||||
const login = options.login || await promptUsername()
|
||||
const password = options.password || await promptPassword()
|
||||
return new appClass(login, password, options.url)
|
||||
let selectedApp
|
||||
if (options.application && appIds.includes(options.application)) {
|
||||
selectedApp = options.application
|
||||
} else {
|
||||
const input = await inquirer.prompt([
|
||||
{
|
||||
type: 'list',
|
||||
name: 'application',
|
||||
prefix: '💾',
|
||||
message: 'Select Application:',
|
||||
choices: applications.map(item => ({ name: item.name, value: item.id, short: item.id }))
|
||||
}])
|
||||
selectedApp = input.application
|
||||
}
|
||||
const { appClass } = applications.find(item => item.id === selectedApp)
|
||||
const login = options.login || await promptUsername()
|
||||
const password = options.password || await promptPassword()
|
||||
return new appClass(login, password, options.url)
|
||||
}
|
||||
|
||||
async function main() {
|
||||
try {
|
||||
const app = await selectApplication()
|
||||
await app.login()
|
||||
await app.inputHandler()
|
||||
await app.logout()
|
||||
} catch (error) {
|
||||
logError(error)
|
||||
}
|
||||
try {
|
||||
if (!process.env.DISABLE_UPDATE_CHECK) {
|
||||
await checkVersion()
|
||||
}
|
||||
const app = await selectApplication()
|
||||
await app.login()
|
||||
await app.inputHandler()
|
||||
await app.logout()
|
||||
} catch (error) {
|
||||
logError(error)
|
||||
}
|
||||
}
|
||||
|
||||
main()
|
||||
|
Reference in New Issue
Block a user