Debian Codegraph @

ncjsm/resolve/index.js

raw
1 // Async module resolver
2
3 "use strict";
4
5 const getResolver = require("../lib/get-node-resolver")
6 , { resolve } = require("path")
7 , stat = require("fs2/stat")
8 , readFile = require("fs2/read-file")
9 , realpath = require("fs2/realpath");
10
11 const { parse } = JSON;
12
13 module.exports = getResolver(
14 targetPath => {
15 targetPath = resolve(targetPath);
16 return stat(targetPath)
17 .then(stats => {
18 if (!stats.isFile()) return null;
19 return realpath(targetPath).then(realPath => ({ targetPath, realPath }));
20 })
21 .catch(e => {
22 if (e.code === "ENOENT") return null;
23 throw e;
24 });
25 },
26 path =>
27 readFile(resolve(path, "package.json"))(
28 data => {
29 try { return parse(data).main; }
30 catch (e) { return null; }
31 },
32 e => {
33 if (e.code === "ENOENT") return null;
34 throw e;
35 }
36 )
37 );