@makano/rew 1.2.32 → 1.2.33

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,6 +3,7 @@ const path = require('path');
3
3
 
4
4
  module.exports.customRequire = function customRequire(modulePath, filePath) {
5
5
  const resolvedPath = resolveModulePath(modulePath, filePath);
6
+ if(!resolvedPath) throw new Error('Module '+modulePath+' not found');
6
7
  return require(resolvedPath);
7
8
  };
8
9
 
@@ -20,19 +21,40 @@ function resolveModulePath(modulePath, filePath) {
20
21
  if (fs.existsSync(fullPath + '.json')) {
21
22
  return fullPath + '.json';
22
23
  }
24
+
23
25
  if (fs.existsSync(fullPath) && fs.statSync(fullPath).isDirectory()) {
24
- const packageJsonPath = path.join(fullPath, 'package.json');
25
- if (fs.existsSync(packageJsonPath)) {
26
- const main = require(packageJsonPath).main || 'index.js';
27
- const mainPath = path.join(fullPath, main);
28
- if (fs.existsSync(mainPath)) {
29
- return mainPath;
30
- }
31
- }
32
- const indexPath = path.join(fullPath, 'index.js');
33
- if (fs.existsSync(indexPath)) {
34
- return indexPath;
35
- }
26
+ return searchInPath(fullPath);
27
+ }
28
+
29
+ const rootPath = modulePath.split('/').shift();
30
+ const halfFullPath = path.join(basePath, rootPath);
31
+ if (fs.existsSync(halfFullPath) && fs.statSync(halfFullPath).isDirectory()) {
32
+ return searchInPath(halfFullPath, ['.'].concat(fullPath.split('/').slice(1)).join('/'));
36
33
  }
37
34
  }
38
35
  }
36
+
37
+ function searchInPath(fullPath, exportses){
38
+ const packageJsonPath = path.join(fullPath, 'package.json');
39
+ if (fs.existsSync(packageJsonPath)) {
40
+ const packageJson = require(packageJsonPath);
41
+ let main = packageJson.main || 'index.js';
42
+ if(exportses){
43
+ if(packageJson.exports){
44
+ if(exportses in packageJson.exports) main = packageJson.exports[exportses];
45
+ }
46
+ }
47
+ if(typeof main == "object"){
48
+ if(Array.isArray(main)) main = main[0].require;
49
+ else main = main.require;
50
+ }
51
+ const mainPath = path.join(fullPath, main);
52
+ if (fs.existsSync(mainPath)) {
53
+ return mainPath;
54
+ }
55
+ }
56
+ const indexPath = path.join(fullPath, 'index.js');
57
+ if (fs.existsSync(indexPath)) {
58
+ return indexPath;
59
+ }
60
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@makano/rew",
3
- "version": "1.2.32",
3
+ "version": "1.2.33",
4
4
  "description": "A simple coffescript runtime and app manager",
5
5
  "main": "main.js",
6
6
  "directories": {