{"version":3,"file":"resolve-source-path.mjs","names":[],"sources":["../../../src/utils/helpers/resolve-source-path.ts"],"sourcesContent":["import fs from \"node:fs/promises\";\nimport { findPackageJSON } from \"node:module\";\nimport path from \"node:path\";\nimport { fileURLToPath, pathToFileURL } from \"node:url\";\nimport { LucidError } from \"../errors/index.js\";\n\n/**\n * Treats a missing source directory as optional project configuration,\n * while still allowing other filesystem/read errors to surface later.\n */\nexport const pathExists = async (targetPath: string) => {\n\ttry {\n\t\tawait fs.access(targetPath);\n\t\treturn true;\n\t} catch {\n\t\treturn false;\n\t}\n};\n\n/**\n * Resolves a package source from the project that owns the Lucid config. Node's\n * `import.meta.resolve` is relative to this core module, which can point at a\n * different `node_modules` tree in nested workspace installs.\n */\nconst resolveProjectPackagePath = async (\n\tsource: string,\n\tprojectRoot: string,\n) => {\n\tconst sourceParts = source.split(\"/\");\n\tconst packageNamePartCount = source.startsWith(\"@\") ? 2 : 1;\n\tif (sourceParts.length <= packageNamePartCount) return undefined;\n\n\tlet packageJsonPath: string | undefined;\n\ttry {\n\t\tpackageJsonPath = findPackageJSON(\n\t\t\tsource,\n\t\t\tpathToFileURL(path.join(projectRoot, \"package.json\")),\n\t\t);\n\t} catch {\n\t\treturn undefined;\n\t}\n\tif (!packageJsonPath) return undefined;\n\n\tconst sourcePath = path.join(\n\t\tpath.dirname(packageJsonPath),\n\t\t...sourceParts.slice(packageNamePartCount),\n\t);\n\treturn (await pathExists(sourcePath)) ? sourcePath : undefined;\n};\n\n/**\n * Converts source strings, package specifiers, and file URLs into absolute\n * filesystem paths. Project config may use relative strings; plugins can use\n * exported package subpaths such as `@scope/plugin/translations`.\n */\nexport const resolveSourcePath = async (\n\tsource: string | URL,\n\toptions?: {\n\t\tprojectRoot?: string;\n\t\t/**\n\t\t * Used in error messages to describe the source type. Eg. `Translation`, `Migration`.\n\t\t */\n\t\tlabel?: string;\n\t},\n) => {\n\tif (source instanceof URL) return fileURLToPath(source);\n\tif (path.isAbsolute(source)) return source;\n\n\tconst projectRoot = options?.projectRoot ?? process.cwd();\n\tconst projectPath = path.join(projectRoot, source);\n\tif (await pathExists(projectPath)) return projectPath;\n\tif (\n\t\tsource.startsWith(\".\") ||\n\t\t(!source.startsWith(\"@\") && !source.includes(\"/\"))\n\t) {\n\t\treturn projectPath;\n\t}\n\n\ttry {\n\t\tconst projectPackagePath = await resolveProjectPackagePath(\n\t\t\tsource,\n\t\t\tprojectRoot,\n\t\t);\n\t\tif (projectPackagePath) return projectPackagePath;\n\n\t\tconst resolved = import.meta.resolve(source);\n\t\tconst resolvedUrl = new URL(resolved);\n\n\t\tif (resolvedUrl.protocol !== \"file:\") {\n\t\t\tthrow new Error(`Expected a file URL but received \"${resolved}\".`);\n\t\t}\n\n\t\treturn fileURLToPath(resolvedUrl);\n\t} catch (error) {\n\t\tthrow new LucidError({\n\t\t\tmessage: `${options?.label ?? \"Source\"} package specifier \"${source}\" could not be resolved.`,\n\t\t\tdata: {\n\t\t\t\terror: error instanceof Error ? error.message : error,\n\t\t\t},\n\t\t});\n\t}\n};\n"],"mappings":"4MAUA,MAAa,EAAa,KAAO,IAAuB,CACvD,GAAI,CAEH,OADA,MAAM,EAAG,OAAO,CAAU,EACnB,EACR,MAAQ,CACP,MAAO,EACR,CACD,EAOM,EAA4B,MACjC,EACA,IACI,CACJ,IAAM,EAAc,EAAO,MAAM,GAAG,EAC9B,EAAuB,EAAO,WAAW,GAAG,EAAI,EAAI,EAC1D,GAAI,EAAY,QAAU,EAAsB,OAEhD,IAAI,EACJ,GAAI,CACH,EAAkB,EACjB,EACA,EAAc,EAAK,KAAK,EAAa,cAAc,CAAC,CACrD,CACD,MAAQ,CACP,MACD,CACA,GAAI,CAAC,EAAiB,OAEtB,IAAM,EAAa,EAAK,KACvB,EAAK,QAAQ,CAAe,EAC5B,GAAG,EAAY,MAAM,CAAoB,CAC1C,EACA,OAAQ,MAAM,EAAW,CAAU,EAAK,EAAa,IAAA,EACtD,EAOa,EAAoB,MAChC,EACA,IAOI,CAC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