function flattenPkgDependencies(pkg, dep, pkgsMap) {
        if (pkg._dependencies.indexOf(dep) !== -1) {
            // circular dependency
            return;
        }
        pkg._dependencies.push(dep);
        const findDeps = function (targetDeps, required, depType) {
            Object.keys(targetDeps || {})
                .map(targetDep => {
                // look for matching nested package
                const dirSegments = dep._dir.split('/');
                while (dirSegments.length) {
                    const maybe = path.posix.join(...dirSegments, 'node_modules', targetDep);
                    if (pkgsMap.has(maybe)) {
                        return pkgsMap.get(maybe);
                    }
                    dirSegments.pop();
                }
                // look for matching root package
                if (pkgsMap.has(targetDep)) {
                    return pkgsMap.get(targetDep);
                }
                // dependency not found
                if (required) {
                    console.error(`could not find ${depType} '${targetDep}' of '${dep._dir}'`);
                    process.exit(1);
                }
                return null;
            })
                .filter(dep => !!dep)
                .forEach(dep => flattenPkgDependencies(pkg, dep, pkgsMap));
        };
        // npm will in some cases add optionalDependencies to the list
        // of dependencies to the package.json it writes to node_modules.
        // We delete these here if they exist as they may result
        // in expected dependencies that are not found.
        if (dep.dependencies && dep.optionalDependencies) {
            Object.keys(dep.optionalDependencies).forEach(optionalDep => {
                delete dep.dependencies[optionalDep];
            });
        }
        findDeps(dep.dependencies, true, 'dependency');
        findDeps(dep.peerDependencies, false, 'peer dependency');
        // `optionalDependencies` that are missing should be silently
        // ignored since the npm/yarn will not fail if these dependencies
        // fail to install. Packages should handle the cases where these
        // dependencies are missing gracefully at runtime.
        // An example of this is the `chokidar` package which specifies
        // `fsevents` as an optionalDependency. On OSX, `fsevents`
        // is installed successfully, but on Windows & Linux, `fsevents`
        // fails to install and the package will not be present when
        // checking the dependencies of `chokidar`.
        findDeps(dep.optionalDependencies, false, 'optional dependency');
    }
/**
     * Flattens all transitive dependencies of a package
     * into a _dependencies array.
     */
Flattens all transitive dependencies of a package
into a _dependencies array.
Flattens all transitive dependencies of a package into a _dependencies array .
function flattenPkgDependencies(pkg, dep, pkgsMap) {
        if (pkg._dependencies.indexOf(dep) !== -1) {
            return;
        }
        pkg._dependencies.push(dep);
        const findDeps = function (targetDeps, required, depType) {
            Object.keys(targetDeps || {})
                .map(targetDep => {
                const dirSegments = dep._dir.split('/');
                while (dirSegments.length) {
                    const maybe = path.posix.join(...dirSegments, 'node_modules', targetDep);
                    if (pkgsMap.has(maybe)) {
                        return pkgsMap.get(maybe);
                    }
                    dirSegments.pop();
                }
                if (pkgsMap.has(targetDep)) {
                    return pkgsMap.get(targetDep);
                }
                if (required) {
                    console.error(`could not find ${depType} '${targetDep}' of '${dep._dir}'`);
                    process.exit(1);
                }
                return null;
            })
                .filter(dep => !!dep)
                .forEach(dep => flattenPkgDependencies(pkg, dep, pkgsMap));
        };
        if (dep.dependencies && dep.optionalDependencies) {
            Object.keys(dep.optionalDependencies).forEach(optionalDep => {
                delete dep.dependencies[optionalDep];
            });
        }
        findDeps(dep.dependencies, true, 'dependency');
        findDeps(dep.peerDependencies, false, 'peer dependency');
        findDeps(dep.optionalDependencies, false, 'optional dependency');
    }
function flattenPkgDependencies ( pkg , dep , pkgsMap ) { if ( pkg . _dependencies . indexOf ( dep ) !== - 1 ) { return ; } pkg . _dependencies . push ( dep ) ; const findDeps = function ( targetDeps , required , depType ) { Object . keys ( targetDeps || { } ) . map ( targetDep => { const dirSegments = dep . _dir . split ( '/' ) ; while ( dirSegments . length ) { const maybe = path . posix . join ( ... dirSegments , 'node_modules' , targetDep ) ; if ( pkgsMap . has ( maybe ) ) { return pkgsMap . get ( maybe ) ; } dirSegments . pop ( ) ; } if ( pkgsMap . has ( targetDep ) ) { return pkgsMap . get ( targetDep ) ; } if ( required ) { console . error ( ` ${ depType } ${ targetDep } ${ dep . _dir } ` ) ; process . exit ( 1 ) ; } return null ; } ) . filter ( dep => ! ! dep ) . forEach ( dep => flattenPkgDependencies ( pkg , dep , pkgsMap ) ) ; } ; if ( dep . dependencies && dep . optionalDependencies ) { Object . keys ( dep . optionalDependencies ) . forEach ( optionalDep => { delete dep . dependencies [ optionalDep ] ; } ) ; } findDeps ( dep . dependencies , true , 'dependency' ) ; findDeps ( dep . peerDependencies , false , 'peer dependency' ) ; findDeps ( dep . optionalDependencies , false , 'optional dependency' ) ; }
Flattens all transitive dependencies of a package
 into a _dependencies array.
Flattens all transitive dependencies of a package into a _dependencies array .
// circular dependency // look for matching nested package // look for matching root package // dependency not found // npm will in some cases add optionalDependencies to the list // of dependencies to the package.json it writes to node_modules. // We delete these here if they exist as they may result // in expected dependencies that are not found. // `optionalDependencies` that are missing should be silently // ignored since the npm/yarn will not fail if these dependencies // fail to install. Packages should handle the cases where these // dependencies are missing gracefully at runtime. // An example of this is the `chokidar` package which specifies // `fsevents` as an optionalDependency. On OSX, `fsevents` // is installed successfully, but on Windows & Linux, `fsevents` // fails to install and the package will not be present when // checking the dependencies of `chokidar`.