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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
import fs from "node:fs"
import path from "node:path"
import { BitArray, Ok, Error as GError, toList} from "./gleam.mjs";
export function readBits(filepath) {
try {
const contents = fs.readFileSync(path.normalize(filepath))
return new Ok(new BitArray(new Uint8Array(contents)))
} catch(e) {
return new GError(stringifyError(e))
}
}
export function writeBits(contents, filepath) {
try {
fs.writeFileSync(path.normalize(filepath), contents.buffer)
return new Ok(undefined)
} catch (e) {
return new GError(stringifyError(e))
}
}
export function appendBits(contents, filepath) {
try {
fs.appendFileSync(path.normalize(filepath), contents.buffer)
return new Ok(undefined)
} catch (e) {
return new GError(stringifyError(e))
}
}
function stringifyError(e) {
return e.code
}
export function isFile(filepath) {
let fp = path.normalize(filepath)
return fs.existsSync(fp) && fs.lstatSync(fp).isFile();
}
export function isDirectory(filepath) {
let fp = path.normalize(filepath)
return fs.existsSync(fp) && fs.lstatSync(fp).isDirectory();
}
export function makeDirectory(filepath) {
try {
fs.mkdirSync(path.normalize(filepath))
return new Ok(undefined)
} catch (e) {
return new GError(stringifyError(e))
}
}
export function createDirAll(filepath) {
try {
fs.mkdirSync(filepath, { recursive: true })
return new Ok(undefined)
} catch (e) {
return new GError(stringifyError(e))
}
}
export function deleteFileOrDirRecursive(fileOrDirPath) {
try {
if (isDirectory(fileOrDirPath)) {
fs.rmSync(path.normalize(fileOrDirPath), { recursive: true })
} else {
fs.unlinkSync(path.normalize(fileOrDirPath))
}
return new Ok(undefined)
} catch (e) {
return new GError(stringifyError(e))
}
}
export function listContents(filepath) {
try {
const stuff = toList(fs.readdirSync(path.normalize(filepath)))
return new Ok(stuff)
} catch(e) {
return new GError(stringifyError(e))
}
}
export function copyFile(srcpath, destpath) {
try {
fs.copyFileSync(path.normalize(srcpath), path.normalize(destpath))
return new Ok(undefined)
} catch (e) {
return new GError(stringifyError(e))
}
}
export function renameFile(srcpath, destpath) {
try {
fs.renameSync(path.normalize(srcpath), path.normalize(destpath))
return new Ok(undefined)
} catch (e) {
return new GError(stringifyError(e))
}
}
|