UNPKG

file-manipulator

Version:

## Easy File operations library

110 lines (109 loc) 5.78 kB
"use strict"; 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 fs_extra_1 = __importDefault(require("fs-extra")); const path_1 = __importDefault(require("path")); const utils_1 = __importDefault(require("../utils")); class Updater { /** * update file method actions * 1) check exist * 2) rename if need * 3) read and edit inside content * 4) rewrite */ file(config) { return __awaiter(this, void 0, void 0, function* () { try { //file names let startFullName = utils_1.default.getNameByConfig(config); const endFullName = utils_1.default.getNameRenameConfig(config); //file pathes let startFullPath = path_1.default.resolve(config.path, startFullName); const endFullPath = path_1.default.resolve(config.path, endFullName); //1) chek exist const isExist = yield utils_1.default.checkExist(startFullPath); if (!isExist) return false; //2) renamed block if (!!(config === null || config === void 0 ? void 0 : config.renameTo)) { const isRenamed = yield utils_1.default.rename(startFullPath, endFullPath, !!(config === null || config === void 0 ? void 0 : config.replaceExisting)); if (isRenamed) { startFullPath = endFullPath; } else { return false; } } //3) read and edit inside content const startContent = yield utils_1.default.readFile(startFullPath, 'utf8'); let editedContent = startContent; const isReplace = (config === null || config === void 0 ? void 0 : config.stringFrom) && (config === null || config === void 0 ? void 0 : config.stringTo); if (isReplace) { editedContent = editedContent.replace(config.stringFrom || '', config.stringTo || ''); } const isRewrite = !(config === null || config === void 0 ? void 0 : config.stringFrom) && !(config === null || config === void 0 ? void 0 : config.lineParams) && (config === null || config === void 0 ? void 0 : config.stringTo); if (isRewrite) { editedContent = config.stringTo || ''; } const isEditLines = (config === null || config === void 0 ? void 0 : config.lineParams) && (config === null || config === void 0 ? void 0 : config.stringTo) !== undefined; if (isEditLines) { const { line = 0, len = 0, space = 0 } = (config === null || config === void 0 ? void 0 : config.lineParams) || {}; const arrRows = editedContent.split('\n'); if (arrRows[line]) { const targetRowCharts = arrRows[line].split(''); targetRowCharts.splice(space, len, config.stringTo || ''); const editedTargerRowString = targetRowCharts.join(''); arrRows[line] = editedTargerRowString; const resultFileContent = arrRows.join('\n'); editedContent = resultFileContent; } } //4) rewrite yield fs_extra_1.default.writeFile(startFullPath, editedContent, { encoding: 'utf8' }); return true; } catch (e) { console.log(this.constructor.name, `[${this.file.name}]`, config.name, e); return false; } }); } /** * update folder method actions * 1) check exist * 2) rename if need */ folder(config) { return __awaiter(this, void 0, void 0, function* () { try { //file names const startFullName = `${config.name}`; const endFullName = (config === null || config === void 0 ? void 0 : config.renameTo) ? `${config === null || config === void 0 ? void 0 : config.renameTo}` : startFullName; //file pathes const startFullPath = path_1.default.resolve(config.path, startFullName); const endFullPath = path_1.default.resolve(config.path, endFullName); const isExist = yield utils_1.default.checkExist(startFullPath); if (!isExist) return false; return utils_1.default.rename(startFullPath, endFullPath, !!(config === null || config === void 0 ? void 0 : config.replaceExisting)); } catch (e) { console.log(this.constructor.name, `[${this.folder.name}]`, config.name, e); return false; } }); } } exports.default = new Updater();