const paths = require("../../paths");
const { npmScope } = require("../../config");
const glob = require("glob");
const globPath = `${npmScope}/*`;

/**
 * Returns a list of the packages in the directory
 */
function getCandidatePackages() {
  return glob.sync(globPath, { cwd: paths.nodeModules });
}

/**
 * Try npm link for each npm candidate package
 */
function tryNpmLink() {
  const packages = getCandidatePackages();
  const execSync = require("child_process").execSync;

  packages.forEach((name) => {
    try {
      execSync(`npm link ${name}`, { cwd: paths.root });
    } catch (e) {
      console.warn(`Could not npm link package ${name}`);
    }
  });
}

tryNpmLink();
