Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/backend/files/move-files.ts
Views: 687
import { getHome } from "./util";1import { move, pathExists } from "fs-extra";2import { stat } from "node:fs/promises";3import { move_file_variations } from "@cocalc/util/delete-files";4import { path_split } from "@cocalc/util/misc";5import { join } from "path";6import getLogger from "@cocalc/backend/logger";78const log = getLogger("move-files");910export async function move_files(11paths: string[],12dest: string, // assumed to be a directory13set_deleted: (path: string) => Promise<void>,14home?: string,15): Promise<void> {16const HOME = getHome(home);17log.debug({ paths, dest });18if (dest == "") {19dest = HOME;20} else if (!dest.startsWith("/")) {21dest = join(HOME, dest);22}23if (!dest.endsWith("/")) {24dest += "/";25}26const to_move: { src: string; dest: string }[] = [];27for (let path of paths) {28if (!path.startsWith("/")) {29path = join(HOME, path);30}31const target = dest + path_split(path).tail;32log.debug({ path, target });33await set_deleted(path);34to_move.push({ src: path, dest: target });3536// and the aux files:37try {38const s = await stat(path);39if (!s.isDirectory()) {40for (const variation of move_file_variations(path, target)) {41if (await pathExists(variation.src)) {42await set_deleted(variation.src);43to_move.push(variation);44}45}46}47} catch (_err) {}48}4950for (const x of to_move) {51await move(x.src, x.dest);52}53}545556