async function generatePackageBuildFiles()

in internal/npm_install/index.js [218:291]


async function generatePackageBuildFiles(pkg) {
    let buildFilePath;
    if (pkg._files.includes('BUILD'))
        buildFilePath = 'BUILD';
    if (pkg._files.includes('BUILD.bazel'))
        buildFilePath = 'BUILD.bazel';
    const nodeModules = nodeModulesFolder();
    const nodeModulesPkgDir = `${nodeModules}/${pkg._dir}`;
    const isPkgDirASymlink = await fs_1.promises.lstat(nodeModulesPkgDir)
        .then(stat => stat.isSymbolicLink())
        .catch(constant(false));
    const symlinkBuildFile = isPkgDirASymlink && buildFilePath && !config.generate_local_modules_build_files;
    if (isPkgDirASymlink && !buildFilePath && !config.generate_local_modules_build_files) {
        console.log(`[yarn_install/npm_install]: package ${nodeModulesPkgDir} is local symlink and as such a BUILD file for it is expected but none was found. Please add one at ${await fs_1.promises.realpath(nodeModulesPkgDir)}`);
    }
    let buildFile = config.exports_directories_only ?
        printPackageExportsDirectories(pkg) :
        printPackageLegacy(pkg);
    if (buildFilePath) {
        buildFile = buildFile + '\n' +
            await fs_1.promises.readFile(path.join(nodeModules, pkg._dir, buildFilePath), 'utf-8');
    }
    else {
        buildFilePath = 'BUILD.bazel';
    }
    const visibility = !pkg._directDependency && config.strict_visibility ?
        config.limited_visibility :
        PUBLIC_VISIBILITY;
    if (!pkg._files.includes('bin/BUILD.bazel') && !pkg._files.includes('bin/BUILD')) {
        const binBuildFile = printPackageBin(pkg);
        if (binBuildFile.length) {
            await writeFile(path.posix.join(pkg._dir, 'bin', 'BUILD.bazel'), generateBuildFileHeader(visibility) + binBuildFile);
        }
    }
    const hasIndexBzl = pkg._files.includes('index.bzl');
    if (hasIndexBzl) {
        await pkg._files.filter(f => f !== 'BUILD' && f !== 'BUILD.bazel').reduce(async (prev, file) => {
            if (/^node_modules[/\\]/.test(file)) {
                return;
            }
            let destFile = path.posix.join(pkg._dir, file);
            const basename = path.basename(file);
            const basenameUc = basename.toUpperCase();
            if (basenameUc === '_BUILD' || basenameUc === '_BUILD.BAZEL') {
                destFile = path.posix.join(path.dirname(destFile), basename.substr(1));
            }
            const src = path.posix.join(nodeModules, pkg._dir, file);
            await prev;
            await mkdirp(path.dirname(destFile));
            await fs_1.promises.copyFile(src, destFile);
        }, Promise.resolve());
    }
    const indexFile = printIndexBzl(pkg);
    if (indexFile.length) {
        await writeFile(path.posix.join(pkg._dir, hasIndexBzl ? 'private' : '', 'index.bzl'), indexFile);
        const buildContent = `
# For integration testing
exports_files(["index.bzl"])
`;
        if (hasIndexBzl) {
            await writeFile(path.posix.join(pkg._dir, 'private', 'BUILD.bazel'), buildContent);
        }
        else {
            buildFile += buildContent;
        }
    }
    if (!symlinkBuildFile) {
        await writeFile(path.posix.join(pkg._dir, buildFilePath), generateBuildFileHeader(visibility) + buildFile);
    }
    else {
        const realPathBuildFileForPkg = await fs_1.promises.realpath(path.posix.join(nodeModulesPkgDir, buildFilePath));
        await createFileSymlink(realPathBuildFileForPkg, path.posix.join(pkg._dir, buildFilePath));
    }
}