This repository has been archived by the owner on Jan 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
LocationContext.ts
115 lines (104 loc) · 2.72 KB
/
LocationContext.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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
import {
constants,
accessSync,
access as _access,
readFileSync,
readFile as _readFile,
} from "fs";
import { promisify } from "util";
import { join, normalize } from "path";
import { INPUT_PATHS_NOT_FOUND } from "../constants/errors";
/**
* @public
* location context to search and read file content
*/
export class LocationContext {
/**
* mirror path.join()
*
* @param args paths to join
* @returns
*/
join(...args: string[]): string {
return join(...args);
}
/**
* check whether input args is existing
*
* @param args paths to join
* @returns true if path file/directory is exist
*/
async isExist(...args: string[]): Promise<boolean> {
const access = promisify(_access);
return access(this.join(...args), constants.F_OK | constants.R_OK)
.then(() => true)
.catch(() => false);
}
/**
* check whether input args is existing
*
* @param args array path that will join to single path
* @returns true if path file/directory is exist
*/
isExistSync(...args: string[]): boolean {
try {
accessSync(this.join(...args), constants.F_OK | constants.R_OK);
return true;
} catch (e) {
return false;
}
}
/**
* find which input paths is existed
*
* @param paths full-path
* @returns first existing path
* @throws input not found if none of input paths exist
*/
async findExist(...paths: string[]): Promise<string> {
for await (const p of paths) {
const path = normalize(p);
if (await this.isExist(path)) {
return path;
}
}
throw new Error(INPUT_PATHS_NOT_FOUND);
}
/**
* find which input paths is existed
*
* @param paths full-path
* @returns first existing path
* @throws input not found if none of input paths exist
*/
findExistSync(...paths: string[]): string {
for (const p of paths) {
const path = normalize(p);
if (this.isExistSync(path)) {
return path;
}
}
throw new Error(INPUT_PATHS_NOT_FOUND);
}
/**
* read first existing input file content
* @param paths list of full-path
* @returns first file content
* @throws input not found if none of input paths exist
*/
async readFirst(...paths: string[]): Promise<string> {
const readFile = promisify(_readFile);
const validPath = await this.findExist(...paths);
return readFile(validPath, "utf-8");
}
/**
* read first existing input file content
* @param paths list of full-path
* @returns first file content
* @throws input not found if none of input paths exist
*/
async readFirstSync(...paths: string[]): Promise<string> {
const validPath = this.findExistSync(...paths);
return readFileSync(validPath, "utf-8");
}
}