-
Notifications
You must be signed in to change notification settings - Fork 1
/
path.js
146 lines (135 loc) · 3.98 KB
/
path.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
var fs = require('fs');
var path = require('path');
/**
* get real required file path
*
* module to module.js or module/index.js
*
* @param filePath
* @returns {*}
*/
function realRequirPath(filePath) {
// console.log('realRequirPath:filePath: ' + filePath);
var jsFilePath = filePath + '.js';
if (fs.existsSync(filePath)) {
if (fs.statSync(filePath).isDirectory()) {
jsFilePath = filePath + '.js';
if (fs.existsSync(jsFilePath)) {
return jsFilePath;
} else {
jsFilePath = path.join(filePath, 'index.js');
}
// console.log('realRequirPath:jsFilePath: ' + jsFilePath);
} else if (fs.statSync(filePath).isFile()) {
return filePath;
} else {
return null;
}
} else {
jsFilePath = filePath + '.js';
// console.log('realRequirPath:filePath: ' + filePath);
}
if (fs.existsSync(jsFilePath)) {
// console.log('realRequirPath:filePath: ' + filePath);
return jsFilePath;
} else {
return null;
}
}
/**
* get the full path of subModule
*
* @param subModule subModule
* @returns {*} /User/XXXX/node_modules/module/relative.js
*/
function subModulePath(subModule, node_modules) {
var index = subModule.indexOf("/");
var length = subModule.length;
if (index <= 0 || index + 1 == length) {
return null;
}
var module = subModule.substring(0, index);
var relative = subModule.substring(index + 1, length);
var moduleDirectory = path.join(node_modules, module);
var filePath = path.join(moduleDirectory, relative);
return realRequirPath(filePath);
}
/**
* get required module full path
*
* @param module module
* @returns {*} /User/XXXX/node_modules/module/index.js
*/
function modulePath(module, node_modules) {
var pkgFile = path.join(node_modules, module, 'package.json');
if (!fs.existsSync(pkgFile)) {
return null;
}
var data = fs.readFileSync(pkgFile, 'utf8');
var pkg = JSON.parse(data);
var fileName = pkg.main || 'index.js';
var filePath = path.join(node_modules, module, fileName);
return realRequirPath(filePath);
}
/**
* get required file full path
*
* @param fromPath /User/XXXX/node_modules/module/index.js
* @param require ./require
* @returns {*} /User/XXXX/node_modules/module/require/index.js
*/
function requirePath(fromPath, require) {
// console.log('requirePath:fromPath: ' + fromPath);
// console.log('requirePath:require: ' + require);
if (fromPath === null || fromPath === undefined
|| require === null || require === undefined) {
return null;
}
var filePath = path.resolve(path.dirname(fromPath), require);
return realRequirPath(filePath);
}
/**
* full path in targetDirectory
* @param fromPath /User/XXXX/node_modules/module/index.js
* @param basePath /User/XXXX/node_modules/
* @param targetDirectory /User/XXXX/dist/npm/
* @returns {*} /User/XXXX/dist/npm/module/index.js
*/
function targetPath(fromPath, basePath, targetDirectory) {
// console.log('targetPath:fromPath: ' + fromPath);
// console.log('targetPath:basePath: ' + basePath);
// console.log('targetPath:targetDirectory: ' + targetDirectory);
var relativePath = path.relative(basePath, fromPath);
// console.log('targetPath:relativePath: ' + relativePath);
// console.log('targetPath: ' + path.resolve(targetDirectory, relativePath));
return path.resolve(targetDirectory, relativePath);
}
/**
* get relative path
*
* @param from
* @param to
* @returns {*}
*/
function relativePath(from, to) {
// console.log('relativePath:from: ' + from);
// console.log('relativePath:to: ' + to);
var relative = path.relative(from, to);
if (!relative || relative.length < 1) {
return relative;
}
var first = relative.substr(0, 1);
if (first !== '.' && first !== '/') {
relative = './' + relative;
}
// especially used for windows
relative = relative.split(path.sep).join('/');
return relative;
}
module.exports = {
modulePath: modulePath,
subModulePath: subModulePath,
requirePath: requirePath,
targetPath: targetPath,
relativePath: relativePath
};