-
-
Notifications
You must be signed in to change notification settings - Fork 176
/
Copy pathimport-target.js
163 lines (144 loc) · 4.67 KB
/
import-target.js
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/**
* @author Toru Nagashima
* @copyright 2016 Toru Nagashima. All rights reserved.
* See LICENSE file in root directory for full license.
*/
"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
var fs = require("fs");
var path = require("path");
var exists = require("./exists");
var getPackageJson = require("./get-package-json");
//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------
/**
* Checks whether or not a given path is a directory.
*
* @param {string} filePath - A file path to check.
* @returns {boolean} `true` if the path is a directory.
*/
function isDirectory(filePath) {
try {
return fs.statSync(filePath).isDirectory();
}
catch (err) {
return false;
}
}
/**
* Resolve a given path as a file with given extensions.
*
* @param {string} filePath - A path to resolve.
* @param {string[]} exts - Extensions that it checks whether or not the file exists.
* @returns {string|null} The resolved path. Or `null` if failed to resolve.
*/
function tryExtentions(filePath, exts) {
for (var i = 0; i < exts.length; ++i) {
var ext = exts[i];
if (exists(filePath + ext)) {
return filePath + ext;
}
}
return null;
}
/**
* Resolve a given path as a file.
*
* @param {string} filePath - A path to resolve.
* @param {string[]} exts - Extensions that it checks whether or not the file exists.
* @returns {string|null} The resolved path. Or `null` if failed to resolve.
*/
function resolveAsFile(filePath, exts) {
if (exists(filePath)) {
return filePath;
}
return tryExtentions(filePath, exts);
}
/**
* Resolve a given path as a directory.
*
* @param {string} filePath - A path to resolve.
* @param {string[]} exts - Extensions that it checks whether or not the file exists.
* @returns {string|null} The resolved path. Or `null` if failed to resolve.
*/
function resolveAsDirectory(filePath, exts) {
if (!isDirectory(filePath)) {
return null;
}
var p = getPackageJson(path.join(filePath, "package.json"));
if (p && path.dirname(p.filePath) === filePath && p.main) {
return resolveAsFile(path.join(filePath, p.main), exts);
}
return tryExtentions(path.join(filePath, "index"), exts);
}
/**
* Resolves the file.
*
* @param {string} basedir - The path of base directory to resolve relative path.
* @param {string} name - The name of an import target.
* @param {string[]} exts - Extensions that it checks whether or not the file exists.
* @returns {string} The resolved path.
*/
function resolve(basedir, name, exts) {
var resolvedPath = path.resolve(basedir, name);
return (
resolveAsFile(resolvedPath, exts) ||
resolveAsDirectory(resolvedPath, exts) ||
resolvedPath
);
}
/**
* Gets the module name of a given path.
*
* e.g. `eslint/lib/ast-utils` -> `eslint`
*
* @param {string} nameOrPath - A path to get.
* @returns {string} The module name of the path.
*/
function getModuleName(nameOrPath) {
var end = nameOrPath.indexOf("/");
if (end !== -1 && nameOrPath[0] === "@") {
end = nameOrPath.indexOf("/", 1 + end);
}
return end === -1 ? nameOrPath : nameOrPath.slice(0, end);
}
//------------------------------------------------------------------------------
// Public Interface
//------------------------------------------------------------------------------
/**
* Information of an import target.
*
* @constructor
* @param {ASTNode} node - The node of a `require()` or a module declaraiton.
* @param {string} name - The name of an import target.
* @param {string} basedir - The path of base directory to resolve relative path.
* @param {string[]} exts - Extensions that it checks whether or not the file exists.
*/
module.exports = function ImportTarget(node, name, basedir, exts) {
var relative = /^\./.test(name);
/**
* The node of a `require()` or a module declaraiton.
* @type {ASTNode}
*/
this.node = node;
/**
* The name of this import target.
* @type {string}
*/
this.name = name;
/**
* The full path of this import target.
* If the target is a module then this is `null`.
* @type {string|null}
*/
this.filePath = relative ? resolve(basedir, name, exts) : null;
/**
* The module name of this import target.
* If the target is a relative path then this is `null`.
* @type {string|null}
*/
this.moduleName = relative ? null : getModuleName(name);
};