@axway/axway-central-cli
Version:
Manage APIs, services and publish to the Amplify Marketplace
76 lines (71 loc) • 3.41 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.CliConfigManager = exports.CliConfigKeys = void 0;
var _fsExtra = require("fs-extra");
var _unset2 = _interopRequireDefault(require("lodash/unset"));
var _os = require("os");
var _path = require("path");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
let CliConfigKeys = exports.CliConfigKeys = /*#__PURE__*/function (CliConfigKeys) {
CliConfigKeys["APIC_DEPLOYMENT"] = "apic-deployment";
CliConfigKeys["BASE_URL"] = "base-url";
CliConfigKeys["ACCOUNT"] = "account";
CliConfigKeys["REGION"] = "region";
CliConfigKeys["PLATFORM"] = "platform";
CliConfigKeys["EXTENSIONS"] = "extensions";
CliConfigKeys["CLIENT_ID"] = "client-id";
return CliConfigKeys;
}({});
class CliConfigManager {
saveToFile(values) {
(0, _fsExtra.outputJsonSync)(CliConfigManager.configFilePath, values, {
spaces: '\t'
});
}
/**
* Temporary validator for config file content. Needed only to cleanup some values from config files for a couple of
* versions, remove it after some time.
*/
validateSavedConfigKeys() {
const deprecatedKeys = [
// TODO: a few other configs might be getting deprecated: https://jira.axway.com/browse/APIGOV-19737
// CliConfigKeys.PLATFORM,
CliConfigKeys.CLIENT_ID];
const keysToRemove = Object.keys(this.getAll()).filter(key => deprecatedKeys.includes(key));
if (keysToRemove.length) {
throw Error(`Following Axway Central CLI config keys has been deprecated and no longer needed: ${keysToRemove.join(', ')}
Please unset by running:
${keysToRemove.map(key => `axway central config unset ${key}`).join('\n')}
`);
}
}
// Note: current validation is good for "unset" but for "set" its needed to validate the value for "extensions" (should be non-empty)
validate(key) {
// validate 'extensions' keys - should alway have dot in the mid: extensions.abc
if (key.startsWith(`${CliConfigKeys.EXTENSIONS}`)) {
if (!key.includes('.')) throw Error(`invalid "${CliConfigKeys.EXTENSIONS}" key format.`);
} else if (!Object.values(CliConfigKeys).includes(key)) {
throw Error(`central configuration doesn't support the "${key}" key.`);
}
}
getAll() {
return (0, _fsExtra.existsSync)(CliConfigManager.configFilePath) ? (0, _fsExtra.readJsonSync)(CliConfigManager.configFilePath) : {};
}
get(key) {
return this.getAll()[key];
}
// TODO
// set(key: CentralConfigKeys) {}
unset(key) {
const config = this.getAll();
(0, _unset2.default)(config, key);
this.saveToFile(config);
}
}
exports.CliConfigManager = CliConfigManager;
_defineProperty(CliConfigManager, "configFilePath", (0, _path.join)((0, _os.homedir)(), '.axway', 'central.json'));