localbackup
Version:
Utility to make local backups easily and without the hassle.
62 lines • 3.19 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.createTempBackup = void 0;
const node_os_1 = __importDefault(require("node:os"));
const node_path_1 = __importDefault(require("node:path"));
const node_crypto_1 = __importDefault(require("node:crypto"));
const chalk_1 = __importDefault(require("chalk"));
const ora_1 = __importDefault(require("ora"));
const fs_extra_1 = __importDefault(require("fs-extra"));
const zip_a_folder_1 = require("zip-a-folder");
const isDir_1 = require("./helpers/isDir");
function createTempBackup(props) {
return __awaiter(this, void 0, void 0, function* () {
const spinner = (0, ora_1.default)('Creating temporal backup file...').start();
const tmpdir = node_path_1.default.join(node_os_1.default.tmpdir(), `localbackup-tmp-${node_crypto_1.default.randomUUID()}`);
fs_extra_1.default.ensureDirSync(tmpdir);
let source = props.source;
let isTemporal = false;
// Ensure the source is a directory
if (!(0, isDir_1.isDir)(props.source)) {
const filename = node_path_1.default.basename(props.source);
const tmpsource = node_path_1.default.join(tmpdir, 'source');
const tmppath = node_path_1.default.join(tmpsource, filename);
fs_extra_1.default.ensureDirSync(tmpsource);
fs_extra_1.default.copySync(props.source, tmppath);
source = tmpsource;
isTemporal = true;
}
// Create the target directory
const targetdir = node_path_1.default.join(tmpdir, 'target');
const targetfilepath = node_path_1.default.join(targetdir, props.destinationFileName);
fs_extra_1.default.ensureDirSync(targetdir);
// Create the backup
if (props.fileType === 'zip') {
yield (0, zip_a_folder_1.zip)(source, targetfilepath);
}
if (props.fileType === 'tar') {
yield (0, zip_a_folder_1.tar)(source, targetfilepath);
}
// Remove temporal directory if it was created
if (isTemporal) {
fs_extra_1.default.removeSync(source);
}
spinner.stop();
console.log(chalk_1.default.gray('✅ Temporal backup file created'));
return targetfilepath;
});
}
exports.createTempBackup = createTempBackup;
//# sourceMappingURL=createTempBackup.js.map