gx
chenyc
2025-06-12 7b72ac13a83764a662159d4a49b7fffb90476ecb
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
"use strict";
 
const Path = require("path");
const Fs = require("fs");
 
const createRequireFromPath = require("./create-require");
 
const cache = new Map();
 
function requireAt(dir, request) {
  const makeIt = (xdir, checked) => {
    let xRequire = requireAt.cache && requireAt.cache.get(xdir);
 
    if (!xRequire) {
      let stat;
      try {
        stat = Fs.statSync(xdir);
      } catch (e) {
        throw new Error(`require-at: stat '${xdir}' failed: ${e.message}`);
      }
 
      if (!stat || !stat.isDirectory()) {
        if (checked) throw new Error(`require-at: not a directory: '${dir}'`);
        return makeIt(Path.dirname(xdir), true);
      }
 
      xRequire = createRequireFromPath(Path.join(xdir, "._require-at_"), xdir);
 
      requireAt.cache && requireAt.cache.set(xdir, xRequire);
    }
 
    return request ? xRequire(request) : xRequire;
  };
 
  return makeIt(Path.resolve(dir), false);
}
 
requireAt.cache = cache;
 
module.exports = requireAt;