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/backend/misc/touch.ts
Views: 687
1
/*
2
* This file is part of CoCalc: Copyright © 2023 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
import { open, utimes } from "node:fs/promises";
7
8
// touch the file in a similar manner as "touch" in linux
9
export async function touch(
10
path: string,
11
createIfMissing = true
12
): Promise<boolean> {
13
try {
14
const now = new Date();
15
await utimes(path, now, now);
16
return true;
17
} catch (err) {
18
try {
19
if (createIfMissing && "ENOENT" === err.code) {
20
const fd = await open(path, "a");
21
await fd.close();
22
return true;
23
}
24
} finally {
25
return false;
26
}
27
}
28
}
29
30