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

Function resolveToFullPath

scripts/helpers.mjs:179–275  ·  view source on GitHub ↗
(module, importStatement, base, pathsDictionary, exact, wildcard, forceExt, possibleExtensions)

Source from the content-addressed store, hash-verified

177}
178
179export function resolveToFullPath(module, importStatement, base, pathsDictionary, exact, wildcard, forceExt, possibleExtensions) {
180 const node_m = 'node_modules';
181 const isAliased = isAliasedPath(importStatement, exact, wildcard);
182 const includesNodeModule = importStatement.includes(node_m);
183 if (!isRelativeImport(importStatement) && !isAliased) {
184 // dont change, this is a npm module import
185 return importStatement;
186 }
187 // relative import or aliased
188 if (importStatement.includes(node_m)) {
189 const pos = importStatement.lastIndexOf(node_m);
190 return importStatement.slice(pos + node_m.length + 1);
191 }
192 let aliasRelativePath;
193 if (isAliased) {
194 if (importStatement === '@rng/knuth-taocp-2002'){
195 const n = 1;
196 }
197 const resolved = resolveAliasPaths(importStatement, pathsDictionary, base, exact, wildcard, possibleExtensions)
198 if (resolved === importStatement) { // could not be resolved (assume it is node modules or dev forgot to alias in tsconfig.json)
199 return resolved;
200 }
201 if (!resolved) { // because of error (some path element replacement does not exist or no read access)
202 return importStatement;
203 }
204 if (typeof resolved !== 'string') {
205 return;
206 }
207 aliasRelativePath = relative(dirname(module), resolved);
208 if (aliasRelativePath[0] !== '.'){
209 aliasRelativePath = './' + aliasRelativePath;
210 }
211 }
212 const finalImportStatement = aliasRelativePath || importStatement;
213 // possible physical location of a file
214 const candidate = resolve(dirname(module), finalImportStatement);
215 // is it a dir ?
216
217 let lstat = {
218 isDirectory() {
219 return false;
220 }
221 };
222 try {
223 lstat = lstatSync(candidate);
224 } catch (err) {
225 // nothing
226 }
227
228 if (lstat.isDirectory()) {
229 // try index import
230 const dirEntries = readdirSync(candidate);
231 let indexTSFileExists = '';
232 let packageJSONExists = '';
233 let indexFileExists = '';
234 for (const dirEntry of dirEntries) {
235 if (dirEntry === 'index.ts') {
236 indexTSFileExists = dirEntry;

Callers 1

createCompilerFunction · 0.90

Calls 3

isAliasedPathFunction · 0.85
isRelativeImportFunction · 0.85
resolveAliasPathsFunction · 0.85

Tested by

no test coverage detected