import normalizePath from 'normalize-path'; import { readPackageUp } from 'read-pkg-up'; import upath from 'upath'; import { keysOfObject } from './object.js'; import { isString, isUndefined } from './unit.js'; export async function resolveCorePkgName( root: string, ): Promise { const pkg = await readPackageUp({ cwd: root }); return pkg?.packageJson.name; } export const resolvePath = (...pathSegments: string[]): string => normalizePath(upath.resolve(...pathSegments)); export function resolveRelativePath(from: string, to: string): string { const path = upath.relative(upath.dirname(from), to); return path.startsWith('.') ? path : `./${path}`; } export async function resolveConfigPaths>( cwd: string, config: T, match: (key: keyof T) => boolean = key => isString(key) && (key.endsWith('File') || key.endsWith('Dir')), ): Promise { const configWithResolvedPaths: T = { ...config }; const rcwd = cwd.startsWith('.') ? resolvePath(process.cwd(), cwd) : cwd; if (Object.keys(config).includes('cwd')) { (configWithResolvedPaths as unknown as { cwd: string }).cwd = rcwd; } keysOfObject(config).forEach(key => { if (!isUndefined(config[key]) && match(key)) { configWithResolvedPaths[key] = resolvePath( rcwd, config[key] as string, ) as T[keyof T]; } }); return configWithResolvedPaths; }