Path: blob/master/src/packages/backend/misc/new-file.ts
1447 views
import { copyFile, writeFile } from "fs/promises";1import ensureContainingDirectoryExists from "@cocalc/backend/misc/ensure-containing-directory-exists";2import { exists } from "@cocalc/backend/misc/async-utils-node";3import { platform } from "os";4import { filename_extension } from "@cocalc/util/misc";5import { root } from "@cocalc/backend/data";6import { join } from "path";78export async function newFile(path: string) {9if (!process.env.HOME) {10throw Error("HOME must be set");11}12if (!path) {13return;14}15path = path.startsWith("/") ? path : join(process.env.HOME, path);1617if (await exists(path)) {18return;19}2021await ensureContainingDirectoryExists(path);22const ext = filename_extension(path);23const PLATFORM = platform().toLowerCase();2425for (const place of [26process.env.HOME,27join(root, "smc_pyutil", "smc_pyutil"),28]) {29const template = join(place, "templates", PLATFORM, "default." + ext);30if (await exists(template)) {31await copyFile(template, path);32return;33}34}35await writeFile(path, "");36}373839