Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/backend/misc/new-file.ts
1447 views
1
import { copyFile, writeFile } from "fs/promises";
2
import ensureContainingDirectoryExists from "@cocalc/backend/misc/ensure-containing-directory-exists";
3
import { exists } from "@cocalc/backend/misc/async-utils-node";
4
import { platform } from "os";
5
import { filename_extension } from "@cocalc/util/misc";
6
import { root } from "@cocalc/backend/data";
7
import { join } from "path";
8
9
export async function newFile(path: string) {
10
if (!process.env.HOME) {
11
throw Error("HOME must be set");
12
}
13
if (!path) {
14
return;
15
}
16
path = path.startsWith("/") ? path : join(process.env.HOME, path);
17
18
if (await exists(path)) {
19
return;
20
}
21
22
await ensureContainingDirectoryExists(path);
23
const ext = filename_extension(path);
24
const PLATFORM = platform().toLowerCase();
25
26
for (const place of [
27
process.env.HOME,
28
join(root, "smc_pyutil", "smc_pyutil"),
29
]) {
30
const template = join(place, "templates", PLATFORM, "default." + ext);
31
if (await exists(template)) {
32
await copyFile(template, path);
33
return;
34
}
35
}
36
await writeFile(path, "");
37
}
38
39