-
Notifications
You must be signed in to change notification settings - Fork 1
/
path-helper.ts
56 lines (50 loc) · 1.63 KB
/
path-helper.ts
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
import { mkdir, stat, readdir } from 'fs';
import { resolve, isAbsolute, join } from 'path';
import { promisify } from 'util';
/**
* Various helper functions for directory and file paths.
*/
export class PathHelper {
/**
* Get the absolute path to a file or directory, resolved. If path is not
* absolute, it's considered to be relative to the current working directory.
* The resulting path will not have a trailing slash.
* @param path The path to resolve.
*/
getAbsolutePath(path: string): string {
if (!isAbsolute(path))
path = join(process.cwd(), path);
return resolve(path);
}
/**
* Create a directory if it doesn't exist.
* @param dir To-be-created directory path.
*/
async mkdirIfNotExists(dir: string): Promise<void> {
const statP = promisify(stat);
const mkdirP = promisify(mkdir);
const absPath = this.getAbsolutePath(dir);
try {
await statP(absPath);
}
catch (err) {
await mkdirP(absPath, {recursive: true});
}
}
/**
* Get a list of files in a directory, matching a pattern and ordered by
* name.
* @param dir - The directory to list, which is resolved using
* getAbsolutePath.
* @param match - A regex pattern that files must match.
* @param order - The order direction: 1 ascending: -1 descending.
*/
ls(dir: string, match: RegExp = /.*/, order: number = 1): Promise<string[]> {
const readdirP = promisify(readdir);
const absDir = this.getAbsolutePath(dir);
return readdirP(absDir)
.then(files => files
.sort((l, r) => l.localeCompare(r) * order)
.filter(file => !!file.match(match)));
}
}