copybase
Version:
Copy or backup databases quickly
83 lines (82 loc) • 3.63 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const chalk_1 = __importDefault(require("chalk"));
const child_process_1 = require("child_process");
class BaseProvider {
constructor(config) {
this.config = config;
}
logInfo(str) {
console.info(str);
}
logVerbose(str) {
if (this.config.verbose)
console.info(str);
}
execCommand(cmd, args, _options) {
return __awaiter(this, void 0, void 0, function* () {
const _a = _options || {}, { quiet } = _a, options = __rest(_a, ["quiet"]);
const cmdArgs = args.filter((o) => !!o);
if (!quiet) {
this.logVerbose(`exec "${chalk_1.default.yellow(`${cmd} ${cmdArgs.join(" ")}`)}"`);
}
const child = (0, child_process_1.spawn)(cmd, cmdArgs, Object.assign(Object.assign({}, options), { env: Object.assign(Object.assign({}, options === null || options === void 0 ? void 0 : options.env), process.env), shell: true }));
if (!quiet) {
child.stdout.on("data", (data) => process.stdout.write(data));
child.stderr.on("data", (data) => process.stdout.write(data));
child.on("error", (error) => process.stderr.write(error.message));
}
return new Promise((resolve, reject) => child.on("close", (code) => resolve(code)));
});
}
checkCommandExists(cmd, args) {
return __awaiter(this, void 0, void 0, function* () {
const exitCode = yield this.execCommand(cmd, args, { quiet: true });
if (exitCode !== 0) {
throw new Error(`Command ${cmd} not found`);
}
});
}
getCustomOptions(options) {
if (!options) {
return [];
}
const result = [];
Object.entries(options).map(([key, valueOrArray]) => {
if (key === "_raw_options") {
result.push(valueOrArray);
}
else if (Array.isArray(valueOrArray)) {
valueOrArray.forEach((value) => result.push(`--${key}=${value}`));
}
else {
result.push(`--${key}=${valueOrArray}`);
}
});
return result;
}
}
exports.default = BaseProvider;