@vonage/cli-core
Advanced tools
Comparing version 2.0.1 to 2.1.0
@@ -13,4 +13,6 @@ "use strict"; | ||
const chalk_1 = __importDefault(require("chalk")); | ||
const error_1 = require("../../config/error"); | ||
class SetConfig extends baseSetCommand_1.BaseSetCommand { | ||
async run() { | ||
super.run(); | ||
const { global } = this.flags; | ||
@@ -22,5 +24,3 @@ const checkFile = global | ||
if (!(0, fs_1.pathExists)(checkFile)) { | ||
this.log(`You need to run "${chalk_1.default.green('vonage config:setup')}${global ? chalk_1.default.green(' --global') : ''}" before you can set a value`); | ||
process.exit(1); | ||
return; | ||
throw new error_1.ConfigFileMissing(); | ||
} | ||
@@ -36,2 +36,3 @@ const { setting, value } = this.args; | ||
: await this.vonageConfig.saveLocalConfig(this.flags.yes); | ||
/* istanbul ignore next */ | ||
this.log(result | ||
@@ -38,0 +39,0 @@ ? 'Config file updated! ✅' |
@@ -6,3 +6,2 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const fs_1 = require("fs"); | ||
const baseSetCommand_1 = require("../../config/baseSetCommand"); | ||
@@ -26,2 +25,3 @@ const index_1 = require("../../enums/index"); | ||
: await this.vonageConfig.saveLocalConfig(this.flags.yes); | ||
/* istanbul ignore next */ | ||
this.log(result | ||
@@ -44,11 +44,10 @@ ? 'Config file saved! ✅' | ||
welcomeFile(file) { | ||
if ((0, fs_1.existsSync)(file)) { | ||
this.log(`The config file: ${file} with be updated`); | ||
return; | ||
} | ||
this.log(`The ${file} with be created`); | ||
/* istanbul ignore next */ | ||
this.fs.pathExists(file) | ||
? this.log(`The config file: ${file} will be updated`) | ||
: this.log(`The ${file} will be created`); | ||
} | ||
} | ||
SetupConfig.summary = 'Vonage CLI configuration wizard'; | ||
SetupConfig.description = `This wizard will setup a config file for the CLI | ||
SetupConfig.description = `This wizard will setup a config file for the Vonage CLI | ||
@@ -55,0 +54,0 @@ You can use the command line flags to skip interactive mode`; |
@@ -15,3 +15,3 @@ import { VonageCommand } from '../../vonageCommand'; | ||
protected dumpConfig(from?: ConfigParts | null): void; | ||
protected echoSetting(setting: ConfigParams, part: ConfigParts | null, value: string | null): string; | ||
protected echoSetting(setting: ConfigParams, part: ConfigParts | null, value: string | null, noColor?: boolean): string; | ||
} |
@@ -30,5 +30,6 @@ "use strict"; | ||
truncatePrivateKey(privateKey) { | ||
/* istanbul ignore next */ | ||
return (0, fs_1.existsSync)(privateKey) | ||
? privateKey | ||
: this.ux.dumpValue(this.ux.truncateString(privateKey)); | ||
: this.ux.truncateString(privateKey); | ||
} | ||
@@ -51,10 +52,10 @@ dumpConfig(from = null) { | ||
if (setting.includes(index_1.ConfigParams.PRIVATE_KEY)) { | ||
this.log(this.echoSetting(index_1.ConfigParams.PRIVATE_KEY, from, this.truncatePrivateKey(this.vonageConfig.getVariableFrom(index_1.ConfigParams.PRIVATE_KEY, from)))); | ||
this.log(this.echoSetting(index_1.ConfigParams.PRIVATE_KEY, from, this.truncatePrivateKey(this.vonageConfig.getVariableFrom(index_1.ConfigParams.PRIVATE_KEY, from)), true)); | ||
} | ||
this.log(''); | ||
} | ||
echoSetting(setting, part, value) { | ||
echoSetting(setting, part, value, noColor = false) { | ||
return (chalk_1.default.bold(`The ${part ? `${part} ` : ''}${index_1.DisplayedSetting[setting]} is`) | ||
+ ': ' | ||
+ this.ux.dumpValue(value)); | ||
+ (noColor ? value : this.ux.dumpValue(value))); | ||
} | ||
@@ -61,0 +62,0 @@ } |
@@ -10,3 +10,4 @@ import { Command } from '@oclif/core'; | ||
static enableJsonFlag: boolean; | ||
run(): Promise<void>; | ||
protected getNewSetting(setting: ConfigParams, append?: string): Promise<void>; | ||
} |
@@ -13,3 +13,13 @@ "use strict"; | ||
const lodash_startcase_1 = __importDefault(require("lodash.startcase")); | ||
const error_1 = require("./error"); | ||
const ux_1 = require("../ux"); | ||
class BaseSetCommand extends vonageCommand_1.VonageCommand { | ||
async run() { | ||
const addGlobal = this.flags.global ? ' --global' : ''; | ||
this.errors = { | ||
[error_1.ConfigFileMissing.name]: [ | ||
`You need to run ${(0, ux_1.dumpCommand)(`vonage config:setup${addGlobal}`)} before you can set a value`, | ||
], | ||
}; | ||
} | ||
async getNewSetting(setting, append = '') { | ||
@@ -16,0 +26,0 @@ const argumentSetting = this.vonageConfig.getArgVar(setting); |
export declare const loadFile: (file: string) => string | null; | ||
export declare const loadJSON: (file: string) => unknown; | ||
export declare const pathExists: (path: string) => boolean; | ||
@@ -3,0 +4,0 @@ export declare const checkDirectoryExistsForFile: (file: string) => boolean; |
@@ -6,5 +6,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.saveFile = exports.makeDirectory = exports.makeDirectoryForFile = exports.checkDirectoryExistsForFile = exports.pathExists = exports.loadFile = void 0; | ||
/* istanbul ignore file: Mocking the filesystem is not fun */ | ||
const fs_1 = require("fs"); | ||
exports.saveFile = exports.makeDirectory = exports.makeDirectoryForFile = exports.checkDirectoryExistsForFile = exports.pathExists = exports.loadJSON = exports.loadFile = void 0; | ||
const core_1 = require("@oclif/core"); | ||
@@ -15,21 +13,27 @@ const path_1 = require("path"); | ||
const ux_1 = require("./ux"); | ||
const path_2 = require("path"); | ||
const fsUtils_1 = require("./fsUtils"); | ||
const log = (0, debug_1.default)('vonage:cli:fs'); | ||
const loadFile = (file) => { | ||
const normalFile = (0, path_2.normalize)(file); | ||
log(`Loading file ${file}`); | ||
if (!(0, exports.pathExists)(file)) { | ||
if (!(0, exports.pathExists)(normalFile)) { | ||
log(`${file} does not exist`); | ||
return null; | ||
} | ||
const fileContents = (0, fs_1.readFileSync)(file).toString(); | ||
log(`Contents of ${file}: `, fileContents); | ||
const fileContents = (0, fsUtils_1.readFile)(normalFile); | ||
log(`Contents of ${normalFile}: `, fileContents); | ||
return fileContents; | ||
}; | ||
exports.loadFile = loadFile; | ||
const loadJSON = (file) => JSON.parse((0, exports.loadFile)(file) || '{}'); | ||
exports.loadJSON = loadJSON; | ||
const pathExists = (path) => { | ||
log(`Checking if ${path} exists`); | ||
if ((0, fs_1.existsSync)(path)) { | ||
log(`${path} exists`); | ||
const normalPath = (0, path_2.normalize)(path); | ||
log(`Checking if ${normalPath} exists`); | ||
if ((0, fsUtils_1.dirExists)(normalPath)) { | ||
log(`${normalPath} exists`); | ||
return true; | ||
} | ||
log(`${path} does not exist`); | ||
log(`${normalPath} does not exist`); | ||
return false; | ||
@@ -60,3 +64,3 @@ }; | ||
} | ||
(0, fs_1.mkdirSync)(directory, { recursive: true }); | ||
(0, fsUtils_1.createDir)(directory); | ||
log('Directory created'); | ||
@@ -67,9 +71,10 @@ return true; | ||
const saveFile = async (file, data, force = false) => { | ||
log(`Saiving file ${file}. Forcing? ${force}`); | ||
const fileExistsWarning = (0, exports.pathExists)(file) | ||
const normalFile = (0, path_2.normalize)(file); | ||
log(`Saiving file ${normalFile}. Forcing? ${force}`); | ||
const fileExistsWarning = (0, exports.pathExists)(normalFile) | ||
? chalk_1.default.yellow('This will overwrite the file') | ||
: chalk_1.default.yellow('This will create the file'); | ||
let okToWrite = false; | ||
let okToWrite = true; | ||
if (!force) { | ||
core_1.ux.log('Confirm saving'); | ||
core_1.ux.log('Confirm saving:'); | ||
core_1.ux.log((0, ux_1.dumpValue)(data)); | ||
@@ -92,4 +97,4 @@ okToWrite = await core_1.ux.confirm(`To ${file} (${fileExistsWarning}) [y/n]`); | ||
const dataToWrite = typeof data !== 'string' ? JSON.stringify(data, null, 2) : data; | ||
(0, fsUtils_1.writeFile)(file, dataToWrite); | ||
log(`Saiving file ${file}`); | ||
(0, fs_1.writeFileSync)(file, dataToWrite); | ||
log(`File written`); | ||
@@ -96,0 +101,0 @@ return true; |
export declare const dumpValue: (value: Array<unknown> | string | number | unknown) => string; | ||
export declare const dumpCommand: (command: string) => string; | ||
export declare const truncateString: (value: string, length?: number) => string; |
@@ -6,3 +6,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.truncateString = exports.dumpValue = void 0; | ||
exports.truncateString = exports.dumpCommand = exports.dumpValue = void 0; | ||
const chalk_1 = __importDefault(require("chalk")); | ||
@@ -66,2 +66,4 @@ const flags = { truncate: true }; | ||
exports.dumpValue = dumpValue; | ||
const dumpCommand = (command) => chalk_1.default.green(command); | ||
exports.dumpCommand = dumpCommand; | ||
const truncateString = (value, length = 25) => flags.truncate && `${value}`.length > length | ||
@@ -68,0 +70,0 @@ ? (0, exports.dumpValue)(value.substring(0, length)) + chalk_1.default.dim(' ...value truncated') |
@@ -16,2 +16,3 @@ import { Command, Interfaces } from '@oclif/core'; | ||
}; | ||
protected errors: Record<string, Array<string>>; | ||
protected vonageConfig: VonageConfig; | ||
@@ -25,3 +26,3 @@ protected flags: VonageFlags<T>; | ||
exitCode?: number; | ||
}): Promise<any>; | ||
}): Promise<void>; | ||
} |
@@ -25,5 +25,2 @@ "use strict"; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -33,7 +30,5 @@ exports.VonageCommand = void 0; | ||
const vonageConfig_1 = require("./vonageConfig"); | ||
const index_1 = require("./enums/index"); | ||
const errorHelp_1 = require("./errorHelp"); | ||
const ux = __importStar(require("./ux")); | ||
const fs = __importStar(require("./fs")); | ||
const chalk_1 = __importDefault(require("chalk")); | ||
const jwt_1 = require("@vonage/jwt"); | ||
class VonageCommand extends core_1.Command { | ||
@@ -57,38 +52,12 @@ constructor() { | ||
} | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
async catch(err) { | ||
switch (err.constructor.name) { | ||
case jwt_1.InvalidPrivateKeyError.name: | ||
this.log('The private key is invalid!'); | ||
this.log(`Check that you have the correcte private key and run this command again`); | ||
this.log(`${chalk_1.default.bold('Note:')} The private key can be a path to the file or the value of the private key`); | ||
return; | ||
case jwt_1.MissingPrivateKeyError.name: | ||
this.log('You do not have the private key set!'); | ||
this.log(`${chalk_1.default.bold('Note:')} The private key can be a path to the file or the value of the private key`); | ||
this.log(''); | ||
this.log('To fix this error you can:'); | ||
this.log(`1. Run ${chalk_1.default.green('vonage config:set private-key <value>')}`); | ||
this.log(`2. Run this command again and pass in the private key using ${chalk_1.default.green('--private-key=<value>')}`); | ||
this.log(`3. Set the ${chalk_1.default.green(index_1.ConfigEnv.PRIVATE_KEY)} environment variable`); | ||
return; | ||
case jwt_1.MissingApplicationIdError.name: | ||
this.log('You do not have an application id set!'); | ||
this.log(''); | ||
this.log('To fix this error you can:'); | ||
this.log(`1. Run ${chalk_1.default.green('vonage config:set application-id <value>')}`); | ||
this.log(`2. Run this command again and pass in the application id using ${chalk_1.default.green('--application-id=<value>')}`); | ||
this.log(`3. Set the ${chalk_1.default.green(index_1.ConfigEnv.APPLICATION_ID)} environment variable`); | ||
return; | ||
case jwt_1.InvalidApplicationIdError.name: | ||
this.log('The application id is invalid!'); | ||
this.log(''); | ||
this.log('Check that you have the correct application id and try again'); | ||
return; | ||
default: | ||
this.log(`An error occurred: ${err.message}`); | ||
this.log(''); | ||
} | ||
const messages = { | ||
...errorHelp_1.commonErrors, | ||
...this.errors, | ||
}[err.constructor.name] || [`An error occurred: ${err.message}`]; | ||
messages.forEach(function (msg) { | ||
this.log(msg); | ||
}.bind(this)); | ||
this.log(''); | ||
this.log('You can set DEBUG=* for more information'); | ||
return super.catch(err); | ||
} | ||
@@ -95,0 +64,0 @@ } |
@@ -12,7 +12,8 @@ "use strict"; | ||
const update_1 = require("./config/update"); | ||
const path_1 = require("path"); | ||
const log = (0, debug_1.default)('vonage:cli:config'); | ||
class VonageConfig { | ||
constructor(configDir, flags) { | ||
this.localConfigFile = `${process.cwd()}/vonage_app.json`; | ||
this.globalConfigFile = `${configDir}/vonage.config.json`; | ||
this.localConfigFile = (0, path_1.normalize)(`${process.cwd()}/vonage_app.json`); | ||
this.globalConfigFile = (0, path_1.normalize)(`${configDir}/vonage.config.json`); | ||
log(`Vonage local config file ${this.localConfigFile}`); | ||
@@ -69,2 +70,3 @@ log(`Vonage config file ${this.globalConfigFile}`); | ||
return this.setConfigVar(which, value); | ||
/* istanbul ignore next */ | ||
default: | ||
@@ -71,0 +73,0 @@ throw new Error(`Cannot set ${which} for ${from}`); |
{ | ||
"name": "@vonage/cli-core", | ||
"version": "2.0.1", | ||
"version": "2.1.0", | ||
"description": "Core utilities for vonage CLI's ", | ||
@@ -54,3 +54,4 @@ "keywords": [ | ||
} | ||
} | ||
}, | ||
"gitHead": "137ec4ad2edd0a00464325e613fd55ea30c60d6d" | ||
} |
@@ -1,11 +0,35 @@ | ||
# `core` | ||
# Vonage Core CLI | ||
> TODO: description | ||
![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/vonage/vonage-cli/ci.yml?branch=3.x) [![Codecov](https://img.shields.io/codecov/c/github/vonage/vonage-cli?label=Codecov&logo=codecov&style=flat-square)](https://codecov.io/gh/Vonage/vonage-cli) ![Latest Release](https://img.shields.io/npm/v/@vonage/cli-core?label=%40vonage%2Fcli&style=flat-square) [![Contributor Covenant](https://img.shields.io/badge/Contributor%20Covenant-v2.0%20adopted-ff69b4.svg?style=flat-square)](../../CODE_OF_CONDUCT.md) [![License](https://img.shields.io/npm/l/@vonage/cli-core?label=License&style=flat-square)][license] | ||
## Usage | ||
<img src="https://developer.nexmo.com/images/logos/vbc-logo.svg" height="48px" alt="Vonage" /> | ||
``` | ||
const core = require('core'); | ||
This is the core package for all Vonage CLI commands. It contians the base `VonageCommand`, `VonageConfig`, common User Experience (UX), and Filesystem (fs) functionality. Since this pacakge contains the config functionality, it also adds configuration commands to the CLI. | ||
// TODO: DEMONSTRATE API | ||
``` | ||
## VonageCommand | ||
The `VonageCommand` base class, provides global CLI arguments which can be used to configure [`@vonage/auth`](https://github.com/Vonage/vonage-node-sdk/tree/3.x/packages/auth) and [`@vonage/server-sdk`](https://github.com/Vonage/vonage-node-sdk/tree/3.x/packages/server-sdk). Along with the configuration, standard UX functions for outputting data is also provided. Keeping a consistent UX is pivitol for the CLI so it is recommended that you use these functions. | ||
## Configuration | ||
Config settings are parsed into the CLI in four ways (in order): | ||
1. From command line arguments, (`--api-key`,`--api-secret`, `--private-key`, and `--application-id`) | ||
1. Reading in from `$CWD/vonage_app.json` file | ||
1. The `VONAGE_API_KEY`, `VONAGE_API_SECRET`, `VONAGE_PRIVATE_KEY`, and `VONAGE_APPLICATION_ID` environment variables | ||
1. A Global config file loacated at `$XDG_CONFIG_HOME/@vonage/cli/vonage.config.json` | ||
### Configuration API | ||
By having your command extend `VonageCommand` you will get access to the base configuration parameters. It is recommened that you do not interact While you shouldn't need to access variables directly, the best way is to use `vonageConfig.getVar(<which>)` from the `VonageCommand` base class. `VonageConfig` also exposes methods from saving the global or local config (again these are use at your own risk) | ||
## Commands Provided | ||
1. `vonage config` (`vonage config:setup`, `vonage setup`): Initial configuration setup wizard | ||
1. `vonage config:show`: A utility command that displays the configuration | ||
1. `vonage config:set SETTING VALUE` A utility command to set an individual configuration setting | ||
[license]: LICENSE.txt | ||
[signup]: https://dashboard.nexmo.com/sign-up?utm_source=DEV_REL&utm_medium=github&utm_campaign=node-cli# | ||
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
93478
78
1115
36
10