MCPcopy Create free account
hub / github.com/R-js/libRmath.js / walkDirPath

Function walkDirPath

scripts/helpers.mjs:78–136  ·  view source on GitHub ↗
(subPaths, basePath, possibleExtensions)

Source from the content-addressed store, hash-verified

76}
77
78function walkDirPath(subPaths, basePath, possibleExtensions) {
79 // if subPaths is empty on entry it is possible directory import
80 if (subPaths.length === 0) {
81 subPaths.push('index');
82 }
83 const current = subPaths.shift();
84 const entries = readdirSync(basePath, { withFileTypes: true });
85 const possibles = entries.filter(entry => {
86 // possible scenarios
87 // last subpath?
88 if (subPaths.length === 0){
89 if (entry.isDirectory() && current === entry.name) { // possible directory import
90 return true;
91 }
92 if (!entry.isFile()) { // no pipes, file sockets, etc
93 return false
94 }
95 // it is a regular file
96 const ext = extname(entry.name);
97 const fileSansExt = entry.name.replace(ext,'');
98 if (fileSansExt === current){
99 return true;
100 }
101 return false;
102 }
103 // must be a directory and exact match
104 if (current === entry.name && entry.isDirectory()){
105 return true;
106 }
107 return false;
108 });
109 if (subPaths.length > 0){
110 if (possibles.length){
111 return walkDirPath(structuredClone(subPaths), join(basePath, possibles[0].name), possibleExtensions);
112 }
113 return
114 }
115 if (possibles.length === 0) {
116 return;
117 }
118 // handle directory imports
119 const dirImports = possibles.filter(possible => possible.isDirectory());
120 const fileImports = possibles.filter(possible => possible.isFile());
121 // handle file imports first
122 for (const ext of possibleExtensions){
123 const found = fileImports.find(possible => extname(possible.name) === ext);
124 if (found){
125 return join(basePath, found.name);
126 }
127 }
128 // handle directory imports
129 for (const dirImport of dirImports){
130 const found = walkDirPath(['index'], join(basePath, dirImport.name), possibleExtensions);
131 if (found) {
132 return found;
133 }
134 }
135 return;

Callers 1

resolveAliasPathsFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected