UNPKG

@codingtools/cdt

Version:
99 lines 3.84 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); // code here to write the tool for csv tool show const command_1 = require("@oclif/command"); const chalk_1 = tslib_1.__importDefault(require("chalk")); const logger_1 = tslib_1.__importDefault(require("../utilities/logger")); const utilities_1 = tslib_1.__importDefault(require("../utilities/utilities")); class View extends command_1.Command { // required FILE async run() { const { args, flags } = this.parse(View); args.file = this.getFilePath(flags, args); args.num = this.getFileLinesToShow(flags); // args.rowsToShow this.checkParameters(flags, args); this.showFile(args); } getFilePath(flags, args) { if (args.file) return args.file; if (flags.file) return flags.file9; logger_1.default.error(this, 'File path not passed'); } getFileLinesToShow(flags) { if (flags.num && flags.num > 0) // if value available and valid return flags.num; else return View.DEFAULT_COUNT; } // tslint:disable-next-line:no-unused checkParameters(flags, args) { if (args.file === undefined || args.file === '') logger_1.default.error(this, 'File path is empty'); // others already checked } showFile(args) { let data = utilities_1.default.getStringFromFile(this, args.file); let rows = data.split('\n'); let widthArray = []; let recordsToShow = parseInt(args.num, 10) + 1; for (let i = 0; i < rows[0].length; i++) { widthArray[i] = 0; } if (recordsToShow > rows.length) { recordsToShow = rows.length - 1; } for (let i = 0; i < recordsToShow; i++) { let row = rows[i].split(','); for (let j = 0; j < row.length; j++) { if (widthArray[j] < row[j].length) { widthArray[j] = row[j].length; } } } // -1 need to be done to exclude header // ${chalk.yellow('Avro Schema') logger_1.default.info(this, `Total ${chalk_1.default.greenBright(rows.length - 2)} records in file.`); logger_1.default.info(this, `showing top ${chalk_1.default.yellow(recordsToShow - 1)} records.`); this.printRow(rows[0].split(','), widthArray, '+', true); for (let i = 0; i < recordsToShow; i++) { let row = rows[i]; this.printRow(row.split(','), widthArray, '|', false); this.printRow(row.split(','), widthArray, '+', true); } logger_1.default.success(this, 'done.\n'); } printRow(row, widthArray, seperator, isLineSeparator) { let output = seperator; for (let i = 0; i < row.length; i++) { let totalSize = widthArray[i]; let dataLength = 0; let space = '-'; if (!isLineSeparator) { let data = row[i]; data = data.split(/\r/)[0]; output += data; dataLength = data.length; space = ' '; } for (let j = 0; j < totalSize - dataLength; j++) { output += space; } output += seperator; } this.log('' + output); } } exports.default = View; View.description = 'View file content and more'; View.DEFAULT_COUNT = 10; View.flags = { help: command_1.flags.help({ char: 'h' }), file: command_1.flags.string({ char: 'f', description: 'formatted file to be shown' }), num: command_1.flags.string({ char: 'n', description: `no. of rows to show, default:${View.DEFAULT_COUNT}` }) }; View.args = [{ name: 'file' }]; //# sourceMappingURL=view.js.map