async function getIndex()

in lib/definitely-typed.ts [45:97]


async function getIndex(content: string, packageName: string): Promise<string> {
    let version = "x.x";
    let project = "https://github.com/baz/foo " +
        "(Does not have to be to GitHub, " +
        "but prefer linking to a source code repository rather than to a project website.)";
    try {
        const reg: Registry = JSON.parse(await loadString(`https://registry.npmjs.org/${packageName}`));
        const { latest } = reg["dist-tags"];
        const { homepage } = reg.versions[latest];

        version = latest.split(".").slice(0, 2).join("."); // Just major.minor
        if (homepage !== undefined) project = homepage;
    } catch (e) {
        console.warn(`Warning: Could not retrieve version/homepage information: ${e.message}`);
    }

    let authorName = 'My Self';
    try {
        const globalGitConfig = parseGitConfig.sync({ cwd: homedir(), path: '.gitconfig' });
        if (globalGitConfig.user && globalGitConfig.user.name) {
            authorName = globalGitConfig.user.name;
        }
    } catch (e) {
        console.warn(`Warning: Could not retrieve author name: ${e.message}`);
    }

    let authorUserName = 'me';
    try {
        const repoGitConfig = parseGitConfig.sync({ path: joinPaths('.git', 'config') });
        if (repoGitConfig['remote "origin"'] && repoGitConfig['remote "origin"'].url) {
            const url = parseUrl(repoGitConfig['remote "origin"'].url);
            if (url.hostname === 'github.com' && url.pathname) {
                authorUserName = url.pathname.split('/')[1] || authorUserName;
            } else if (url.pathname?.startsWith('git@github.com')) {
                authorUserName = url.pathname.split(':')?.[1].split('/')?.[0] || authorUserName;
            }
        }
    } catch (e) {
        console.warn(`Warning: Could not retrieve author's user name: ${e.message}`);
    }
    const authorUrl = formatUrl({
        protocol: 'https',
        hostname: 'github.com',
        pathname: authorUserName,
    });

    return `// Type definitions for ${packageName} ${version}
// Project: ${project}
// Definitions by: ${authorName} <${authorUrl}>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped

${content}`;
}