-
Notifications
You must be signed in to change notification settings - Fork 2
/
path-utility.js
42 lines (31 loc) · 1.08 KB
/
path-utility.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
"use strict";
var find = require("find");
const path = require("path");
const resolve = require("path").resolve;
const fs = require("fs-extra");
module.exports = {
getPath: function(searchPaths, sourceDirectory, statement) {
function searchRecursively(searchDirectory, fileName) {
if(!fs.pathExistsSync(searchDirectory)) {
return undefined;
}
const files = find.fileSync(fileName, searchDirectory);
return files;
};
const importPath = statement.replace("import", "").replace(/"/g, "").replace(/'/g, "").replace(";", "").trim();
for(let i in searchPaths) {
const search = searchPaths[i];
const searchDirectory = path.join(sourceDirectory, search);
const file = path.join(sourceDirectory, search, importPath);
if(fs.pathExistsSync(file)) {
return resolve(file);
}
const fileName = path.basename(importPath);
const result = searchRecursively(searchDirectory, fileName);
if(result && result.length && result[0]) {
return resolve(result[0]);
}
}
return undefined;
}
};