Advertisement
Guest User

Untitled

a guest
Dec 18th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const gulp = require('gulp');
  2. const through = require('through2');
  3. const del = require('del');
  4. const puppeteer = require('puppeteer');
  5. const ansi = require('ansi');
  6. const rename = require('gulp-rename');
  7. const runSequence = require('run-sequence');
  8. const vinylPaths = require('vinyl-paths');
  9. const cursor = ansi(process.stdout);
  10.  
  11. let totalWaitingTime = 0;
  12.  
  13.  
  14. const shotIt = async (page, fileName) => {
  15.     cursor.hex('#00ffff').bold();
  16.     console.log("\nPreparing: "+fileName);
  17.     cursor.reset();
  18.  
  19.     return new Promise(async(resolve, reject) => {
  20.  
  21.         try{
  22.             //const page = await browser.newPage();
  23.             await page.setUserAgent("puppeteer");
  24.  
  25.             const timeout = (ms) => {
  26.                 setTimeout(() => {}, 3000);
  27.                 return new Promise(resolve => setTimeout(resolve, ms));
  28.             };
  29.  
  30.             await timeout(3000);
  31.  
  32.             cursor.hex('#ffff00').bold();
  33.             console.log("\tOpening: http://localhost:8080/"+fileName);
  34.             cursor.reset();
  35.  
  36.             await page.goto(fileName);
  37.  
  38.            
  39.  
  40.             await page.setViewport({
  41.               height: 900,
  42.               width: 1140
  43.           });
  44.  
  45.             const bodyHandle = await page.$('body');
  46.  
  47.             const { width, height } = await bodyHandle.boundingBox();
  48.  
  49.             const bbb = Date.now();
  50.            
  51.             console.log("\tWaiting...");
  52.             await timeout(3000);
  53.             let tWaiting = Date.now() - bbb;
  54.             console.log("\tWaiting time: " + tWaiting + "ms");
  55.             totalWaitingTime += tWaiting;
  56.  
  57.             cursor.hex('#ffffff').bold();
  58.             console.log("\tTaking screenshot...");
  59.             cursor.reset();
  60.  
  61.             await page.screenshot({
  62.                 clip: {
  63.                     x: 0,
  64.                     y: 0,
  65.                     width,
  66.                     height
  67.                 },
  68.                 path: fileName.split(".html")[0]+".jpg"
  69.             });
  70.  
  71.             cursor.hex('#00ff00').bold();
  72.             console.log("\tShot Taken as "+fileName.split(".html")[0]+".jpg");
  73.             cursor.reset();
  74.  
  75.             await bodyHandle.dispose();
  76.             resolve();
  77.         }catch(err){
  78.             reject(err);
  79.         };
  80.     });
  81. };
  82.  
  83.  
  84. gulp.task('through', () => {
  85.     const array = [];
  86.     return gulp.src('./theme/rac/motorquote/**.html')
  87.     .pipe(through.obj( (chunk, enc, cb) => {
  88.         console.log(chunk.path);
  89.         array.push(chunk.path.split("/")[chunk.path.split("/").length - 1])
  90.         cb(null, chunk);
  91.     })).on('end', async() => {
  92.         const startTime = Date.now();
  93.         const browser = await puppeteer.launch();
  94.         const page = await browser.newPage();
  95.  
  96.         for(let pageName of array){
  97.             try{
  98.                 await shotIt(page, pageName);
  99.             }catch(err){
  100.                 console.log('failed', pageName, err);
  101.             }
  102.         };
  103.  
  104.         await browser.close();
  105.         console.log("Total Waiting Time: " + totalWaitingTime/1000 + "s");
  106.         console.log("Total Process Time: " + (Date.now() - startTime)/1000 + "s");
  107.  
  108.     });
  109. });
  110.  
  111. gulp.task('shot', (callback) => {
  112.   runSequence("through", callback);
  113. });
  114.  
  115. gulp.task('rename', function() {
  116.     return gulp.src('./dist/*.jpg')
  117.     .pipe(vinylPaths(del))
  118.     .pipe(rename({prefix: "dist--"}))
  119.     .pipe(gulp.dest('./dist/'))
  120. });
  121.  
  122. gulp.task('default', (callback) => {
  123.     runSequence("rename", "through", callback);
  124. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement