zcatalyst-cli
Version:
Command Line Tool for CATALYST
86 lines (85 loc) • 4.51 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 __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = require("path");
const endpoints_1 = require("../../endpoints");
const error_1 = __importDefault(require("../../error"));
const command_1 = __importDefault(require("../../internal/command"));
const runtime_store_1 = __importDefault(require("../../runtime-store"));
const constants_1 = require("../../util_modules/constants");
const env_1 = require("../../util_modules/env");
const logger_1 = require("../../util_modules/logger");
const option_1 = require("../../util_modules/option");
const shell_1 = require("../../util_modules/shell");
const fs_1 = require("../../util_modules/fs");
const getReadConfigFromPath = (pth) => __awaiter(void 0, void 0, void 0, function* () {
const configJSON = yield fs_1.ASYNC.readJSONFile((0, path_1.resolve)(pth), {
checkpath: true,
throws: false
});
if (!configJSON || Object.keys(configJSON).length === 0) {
(0, logger_1.debug)('No parsable export config given');
return {};
}
return configJSON;
});
exports.default = new command_1.default('ds:export [table]')
.description('Bulk read records from a table in the Catalyst Data Store')
.option('--table <name|id>', 'Name or ID of the table from which data is to be read (eg. "UserDetails")')
.option('--config <path>', 'Path of the configuration json file to be used for export.')
.option('--page <page>', 'Number which denotes the range of rows to be fetched (eg. "1")')
.option('--production', 'flag for pointing to production environment')
.needs('auth', [constants_1.SCOPE.projects, constants_1.SCOPE.datastore, constants_1.SCOPE.row])
.needs('config', { optional: true })
.needs('rc', { optional: true })
.action((optTable) => __awaiter(void 0, void 0, void 0, function* () {
const env = (0, option_1.getOptionValue)('production', false) ? 'Production' : 'Development';
const configPath = (0, option_1.getOptionValue)('config', false);
const exportConfig = configPath ? yield getReadConfigFromPath(configPath) : {};
const tableOptId = (0, option_1.getOptionValue)('table', exportConfig.table_identifier + '');
const tableId = optTable ? optTable : tableOptId;
if (!tableId) {
throw new error_1.default('table identifier missing', { exit: 0, errorId: 'EXP-1' });
}
const page = (0, option_1.getOptionValue)('page', undefined);
if (page !== undefined) {
if (!exportConfig.query) {
exportConfig.query = {};
}
exportConfig.query.page = page;
}
const bulkAPI = yield (0, endpoints_1.bulkDSAPI)({ env });
const readResponse = (yield bulkAPI.read(tableId, exportConfig));
(0, logger_1.info)();
(0, logger_1.success)(`Successfully scheduled export job for table "${tableId}" with jobid "${readResponse.job_id}"`);
if ((0, env_1.isPrimaryShell)()) {
const exitListeners = process.listeners('exit');
process.removeAllListeners('exit');
const allGlobalOpts = runtime_store_1.default.get('opts.globalOpts', {});
const optsArr = Object.keys(allGlobalOpts).reduce((arr, key) => {
arr.push('--' + key);
if (allGlobalOpts[key]) {
arr.push(allGlobalOpts[key]);
}
return arr;
}, (env === 'Production' ? ['--production'] : []));
(0, shell_1.spawn)('catalyst', ['ds:status', 'export', readResponse.job_id + '', ...optsArr], {
shell: true,
stdio: 'inherit'
}).SYNC();
(exitListeners || []).forEach((listner) => {
process.addListener('exit', listner);
});
}
}));