UNPKG

xrefcli

Version:

CLI command for the searching through OpenEdge XREF

210 lines 8.34 kB
"use strict"; 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 help_1 = require("../help"); const readline = __importStar(require("readline")); class MatrixCommand { constructor(config) { this.inputFromSources = true; this.inputFromTables = false; this.outputJson = false; this.excludes = []; this.config = config; this.xreffiles = this.config.loadRepo(this.config.data.current); } execute(params) { const promise = new Promise((resolve) => __awaiter(this, void 0, void 0, function* () { let inputArray = yield this.readStdin(); inputArray = this.extractExcludes(inputArray); if (this.inputFromSources) { this.iterateSources(inputArray); } else if (this.inputFromTables) { this.iterateTables(inputArray); } resolve(); })); return promise; } extractExcludes(inputArray) { const result = []; inputArray.forEach(line => { if (line.startsWith('-') /*|| line.startsWith('+')*/) { this.excludes.push(line.substring(1)); } else { result.push(line); } }); return result; } iterateSources(sourcesArray) { const matrix = {}; const allTables = []; // sort sources first sourcesArray = sourcesArray.sort((a, b) => a.toLowerCase() < b.toLowerCase() ? -1 : 1); for (const source of sourcesArray) { const tablesOfSource = {}; const xreffile = this.xreffiles.filter(item => item.sourcefile === source)[0]; let sourcename = ''; if (xreffile !== undefined) { for (const table of xreffile.tables) { const crdString = (table.isCreated ? 'C' : ' ') + 'R' + (table.isUpdated ? 'U' : ' ') + (table.isDeleted ? 'D' : ' '); tablesOfSource[table.database + '.' + table.name] = crdString; allTables.push(table.database + '.' + table.name); } sourcename = source; } else { sourcename = source + ' <NOT FOUND>'; } matrix[sourcename] = tablesOfSource; } let tablenames = [...new Set(allTables)]; if (this.excludes.length > 0) { tablenames = tablenames.filter(table => { let includeSource = true; this.excludes.forEach(exclude => { if (table.startsWith(exclude)) { includeSource = false; } }); return includeSource; }); } tablenames.sort((a, b) => a.toLowerCase() < b.toLowerCase() ? -1 : 1); this.outputMatrix(matrix, tablenames); } iterateTables(tablesArray) { const matrix = {}; let allsources = []; let sourcesArray = []; const allTables = []; tablesArray.forEach(tablename => { const files = this.xreffiles.filter(xreffile => { const tables = xreffile.tables.filter(table => { return (table.database + '.' + table.name).toLowerCase() === tablename.toLowerCase() || table.name.toLowerCase() === tablename.toLowerCase(); }); return tables.length > 0; }); allsources = allsources.concat(files.map(xfile => xfile.sourcefile)); }); sourcesArray = [...new Set(allsources)]; for (const source of sourcesArray) { const tablesOfSource = {}; const xreffile = this.xreffiles.filter(item => item.sourcefile === source)[0]; // let sourcename = ''; if (xreffile !== undefined) { xreffile.tables.forEach(table => { if (tablesArray.indexOf((table.database + '.' + table.name).toLowerCase()) >= 0 || tablesArray.indexOf((table.name).toLowerCase()) >= 0) { const crdString = (table.isCreated ? 'C' : ' ') + 'R' + (table.isUpdated ? 'U' : ' ') + (table.isDeleted ? 'D' : ' '); tablesOfSource[table.database + '.' + table.name] = crdString; allTables.push(table.database + '.' + table.name); } }); } matrix[source] = tablesOfSource; } const tablenames = [...new Set(allTables)]; tablenames.sort((a, b) => a.toLowerCase() < b.toLowerCase() ? -1 : 1); // remove excluded sources Object.keys(matrix).forEach(key => { this.excludes.forEach(exclude => { if (key.startsWith(exclude)) { delete matrix[key]; } }); }); this.outputMatrix(matrix, tablenames); } outputMatrix(matrix, tablenames) { if (this.outputJson) { console.log(JSON.stringify(matrix, null, 2)); return; } else { // output table names first let line = ''; tablenames.forEach(tablename => { line += ';' + tablename; }); console.log(line); const sourcenames = Object.keys(matrix); sourcenames.forEach(source => { line = source; tablenames.forEach(tablename => { const crud = matrix[source][tablename]; line += ';' + ((crud !== undefined) ? crud : ''); }); console.log(line); }); } } readStdin() { return __awaiter(this, void 0, void 0, function* () { const inputArray = []; const promise = new Promise((resolve) => __awaiter(this, void 0, void 0, function* () { const rl = readline.createInterface({ input: process.stdin }); rl.on('line', (input) => { if (input !== '') { inputArray.push(input); } else { rl.close(); resolve(inputArray); } }); })); return promise; }); } validate(params) { const options = params['options']; if (options['help'] === true) { const help = new help_1.Help(); help.matrixCommand(); process.exit(0); } if (options['tables'] === true && options['sources'] === true) { console.error('--tables and --sources options cannot be combined'); return false; } if (options['sources'] === true) { this.inputFromSources = true; } if (options['tables'] === true) { this.inputFromTables = true; this.inputFromSources = false; // reset default } if (options['json'] === true) { this.outputJson = true; } return true; } } exports.MatrixCommand = MatrixCommand; //# sourceMappingURL=matrix.js.map