xrefcli
Version:
CLI command for the searching through OpenEdge XREF
120 lines • 4.96 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const xrefparser_1 = require("xrefparser");
const fs = __importStar(require("fs"));
const help_1 = require("../help");
class ExportCommand {
constructor(config) {
this.reponame = '';
this.outfile = '';
this.xreffiles = [];
this.includeEmpty = false;
this.config = config;
this.reponame = this.config.data.current;
this.searcher = new xrefparser_1.Searcher();
}
execute(params) {
this.xreffiles = this.config.loadRepo(this.reponame);
this.searcher.add(this.xreffiles);
const promise = new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
yield this.outputSourceTable();
resolve();
}));
return promise;
}
validate(params) {
const options = params['options'];
if (options['help'] === true) {
const help = new help_1.Help();
help.exportCommand();
process.exit(0);
}
const reponame = options['name'];
if (reponame === null) {
console.error('Error: a repo name should be provided');
return false;
}
if (reponame !== undefined) {
if (!this.config.repoExists(reponame)) {
console.error(`Error: repo '${reponame}' does not exist`);
return false;
}
this.reponame = reponame;
}
const outfile = options['outfile'];
if (outfile === null || outfile === undefined) {
console.error('Error: --outfile must be specified');
return false;
}
this.outfile = outfile;
if (options['includeempty'] === true) {
this.includeEmpty = true;
}
return true;
}
outputSourceTable() {
return __awaiter(this, void 0, void 0, function* () {
this.xreffiles.sort((a, b) => a.sourcefile.toLowerCase() < b.sourcefile.toLowerCase() ? -1 : 1);
const writeStream = fs.createWriteStream(this.outfile);
let tablenames = this.searcher.getTableNames().map(table => table.table);
tablenames = [...new Set(tablenames)];
tablenames.sort((a, b) => a.toLowerCase() < b.toLowerCase() ? -1 : 1);
const header = [' ', ...tablenames];
this.writeLine(writeStream, header);
for (let x = 0; x < this.xreffiles.length; x++) {
const currentXreffile = this.xreffiles[x];
const tables = new Array(tablenames.length + 1);
tables[0] = currentXreffile.sourcefile;
let count = 0;
for (let i = 0; i < tablenames.length; i++) {
const currentTablename = tablenames[i];
const pos = currentXreffile.tablenames.findIndex(tablename => tablename === currentTablename);
if (pos >= 0) {
tables[i + 1] = 'X';
count++;
}
else {
tables[i + 1] = ' ';
}
}
// skip sources without db access, if no -i parameters given
if (count > 0 || this.includeEmpty) {
yield this.writeLine(writeStream, tables);
}
}
writeStream.end();
});
}
writeLine(writeStream, line) {
return __awaiter(this, void 0, void 0, function* () {
let lineOut = '';
line.forEach(element => {
lineOut += `"${element}";`;
});
lineOut = lineOut.substring(0, lineOut.length - 1) + '\n';
const promise = new Promise((resolve, reject) => {
writeStream.write(lineOut, () => {
resolve();
});
});
yield promise;
});
}
}
exports.ExportCommand = ExportCommand;
//# sourceMappingURL=export.js.map