const { execa } = require('@umijs/utils');
const { join } = require('path');
const getPackages = require('./utils/getPackages');

process.setMaxListeners(Infinity);

module.exports = (publishPkgList) => {
  const pkgList = (publishPkgList || getPackages()).map((pkg) => {
    // eslint-disable-next-line global-require,import/no-dynamic-require
    const [name, path] = pkg.split('!');
    // eslint-disable-next-line global-require,import/no-dynamic-require
    return require(join(__dirname, '../packages', path, name, 'package.json')).name;
  });
  const commands = pkgList.map((pkg) => {
    const subprocess = execa('tnpm', ['sync', pkg]);
    subprocess.stdout.pipe(process.stdout);
    return subprocess;
  });
  Promise.all(commands);
};
