file-manipulator
Version:
## Easy File operations library
110 lines (109 loc) • 4.84 kB
JavaScript
;
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 utils = {
checkExist(fullPath) {
return __awaiter(this, void 0, void 0, function* () {
try {
return new Promise((ok) => {
fs_extra_1.default.exists(fullPath, ok);
});
}
catch (e) {
return false;
}
});
},
remove(fullPath) {
return __awaiter(this, void 0, void 0, function* () {
try {
return new Promise((ok) => {
fs_extra_1.default.remove(fullPath, (e) => {
ok(!e);
});
});
}
catch (e) {
return false;
}
});
},
readFile(fullPath, encoding = 'utf8') {
return __awaiter(this, void 0, void 0, function* () {
return yield fs_extra_1.default.readFile(fullPath, { encoding });
});
},
rename(oldFullPath, newFullPath, overwrite) {
return __awaiter(this, void 0, void 0, function* () {
try {
const isSame = oldFullPath === newFullPath;
if (isSame)
return true;
const isExistBefore = yield utils.checkExist(oldFullPath);
if (!isExistBefore)
return false;
const isConflictSameName = yield utils.checkExist(newFullPath);
if (!isConflictSameName) {
yield fs_extra_1.default.rename(oldFullPath, newFullPath);
return true;
}
if (isConflictSameName && overwrite) {
yield utils.remove(newFullPath);
yield fs_extra_1.default.rename(oldFullPath, newFullPath);
return true;
}
if (isConflictSameName && !overwrite)
return false;
yield fs_extra_1.default.rename(oldFullPath, newFullPath);
return true;
}
catch (e) {
return false;
}
});
},
restoreTestFolder() {
return __awaiter(this, void 0, void 0, function* () {
//remove eache folders inside test folder
const isExist1 = yield utils.checkExist(`./test_folder/f1`);
const isExist2 = yield utils.checkExist(`./test_folder/f2`);
const isExist3 = yield utils.checkExist(`./test_folder/f3`);
if (isExist1)
yield utils.remove(`./test_folder/f1`);
if (isExist2)
yield utils.remove(`./test_folder/f2`);
if (isExist3)
yield utils.remove(`./test_folder/f3`);
//rewrite each file in each folder (or create)
fs_extra_1.default.mkdirpSync(`./test_folder/f1`);
fs_extra_1.default.mkdirpSync(`./test_folder/f2`);
fs_extra_1.default.writeFileSync(`./test_folder/f1/f1.txt`, 'test file 1', 'utf8');
fs_extra_1.default.writeFileSync(`./test_folder/f2/f2.txt`, 'test file 2', 'utf8');
console.log('🍎 test folder restored 🍎');
});
},
getNameByConfig(config) {
const isAlreadyExt = !!config.name && !!(config === null || config === void 0 ? void 0 : config.ext) && config.name.endsWith(config.ext);
if (isAlreadyExt) {
return config.name || '';
}
return `${config.name}${!!(config === null || config === void 0 ? void 0 : config.ext) ? ('.' + config.ext) : ('')}`;
},
getNameRenameConfig(config) {
const newName = !!(config === null || config === void 0 ? void 0 : config.renameTo) ? config === null || config === void 0 ? void 0 : config.renameTo : config.name;
return this.getNameByConfig({ name: newName, ext: config.ext });
}
};
exports.default = utils;