File size: 780 Bytes
e371833
 
 
 
 
f6678ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e371833
 
 
 
 
 
f6678ab
e371833
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import { join, dirname } from "path";
import { fileURLToPath } from "url";

const __dirname = dirname(fileURLToPath(import.meta.url));

const DEFAULT_DATA_DIR = join(__dirname, "..", "data");

let _dataDir: string | undefined;

/** Override DATA_DIR for testing. Pass undefined to reset to default. */
export function setDataDir(dir: string | undefined) {
  _dataDir = dir;
}

export function getDataDir(): string {
  return _dataDir ?? process.env.DATA_DIR ?? DEFAULT_DATA_DIR;
}

/** @deprecated Use getDataDir() instead */
export const DATA_DIR = DEFAULT_DATA_DIR;

export function sanitizeName(name: string): string {
  return name.replace(/[^a-zA-Z0-9_-]/g, "_");
}

export function docPath(name: string): string {
  return join(getDataDir(), `${sanitizeName(name)}.yjs`);
}