screenshot.js 3.58 KB
Newer Older
陈帅's avatar
陈帅 committed
1 2 3 4 5 6 7 8 9
/* eslint-disable eslint-comments/disable-enable-pair */
/* eslint-disable @typescript-eslint/no-var-requires */
/* eslint-disable eslint-comments/no-unlimited-disable */
const { spawn } = require('child_process');
const puppeteer = require('puppeteer');
const { join, dirname } = require('path');
const fs = require('fs');
const getNpmRegistry = require('getnpmregistry');
const execa = require('execa');
陈帅's avatar
陈帅 committed
10
const { kill } = require('cross-port-killer');
陈帅's avatar
陈帅 committed
11 12 13

const env = Object.create(process.env);
env.BROWSER = 'none';
陈帅's avatar
陈帅 committed
14
env.PORT = process.env.PORT || '2144';
陈帅's avatar
陈帅 committed
15 16 17
env.TEST = true;
env.COMPRESS = 'none';
env.PROGRESS = 'none';
陈帅's avatar
陈帅 committed
18
env.BLOCK_PAGES_LAYOUT = 'blankLayout';
陈帅's avatar
陈帅 committed
19 20 21 22 23 24 25

let browser;

const startServer = async path => {
  let once = false;
  return new Promise(resolve => {
    env.PAGES_PATH = path + '/src';
陈帅's avatar
陈帅 committed
26 27
    console.log(path);
    const startServer = spawn(/^win/.test(process.platform) ? 'npm.cmd' : 'npm', ['run', 'start'], {
陈帅's avatar
陈帅 committed
28 29 30 31 32 33 34 35 36 37 38 39 40
      env,
    });

    console.log('Starting development server');
    startServer.stdout.on('data', data => {
      console.log(data.toString());
      // hack code , wait umi
      if (!once && data.toString().indexOf('Compiled successfully') >= 0) {
        // eslint-disable-next-line
        once = true;
        return resolve(startServer);
      }
    });
陈帅's avatar
陈帅 committed
41 42 43
    startServer.on('exit', () => {
      kill(env.PORT || 8000);
    });
陈帅's avatar
陈帅 committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
  });
};

const autoScroll = page => {
  return page.evaluate(() => {
    return new Promise((resolve, reject) => {
      var totalHeight = 0;
      var distance = 100;
      var timer = setInterval(() => {
        var scrollHeight = document.body.scrollHeight;
        window.scrollBy(0, distance);
        totalHeight += distance;
        if (totalHeight >= scrollHeight) {
          clearInterval(timer);
          resolve();
        }
      }, 100);
    });
  });
};

const getImage = async (page, path) => {
陈帅's avatar
陈帅 committed
66
  kill(env.PORT || 8000);
陈帅's avatar
陈帅 committed
67 68
  const server = await startServer(path);

陈帅's avatar
陈帅 committed
69
  await page.reload(`http://127.0.0.1:${env.PORT}`);
陈帅's avatar
陈帅 committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86

  await page.setViewport({
    width: 1440,
    height: 800,
  });

  await autoScroll(page);

  await page.screenshot({
    path: join(path, 'snapshot.png'),
    fullPage: true,
  });
  server.kill();
};

const openBrowser = async () => {
  browser = await puppeteer.launch({
陈帅's avatar
陈帅 committed
87
    headless: true,
陈帅's avatar
陈帅 committed
88 89 90 91 92 93 94 95 96
    args: [
      '--disable-gpu',
      '--disable-dev-shm-usage',
      '--no-first-run',
      '--no-zygote',
      '--no-sandbox',
    ],
  });
  const page = await browser.newPage();
陈帅's avatar
陈帅 committed
97
  page.goto(`http://127.0.0.1:${env.PORT}`);
陈帅's avatar
陈帅 committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
  return page;
};

const getAllFile = async () => {
  const cwd = join(__dirname, '../');
  const files = fs.readdirSync(cwd);
  return files.filter(path => {
    const itemPath = join(cwd, path);
    const stat = fs.statSync(itemPath);
    if (path.includes('.') || path.includes('_') || path.includes('node_modules')) {
      return false;
    }
    if (stat.isDirectory()) {
      return true;
    }
    return false;
  });
};

getAllFile().then(async dirList => {
  const registry = await getNpmRegistry();
  const page = await openBrowser();
  const loopGetImage = async index => {
    try {
      console.log('install ' + dirList[index] + ' dependencies');
      await execa('yarn', ['install', `--registry=${registry}`], {
        cwd: join(__dirname, '../' + dirList[index]),
      });
      await getImage(page, dirList[index]);

陈帅's avatar
陈帅 committed
128
      if (dirList.length > index && dirList[index + 1]) {
陈帅's avatar
陈帅 committed
129 130 131 132 133 134 135 136 137 138 139 140
        console.log('Screenshot ' + dirList[index]);

        return loopGetImage(index + 1);
      }
    } catch (error) {
      console.log(error);
    }
    return Promise.resolve(true);
  };
  await loopGetImage(0);
  browser.close();
});