UNPKG

@cocalc/project

Version:
37 lines (36 loc) 1.32 kB
"use strict"; /* * 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.canonical_paths = void 0; /* Return a normalized version of the path, which is always relative to the user's home directory. If the path is not in the user's home directory, we use the symlink form ~/.smc/root to / to make it appear to be in the home directory. */ const promises_1 = require("fs/promises"); const path_1 = require("path"); async function canonical_paths(paths) { const v = []; const { HOME: HOME_ENV } = process.env; if (HOME_ENV == null) { throw Error("HOME environment variable must be defined"); } // realpath is necessary, because in some circumstances the home dir is made up of a symlink const HOME = await (0, promises_1.realpath)(HOME_ENV); for (let path of paths) { path = await (0, promises_1.realpath)((0, path_1.resolve)(path)); if (path.startsWith(HOME)) { v.push(path.slice(HOME.length + 1)); } else { v.push(HOME + "/.smc/root" + path); } } return v; } exports.canonical_paths = canonical_paths; //# sourceMappingURL=canonical-path.js.map