CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/project/browser-websocket/canonical-path.ts
Views: 687
1
/*
2
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
/* Return a normalized version of the path, which is always
7
relative to the user's home directory.
8
9
If the path is not in the user's home directory, we use
10
the symlink form ~/.smc/root to / to make it appear to
11
be in the home directory.
12
*/
13
14
import { realpath } from "fs/promises";
15
import { resolve } from "path";
16
17
export async function canonical_paths(paths: string[]): Promise<string[]> {
18
const v: string[] = [];
19
20
const { HOME: HOME_ENV } = process.env;
21
if (HOME_ENV == null) {
22
throw Error("HOME environment variable must be defined");
23
}
24
25
// realpath is necessary, because in some circumstances the home dir is made up of a symlink
26
const HOME = await realpath(HOME_ENV);
27
28
for (let path of paths) {
29
path = await realpath(resolve(path));
30
if (path.startsWith(HOME)) {
31
v.push(path.slice(HOME.length + 1));
32
} else {
33
v.push(HOME + "/.smc/root" + path);
34
}
35
}
36
37
return v;
38
}
39
40