@graphql-hive/cli
Version:
A CLI util to manage and control your GraphQL Hive
126 lines • 4.78 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const colors_1 = tslib_1.__importDefault(require("colors"));
const graphql_request_1 = require("graphql-request");
const log_symbols_1 = tslib_1.__importDefault(require("log-symbols"));
const core_1 = require("@oclif/core");
const config_1 = require("./helpers/config");
const sdk_1 = require("./sdk");
class default_1 extends core_1.Command {
constructor(argv, config) {
super(argv, config);
this._userConfig = new config_1.Config({
// eslint-disable-next-line no-process-env
filepath: process.env.HIVE_CONFIG,
rootDir: process.cwd(),
});
}
success(...args) {
this.log(colors_1.default.green(log_symbols_1.default.success), ...args);
}
fail(...args) {
this.log(colors_1.default.red(log_symbols_1.default.error), ...args);
}
info(...args) {
this.log(colors_1.default.yellow(log_symbols_1.default.info), ...args);
}
bolderize(msg) {
const findSingleQuotes = /'([^']+)'/gim;
const findDoubleQuotes = /"([^"]+)"/gim;
return msg
.replace(findSingleQuotes, (_, value) => colors_1.default.bold(value))
.replace(findDoubleQuotes, (_, value) => colors_1.default.bold(value));
}
/**
* Get a value from arguments or flags first, then from env variables,
* then fallback to config.
* Throw when there's no value.
*
* @param key
* @param args all arguments or flags
* @param defaultValue default value
* @param message custom error message in case of no value
* @param env an env var name
*/
ensure({ key, args, defaultValue, message, env, }) {
if (args[key]) {
return args[key];
}
// eslint-disable-next-line no-process-env
if (env && process.env[env]) {
// eslint-disable-next-line no-process-env
return process.env[env];
}
if (this._userConfig.has(key)) {
return this._userConfig.get(key);
}
if (defaultValue) {
return defaultValue;
}
if (message) {
throw new core_1.Errors.CLIError(message);
}
throw new core_1.Errors.CLIError(`Missing "${String(key)}"`);
}
/**
* Get a value from arguments or flags first, then fallback to config.
* Do NOT throw when there's no value.
*
* @param key
* @param args all arguments or flags
*/
maybe(key, args) {
if (args[key]) {
return args[key];
}
if (this._userConfig.has(key)) {
return this._userConfig.get(key);
}
}
cleanRequestId(requestId) {
return requestId ? requestId.split(',')[0].trim() : undefined;
}
registryApi(registry, token) {
return (0, sdk_1.getSdk)(new graphql_request_1.GraphQLClient(registry, {
headers: {
Accept: 'application/json',
'User-Agent': `hive-cli/${this.config.version}`,
Authorization: `Bearer ${token}`,
'graphql-client-name': 'Hive CLI',
'graphql-client-version': this.config.version,
},
}));
}
handleFetchError(error) {
var _a, _b, _c, _d, _e;
if (typeof error === 'string') {
return this.error(error);
}
if (error instanceof Error) {
if (isClientError(error)) {
const errors = (_a = error.response) === null || _a === void 0 ? void 0 : _a.errors;
if (Array.isArray(errors) && errors.length > 0) {
return this.error(errors[0].message, {
ref: this.cleanRequestId((_c = (_b = error.response) === null || _b === void 0 ? void 0 : _b.headers) === null || _c === void 0 ? void 0 : _c.get('x-request-id')),
});
}
return this.error(error.message, {
ref: this.cleanRequestId((_e = (_d = error.response) === null || _d === void 0 ? void 0 : _d.headers) === null || _e === void 0 ? void 0 : _e.get('x-request-id')),
});
}
return this.error(error);
}
return this.error(JSON.stringify(error));
}
async require(flags) {
if (flags.require && flags.require.length > 0) {
await Promise.all(flags.require.map(mod => Promise.resolve(`${require.resolve(mod, { paths: [process.cwd()] })}`).then(s => tslib_1.__importStar(require(s)))));
}
}
}
exports.default = default_1;
function isClientError(error) {
return 'response' in error;
}
//# sourceMappingURL=base-command.js.map