-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
118 lines (105 loc) · 2.89 KB
/
index.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
116
117
118
import path from "path";
import fs from "fs";
import fsPromises from "fs/promises";
async function file_exists(filePath: string) {
try {
return (await fsPromises.stat(filePath)).isFile();
} catch (err) {
return false;
}
}
function file_exists_sync(filePath: string) {
try {
return fs.statSync(filePath).isFile();
} catch (err) {
return false;
}
}
async function search_for_file_upwards(start: string, filename: string) {
let current_dir = start;
let file_path = path.resolve(current_dir, filename);
while (!(await file_exists(file_path))) {
if (current_dir == "/") {
throw new Error(
"Could not find '" +
filename +
"' in any of the parent directories."
);
}
current_dir = path.resolve(current_dir, "../");
file_path = path.resolve(current_dir, filename);
}
return file_path;
}
function search_for_file_upwards_sync(start: string, filename: string) {
let current_dir = start;
let file_path = path.resolve(current_dir, filename);
while (!file_exists_sync(file_path)) {
if (current_dir == "/") {
throw new Error(
"Could not find '" +
filename +
"' in any of the parent directories."
);
}
current_dir = path.resolve(current_dir, "../");
file_path = path.resolve(current_dir, filename);
}
return file_path;
}
async function locreq_resolve(caller_path: string, module_path: string) {
const package_path = await search_for_file_upwards(
caller_path,
"package.json"
);
return path.resolve(package_path, "../", module_path);
}
function locreq_resolve_sync(caller_path: string, module_path: string) {
const package_path = search_for_file_upwards_sync(
caller_path,
"package.json"
);
return path.resolve(package_path, "../", module_path);
}
async function locreq(
caller_path: string,
module_path: string
): Promise<unknown> {
return require(await locreq_resolve(caller_path, module_path));
}
function locreq_sync(caller_path: string, module_path: string): unknown {
return require(locreq_resolve_sync(caller_path, module_path));
}
function curry_locreq(caller_path: string) {
let curried: {
(module_path: string): Promise<unknown>;
resolve: (module_path: string) => Promise<string>;
} = (() => {
const f = async function (module_path: string) {
return await locreq(caller_path, module_path);
};
f.resolve = async function (module_path: string) {
return await locreq_resolve(caller_path, module_path);
};
return f;
})();
return curried;
}
function curry_locreq_sync(caller_path: string) {
let curried: {
(module_path: string): unknown;
resolve: (module_path: string) => string;
} = (() => {
const f = function (module_path: string) {
return locreq_sync(caller_path, module_path);
};
f.resolve = function (module_path: string) {
return locreq_resolve_sync(caller_path, module_path);
};
return f;
})();
return curried;
}
//curry_locreq -> Asynchronous
//curry_locreq_sync -> Synchronous
export default curry_locreq;