UNPKG

@markandrus/effect-derive

Version:

Derive Covariant (Functor), Foldable, and Traversable instances, as well as base functors, for algebraic data types (ADTs)

30 lines (29 loc) 1.07 kB
import * as fs from 'node:fs'; import * as path from 'node:path'; const suffixes = ['.cjs', '.mjs', '.js', '.cts', '.mts', '.ts']; export function resolveRelativePath(cwd, inFilePath, outFilePath, importPath) { let absPath; for (let pathToTry of [outFilePath, inFilePath, cwd]) { pathToTry = path.resolve(path.join(pathToTry, importPath)); if (fs.existsSync(pathToTry)) { absPath = pathToTry; break; } for (const suffix of suffixes) { if (pathToTry.endsWith(suffix)) { pathToTry = pathToTry.slice(0, pathToTry.length - suffix.length); } } for (const suffix of suffixes) { const pathToTryWithSuffix = pathToTry + suffix; if (fs.existsSync(pathToTryWithSuffix)) { absPath = pathToTry; break; } } } if (absPath == null) { throw new Error(`Could not resolve import path: ${importPath}`); } return './' + path.relative(path.dirname(outFilePath), absPath); }