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/http-api/write-text-file.ts
Views: 687
1
/*
2
Write a text file.
3
4
EXAMPLE:
5
6
curl -u `cat .smc/secret_token`: -d path=a.txt -d content="bar" http://localhost:`cat .smc/api-server.port`/api/v1/write-text-file
7
*/
8
9
import { writeFile } from "node:fs/promises";
10
11
import { client } from "./server";
12
13
export default async function writeTextFile({ path, content }): Promise<void> {
14
if (client == null) throw Error("client must be defined");
15
16
const dbg = client.dbg("write-text-file");
17
dbg(`path="${path}"`);
18
if (typeof path != "string") {
19
throw Error(`provide the path as a string -- got path="${path}"`);
20
}
21
if (typeof content != "string") {
22
throw Error(
23
`provide the content as a string -- got content of type ${typeof content}`
24
);
25
}
26
27
return await writeFile(path, content);
28
}
29
30