UNPKG

@codingtools/cdt

Version:
75 lines 3.65 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const chalk_1 = tslib_1.__importDefault(require("chalk")); const fs_1 = tslib_1.__importDefault(require("fs")); const logger_1 = tslib_1.__importDefault(require("./logger")); // tslint:disable-next-line:no-unnecessary-class class Utilities { static getStringFromFile(thisRef, filePath) { let fileStr = ''; if (!fs_1.default.existsSync(filePath)) { logger_1.default.error(thisRef, `Could not find file: ${chalk_1.default.red(filePath)}`); // this will output error and exit command } else { let fileBuffer = fs_1.default.readFileSync(filePath); fileStr = fileBuffer.toString(); // by default utf8 } return fileStr; } static getJsonObjectFromFile(thisRef, filePath) { if (!fs_1.default.existsSync(filePath)) { logger_1.default.error(thisRef, `Could not find file: ${chalk_1.default.red(filePath)}`); // this will output error and exit command } else { let jsonString = fs_1.default.readFileSync(filePath, 'utf8'); try { return JSON.parse(jsonString); } catch (err) { logger_1.default.error(this, 'Error parsing JSON string:' + err); } } } static getInputString(thisRef, flags, args) { // if -s or -f is not passed we will take it from args if (flags.string) //if -s given return flags.string; else if (flags.file) { // if -f given logger_1.default.info(thisRef, `reading file: ${chalk_1.default.green(flags.file)}`); return Utilities.getStringFromFile(thisRef, flags.file); } else return args.string; } // tslint:disable-next-line:no-unused static getInputStringFromCmd(thisRef, flags, args) { // if -s is not passed we will take it from args if (flags.string) //if -s given return flags.string; else return args.string; } static writeStringToFile(thisRef, filePath, string) { if (!fs_1.default.existsSync(filePath)) logger_1.default.info(thisRef, `Could not find file: ${chalk_1.default.yellow(filePath + ', creating new one')}`); // this will output error and exit command else logger_1.default.warn(thisRef, `File already exists: ${chalk_1.default.green(filePath)}, ${chalk_1.default.yellow('overriding content')}`); // this will output error and exit command fs_1.default.writeFileSync(filePath, string.toString()); if (string !== '') // this condition comes for truncating logger_1.default.success(thisRef, `output written to file: ${chalk_1.default.green(filePath)}`); // this will output error and exit command // return `${chalk.red(pkg)} ${message}` } static appendStringToFile(thisRef, filePath, string) { if (!fs_1.default.existsSync(filePath)) logger_1.default.info(thisRef, `Could not find file: ${chalk_1.default.yellow(filePath + ', creating new one')}`); // this will output error and exit command fs_1.default.appendFileSync(filePath, string); } static truncateFile(thisRef, filePath) { // if (fs.existsSync(filePath)) // Logger.info(thisRef, `file found: ${chalk.yellow(filePath + ', truncating')}`) // this will output error and exit command Utilities.writeStringToFile(thisRef, filePath, ''); // write nothing } } exports.default = Utilities; //# sourceMappingURL=utilities.js.map