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/util/jupyter-api/chdir-commands.ts
Views: 687
1
/*
2
Code to change the working directory to a given directory in
3
several different languages.
4
*/
5
6
export default function createChdirCommand(lang: string, path: string): string {
7
// throws exception if don't know how.
8
lang = lang.toLowerCase(); // xeus-cling is 'C++-17'
9
if (lang == "sparql") {
10
// there is no notion of current directory for sparql.
11
return "";
12
}
13
if (lang.startsWith("python") || lang.startsWith("sage")) {
14
return createPythonChangeDirectoryCode(path);
15
} else if (lang == "r") {
16
return createRChangeDirectoryCode(path);
17
} else if (lang == "julia") {
18
return createJuliaChangeDirectoryCode(path);
19
} else if (lang == "octave" || lang == "matlab") {
20
return createOctaveChangeDirectoryCode(path);
21
} else if (lang == "javascript") {
22
return createNodeChangeDirectoryCode(path);
23
} else if (lang == "bash" || lang == "sh") {
24
return createBashChangeDirectoryCommand(path);
25
} else if (lang == "prolog") {
26
return createPrologChangeDirectoryCode(path);
27
} else if (lang == "c" || lang.startsWith("c++")) {
28
return createCppChangeDirectoryCode(path);
29
} else {
30
// e.g., "gap" -- I got stumped on that.
31
throw Error(
32
`unable to change directory: chdir for language ${lang} is not implemented.`
33
);
34
}
35
}
36
37
// mostly written by ChatGPT4
38
function createPythonChangeDirectoryCode(path: string): string {
39
const escapedPath = path.replace(/\\/g, "\\\\").replace(/'/g, "\\'");
40
return `import os; os.chdir('${escapedPath}')`;
41
}
42
43
function createRChangeDirectoryCode(path) {
44
const escapedPath = path.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
45
return `setwd("${escapedPath}")`;
46
}
47
48
function createJuliaChangeDirectoryCode(path) {
49
const escapedPath = path.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
50
return `cd("${escapedPath}")`;
51
}
52
53
function createOctaveChangeDirectoryCode(path) {
54
const escapedPath = path.replace(/\\/g, "\\\\").replace(/'/g, "\\'");
55
return `cd('${escapedPath}')`;
56
}
57
58
function createNodeChangeDirectoryCode(path) {
59
const escapedPath = path.replace(/\\/g, "\\\\").replace(/`/g, "\\`");
60
return `process.chdir(\`${escapedPath}\`)`;
61
}
62
63
export function escapeBashChangeDirPath(path: string): string {
64
return path.replace(/(["'$`\\])/g, "\\$1");
65
}
66
67
function createBashChangeDirectoryCommand(path) {
68
const escapedPath = escapeBashChangeDirPath(path);
69
return `cd '${escapedPath}'`;
70
}
71
72
/*
73
function createGAPChangeDirectoryCode(path) {
74
const escapedPath = path.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
75
// SetCurrentDirectory doesn't exist. ChatGPT made it up from C#.
76
// I studied gap docs and googled for quite a while and was totally
77
// stumped! Make this just isn't possible in gap...
78
return `SetCurrentDirectory(Directory("${escapedPath}"));;`;
79
}
80
*/
81
82
function createPrologChangeDirectoryCode(path) {
83
const escapedPath = path.replace(/\\/g, "\\\\").replace(/'/g, "\\'");
84
return `working_directory(_, '${escapedPath}').`;
85
}
86
87
function createCppChangeDirectoryCode(path) {
88
const escapedPath = path.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
89
return `#include <unistd.h>\nchdir("${escapedPath}")\n`;
90
}
91
92