@cocalc/project
Version:
CoCalc: project daemon
85 lines • 3 kB
JavaScript
;
/*
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
* License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.rename_file = exports.move_files = void 0;
const fs_extra_1 = require("fs-extra");
const fs_1 = require("fs");
const misc_1 = require("@cocalc/util/misc");
const listings_1 = require("../sync/listings");
const awaiting_1 = require("awaiting");
const delete_files_1 = require("@cocalc/util/delete-files");
function home() {
const { HOME } = process.env;
if (HOME == null)
throw Error("HOME must be defined");
return HOME;
}
async function move_files(paths, dest, // assumed to be a directory
logger) {
const HOME = home();
logger.debug(`move_files ${JSON.stringify(paths)} --> ${dest}`);
if (dest == "") {
dest = HOME;
}
else {
dest = HOME + "/" + dest;
}
dest += "/";
const to_move = [];
const listings = (0, listings_1.get_listings_table)();
for (let path of paths) {
path = HOME + "/" + path;
const target = dest + (0, misc_1.path_split)(path).tail;
logger.debug(`move_file ${path} --> ${target}`);
await listings?.set_deleted(path);
to_move.push({ src: path, dest: target });
// and the aux files:
try {
const s = await (0, awaiting_1.callback)(fs_1.stat, path);
if (!s.isDirectory()) {
for (const variation of (0, delete_files_1.move_file_variations)(path, target)) {
if (await (0, fs_extra_1.pathExists)(variation.src)) {
await listings?.set_deleted(variation.src);
to_move.push(variation);
}
}
}
}
catch (_err) { }
}
for (const x of to_move) {
await (0, fs_extra_1.move)(x.src, x.dest);
}
}
exports.move_files = move_files;
async function rename_file(src, dest, logger) {
if (src == dest)
return; // no-op
const HOME = home();
src = HOME + "/" + src;
dest = HOME + "/" + dest;
logger.debug(`rename_file ${src} --> ${dest}`);
const listings = (0, listings_1.get_listings_table)();
await listings?.set_deleted(src); // todo: later may have a set_moved...
const to_move = [{ src, dest }];
try {
const s = await (0, awaiting_1.callback)(fs_1.stat, src);
if (!s.isDirectory()) {
for (const variation of (0, delete_files_1.move_file_variations)(src, dest)) {
if (await (0, fs_extra_1.pathExists)(variation.src)) {
await listings?.set_deleted(variation.src);
to_move.push(variation);
}
}
}
}
catch (_err) { }
for (const x of to_move) {
await (0, fs_extra_1.move)(x.src, x.dest);
}
}
exports.rename_file = rename_file;
//# sourceMappingURL=move-files.js.map