import { ProjectGraph } from '@nx/devkit';

export function findAllNpmDependencies(
  graph: ProjectGraph,
  projectName: string,
  list: string[] = [],
  seen = new Set<string>()
) {
  // In case of bad circular dependencies
  if (seen.has(projectName)) {
    return list;
  }
  seen.add(projectName);

  const node = graph.externalNodes[projectName];

  // Don't want to include '@nx/react-native' because React Native
  // autolink will warn that the package has no podspec file for iOS.
  if (node) {
    if (
      node.name !== 'npm:@nx/react-native' &&
      node.name !== 'npm:@nrwl/react-native'
    ) {
      list.push(node.data.packageName);
    }
  } else {
    // it's workspace project, search for it's dependencies
    graph.dependencies[projectName]?.forEach((dep) =>
      findAllNpmDependencies(graph, dep.target, list, seen)
    );
  }
  return list;
}
