-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.js
38 lines (34 loc) · 1.03 KB
/
util.js
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
import {access, readFile} from 'fs/promises';
const DIRECTORY_REGEX = /<img src="\/icons\/[^"]+" alt="\[([^\]]+)\]">(?:<\/td><td>)?\s*<a href="([^"]+)">[^<]+<\/a>(?:<\/td><td align="right">)?\s*(\d+-(?:\w{3}|\d+)-\d+ \d+:\d+)/g;
export const ALL_DIRECTORIES = ['os1', 'os2'];
export function parseDirectoryListing(content) {
const listing = {
directories: {},
files: {}
};
let match;
do {
match = DIRECTORY_REGEX.exec(content);
if (match) {
if (match[1] === 'DIR') {
listing.directories[match[2].slice(0, -1)] = new Date(match[3]);
} else {
listing.files[decodeURIComponent(match[2])] = new Date(match[3]);
}
}
} while (match);
return listing;
}
export async function readJSON(path) {
return JSON.parse(await readFile(path, {
encoding : 'utf-8'
}));
}
export async function fileExists(filename) {
try {
await access(filename);
return true;
} catch {
return false;
}
}