2
0

beforeTest.js 1.2 KB

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