-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.js
More file actions
49 lines (41 loc) · 1.02 KB
/
utils.js
File metadata and controls
49 lines (41 loc) · 1.02 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
import * as os from "os";
import * as std from "std";
export function readFile(path, _encoding) {
let f, ret;
let errObj = {};
f = std.open(path, "r", errObj);
if (errObj.errno !== 0) {
console.error(`Error opening file: ${path}`);
}
ret = f.readAsString();
f.close();
return ret;
}
export function fileExists(filePath) {
try {
const stat = os.stat(filePath);
if (stat === null) {
return false;
}
return (stat[0].mode & os.S_IFMT) === os.S_IFREG;
} catch (err) {
if (err.errno === os.ENOENT) {
return false;
}
throw err;
}
}
export function addLeadingUnderscore(path) {
const parts = path.split("/");
const filename = parts[parts.length - 1];
parts[parts.length - 1] = "_" + parts[parts.length - 1];
return parts.join("/");
}
export function removeLeadingUnderscore(path) {
const parts = path.split("/");
const filename = parts[parts.length - 1];
if (filename.startsWith("_")) {
parts[parts.length - 1] = filename.slice(1);
}
return parts.join("/");
}