-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathutil.mts
More file actions
54 lines (49 loc) · 1.16 KB
/
util.mts
File metadata and controls
54 lines (49 loc) · 1.16 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import fs from "fs";
export interface FileStream extends AsyncDisposable {
suspend: boolean;
readonly path: string;
write(s: string): Promise<void>;
writeLine(s?: string): Promise<void>;
}
export async function openFileStream(path: string): Promise<FileStream> {
const fileStream = await fs.promises.open(path, "w");
let _suspended = false;
const write = async (s: string) => {
if (_suspended) {
return;
}
await fileStream.write(s);
};
const writeLine = (s?: string) => {
if (s) {
return write(s + "\n");
} else {
return write("\n");
}
};
const asyncDispose = async () => {
await fileStream.close();
};
return {
path,
write,
writeLine,
[Symbol.asyncDispose]: asyncDispose,
get suspend() {
return _suspended
},
set suspend(v: boolean) {
_suspended = v;
}
};
}
export function toPascalCase(v: string | string[]) {
if (typeof v === "string") {
var parts = v.split(".");
for (let i = 0; i < parts.length; i++) {
parts[i] = parts[i].slice(0, 1).toUpperCase() + parts[i].slice(1);
}
return parts.join(".");
}
return v.map(toPascalCase);
}