Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
82 lines (76 loc) · 3.01 KB
/
Copy pathindex.js
File metadata and controls
82 lines (76 loc) · 3.01 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
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
import { readFileSync } from "node:fs";
import { fileURLToPath, pathToFileURL } from "node:url";
import { createRequire } from "node:module";
import caller from "caller";
/**
* Reads a file and returns its contents, resolving the path
* relative to the calling module.
*
* @param {string} filePath - Absolute path, relative path
* (`./` or `../`), or module path
* @param {{ encoding?: BufferEncoding | null, flag?: string }}
* [options] - Options passed to `fs.readFileSync`; defaults
* to UTF-8 encoding
* @returns {string | Buffer} File contents as a UTF-8 string
* by default, or a Buffer if `encoding: null` is passed
*/
export function file(filePath, options = {}) {
const callerUrl = toCallerUrl(caller());
const resolved = resolveUrl(filePath, callerUrl);
const readOptions = { encoding: "utf-8", ...options };
return readFileSync(resolved, readOptions);
}
export default file;
/**
* Resolves a file path to an absolute path, relative to the
* calling module.
*
* @param {string} pathString - Absolute path, relative path
* (`./` or `../`), or module path (resolved via
* `require.resolve`)
* @param {string} [callerPath] - Override the caller's
* filesystem path; defaults to the auto-detected caller
* @returns {string} Absolute filesystem path, or bare module
* name for Node.js built-ins (e.g. `"path"`, `"fs"`)
*/
export function resolve(pathString, callerPath) {
const ref = callerPath !== undefined ? callerPath : caller();
const callerUrl = toCallerUrl(ref);
const url = resolveUrl(pathString, callerUrl);
return url.protocol === "node:" ? url.pathname : fileURLToPath(url);
}
/**
* Converts a file:// URL string or filesystem path to a URL object.
* @param {string} path - A file:// URL string or filesystem path
* @returns {URL}
*/
function toCallerUrl(path) {
return path.startsWith("file://") ? new URL(path) : pathToFileURL(path);
}
/**
* Resolves a file path against a caller URL, always returning a URL.
* Node.js built-in modules are returned as `node:<name>` URLs.
* @param {string} filePath - Absolute path, relative path, or module path
* @param {URL} callerUrl - file:// URL of the calling module
* @returns {URL}
*/
function resolveUrl(filePath, callerUrl) {
const isAbsolute = filePath.startsWith("/");
const isRelative = !isAbsolute && (
filePath.startsWith("./") || filePath.startsWith("../")
);
const isModule = !isAbsolute && !isRelative;
// Three cases:
// 1. absolute path (starts with /) => convert to file:// URL
// 2. relative path (starts with ./ or ../) => resolve against caller URL
// 3. module path => use require.resolve() for node_modules lookup
if (isAbsolute) {
return pathToFileURL(filePath);
} else if (isRelative) {
return new URL(filePath, callerUrl);
} else if (isModule) {
const resolved = createRequire(callerUrl).resolve(filePath);
// built-in modules resolve to their bare name (e.g. "path"), not a file path
return resolved.startsWith("/") ? pathToFileURL(resolved) : new URL(`node:${resolved}`);
}
}