localbackup
Version:
Utility to make local backups easily and without the hassle.
63 lines • 3.14 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 });
exports.localStorageProvider = exports.LocalStorageProvider = void 0;
const node_path_1 = __importDefault(require("node:path"));
const fs_extra_1 = __importDefault(require("fs-extra"));
class LocalStorageProvider {
isAvailable() {
return __awaiter(this, void 0, void 0, function* () {
return true;
});
}
uploadFile(localFilePath, destinationFilePath) {
return __awaiter(this, void 0, void 0, function* () {
fs_extra_1.default.ensureDirSync(node_path_1.default.dirname(destinationFilePath));
if (!(yield fs_extra_1.default.pathExists(localFilePath))) {
throw new Error(`Local file '${localFilePath}' does not exist.`);
}
if (!(yield fs_extra_1.default.stat(localFilePath)).isFile()) {
throw new Error(`Local file '${localFilePath}' is not a file.`);
}
yield fs_extra_1.default.copy(localFilePath, destinationFilePath);
});
}
deleteFile(fileFullPath) {
return __awaiter(this, void 0, void 0, function* () {
if (!(yield fs_extra_1.default.pathExists(fileFullPath))) {
return;
}
if (!(yield fs_extra_1.default.stat(fileFullPath)).isFile()) {
throw new Error(`'${fileFullPath}' is not a file.`);
}
yield fs_extra_1.default.remove(fileFullPath);
});
}
listFiles(directoryPath) {
return __awaiter(this, void 0, void 0, function* () {
if (!(yield fs_extra_1.default.pathExists(directoryPath))) {
throw new Error(`Directory '${directoryPath}' does not exist.`);
}
if (!(yield fs_extra_1.default.stat(directoryPath)).isDirectory()) {
throw new Error(`'${directoryPath}' is not a directory.`);
}
const files = yield fs_extra_1.default.readdir(directoryPath);
return files.map(file => node_path_1.default.normalize(`${directoryPath}/${file}`));
});
}
}
exports.LocalStorageProvider = LocalStorageProvider;
LocalStorageProvider.providerName = 'local';
exports.localStorageProvider = new LocalStorageProvider();
//# sourceMappingURL=localProvider.js.map