beforeTest.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. /* eslint-disable global-require */
  2. const { execSync } = require('child_process');
  3. const { join } = require('path');
  4. const findChrome = require('carlo/lib/find_chrome');
  5. const detectInstaller = require('detect-installer');
  6. const installPuppeteer = () => {
  7. // find can use package manager
  8. const packages = detectInstaller(join(__dirname, '../'));
  9. // get installed package manager
  10. const packageName = packages.find(detectInstaller.hasPackageCommand) || 'npm';
  11. console.log(`🤖 will use ${packageName} install puppeteer`);
  12. const command = `${packageName} ${packageName.includes('yarn') ? 'add' : 'i'} puppeteer`;
  13. execSync(command, {
  14. stdio: 'inherit',
  15. });
  16. };
  17. const initPuppeteer = async () => {
  18. try {
  19. const findChromePath = await findChrome({});
  20. const { executablePath } = findChromePath;
  21. console.log(`🧲 find you browser in ${executablePath}`);
  22. return;
  23. } catch (error) {
  24. console.log('🧲 no find chrome');
  25. }
  26. try {
  27. require.resolve('puppeteer');
  28. } catch (error) {
  29. // need install puppeteer
  30. await installPuppeteer();
  31. }
  32. };
  33. initPuppeteer();