import url from 'node:url';
import { createRequire } from 'module';
/**
 * Collect significant dependencies and devDependencies from a relative path.
 * Also includes Node.js (under the "node" key).
 * @arg path The absolute or relative path to resolve from (a directory or
 *           file). Usually, `path.join(process.cwd(), 'src')`.
 */
export function newBuildInfoDependencies(path) {
    const require = createRequire(url.pathToFileURL(path));
    const deps = {
        // Strip the leading "v" from "v1.2.3".
        node: process.version.replace(/^v/, ''),
    };
    // A user app should always have @devvit/public-api as a direct dependency.
    // Absence is actionable.
    try {
        const protos = require('@devvit/protos/package.json');
        deps[protos.name] = protos.version;
    }
    catch {
        //
    }
    try {
        // eslint-disable-next-line @benasher44/implicit-dependencies/no-implicit
        const publicAPI = require('@devvit/public-api/package.json');
        deps[publicAPI.name] = publicAPI.version;
    }
    catch (err) {
        throw Error('missing @devvit/public-api NPM dependency', { cause: err });
    }
    return deps;
}
