@axway/axway-central-cli
Version:
Manage APIs, services and publish to the Amplify Marketplace
160 lines (156 loc) • 5.63 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.verifyApigeeXCredentialFile = exports.validateValueRange = exports.validateValidRegex = exports.validateRegex = exports.validateNonEmptyInput = exports.validateInputLength = exports.validateInputIsNew = exports.runValidations = exports.filterEmptyNumberInput = exports.askUsernameAndPassword = exports.askList = exports.askInputValidation = exports.askInput = exports.MAX_FILE_SIZE = void 0;
var _inquirer = _interopRequireDefault(require("inquirer"));
var _fsExtra = require("fs-extra");
var _path = require("path");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
//
// Basic Prompts
//
/**
* @param validators At least one InputValidation func
* @description Executes the provided InputValidation funcs until all are successful, or one returns an error.
* Pass to the askInput for validation when input validation becomes complex. Provides an easy way
* to break down input validation into their own small functions.
*/
const runValidations = (...validators) => input => {
for (const validator of validators) {
const res = validator(input);
if (typeof res === 'string') return res;
}
return true;
};
exports.runValidations = runValidations;
const validateRegex = (regexp, message) => input => {
return input.toString().match(regexp) ? true : message;
};
exports.validateRegex = validateRegex;
const validateInputLength = (length, message) => input => {
return input.toString().length <= length ? true : message;
};
exports.validateInputLength = validateInputLength;
const MAX_FILE_SIZE = exports.MAX_FILE_SIZE = process.env.NODE_ENV === 'test' ? 1e5 : 20 * 1024 * 1024;
const verifyApigeeXCredentialFile = () => input => {
let stats;
let fileExtension = '';
try {
stats = (0, _fsExtra.lstatSync)(input);
fileExtension = (0, _path.extname)(input);
if (!stats.isFile()) {
throw new Error(`Couldn't load the credential file: ${input}`);
} else if (stats.size >= MAX_FILE_SIZE) {
throw new Error(`File size too large`);
} else if (fileExtension !== '.json') {
throw new Error(`File extension is invalid, please provide '.json' file`);
}
return true;
} catch (e) {
throw new Error(`Couldn't find the credential file: ${input}`);
}
};
exports.verifyApigeeXCredentialFile = verifyApigeeXCredentialFile;
const validateValidRegex = () => input => {
try {
new RegExp(input.toString());
} catch (error) {
return 'Please provide a valid regular expression.';
}
return true;
};
exports.validateValidRegex = validateValidRegex;
const validateInputIsNew = (options, error) => input => {
const isFound = options.find(opt => opt === input);
return isFound ? error : true;
};
exports.validateInputIsNew = validateInputIsNew;
const validateValueRange = (lowerLimit, upperLimit) => input => {
const inputNum = Number(input);
if (isNaN(inputNum)) {
return 'Please provide a number.';
}
let msg = '';
if (typeof lowerLimit !== undefined && typeof upperLimit !== undefined) {
msg = `Please provide a number from ${lowerLimit} to ${upperLimit}`;
} else if (typeof lowerLimit !== undefined) {
msg = `Please provide a minimum number of ${lowerLimit}`;
} else if (typeof upperLimit !== undefined) {
msg = `Please provide a maximum number of ${upperLimit}`;
}
if (typeof lowerLimit !== undefined && inputNum < lowerLimit) {
return msg;
}
if (typeof upperLimit !== undefined && inputNum > upperLimit) {
return msg;
}
return true;
};
// exporting for test
exports.validateValueRange = validateValueRange;
const validateNonEmptyInput = input => {
return String(input).length ? true : 'Please provide a non-empty value.';
};
// exporting for test
exports.validateNonEmptyInput = validateNonEmptyInput;
const filterEmptyNumberInput = input => {
// clear the invalid input
return Number.isNaN(input) ? '' : Number(input);
};
exports.filterEmptyNumberInput = filterEmptyNumberInput;
const askInputValidation = (allowEmptyInput, validate) => input => {
if (allowEmptyInput && !String(input).length) {
return true;
}
const isEmpty = allowEmptyInput ? true : validateNonEmptyInput(input);
if (typeof isEmpty === 'string') return isEmpty;
return validate ? validate(input) : true;
};
exports.askInputValidation = askInputValidation;
const askInput = async ({
msg,
defaultValue,
type = 'string',
validate,
allowEmptyInput = false
}) => {
const answers = await _inquirer.default.prompt({
type: type === 'string' ? 'input' : 'number',
name: 'value',
message: `${msg}: `,
default: defaultValue,
validate: askInputValidation(allowEmptyInput, validate),
filter: type === 'number' ? filterEmptyNumberInput : undefined
});
return answers.value;
};
exports.askInput = askInput;
const askList = async opts => {
const answers = await _inquirer.default.prompt({
type: 'list',
name: 'value',
message: `${opts.msg}: `,
choices: opts.choices,
default: opts.default
});
return answers.value;
};
exports.askList = askList;
const askUsernameAndPassword = async (msg, defaultUsername) => {
const answers = await _inquirer.default.prompt([{
type: 'input',
name: 'username',
message: `Enter ${msg} username: `,
default: defaultUsername,
validate: validateNonEmptyInput
}, {
type: 'password',
name: 'password',
mask: '*',
message: `Enter ${msg} password: `,
validate: validateNonEmptyInput
}]);
return answers;
};
exports.askUsernameAndPassword = askUsernameAndPassword;