|
| 1 | +const chalk = require('chalk'); |
| 2 | +const co = require('co'); |
| 3 | +const express = require('express'); |
| 4 | +const fs = require('fs'); |
| 5 | +const Nightmare = require('nightmare'); |
| 6 | +const path = require('path'); |
| 7 | + |
| 8 | +const screenshotTargetWidth = 1024; |
| 9 | +const screenshotMaxHeight = 768; |
| 10 | +const screenshotSelector = '[data-name="component-container"]'; |
| 11 | +const serverPort = 3333; |
| 12 | + |
| 13 | +module.exports = componentsForNavPath => new Promise((resolve, reject) => { |
| 14 | + console.log(chalk.magenta('Working on components screenshots...')); |
| 15 | + if (componentsForNavPath === undefined || !fs.existsSync(componentsForNavPath)) { |
| 16 | + reject('Can not find components nav JSON file'); |
| 17 | + } |
| 18 | + const componentsForNav = JSON.parse(fs.readFileSync(componentsForNavPath, 'utf8')); |
| 19 | + |
| 20 | + // Setup a quick static HTML server so that CSS is loaded correctly. |
| 21 | + const app = express(); |
| 22 | + app.set('port', serverPort); |
| 23 | + app.use(express.static(path.join(__dirname, '..'))); |
| 24 | + const server = app.listen(app.get('port'), () => { |
| 25 | + console.log('- Started static HTML server on port:', server.address().port); |
| 26 | + }); |
| 27 | + |
| 28 | + // Initialize nightmware now. |
| 29 | + const nightmare = Nightmare({ // eslint-disable-line |
| 30 | + show: false, |
| 31 | + frame: false, |
| 32 | + useContentSize: true, |
| 33 | + switches: { |
| 34 | + // Unfortunately .viewport() sets the viewport size in *CSS pixels*. |
| 35 | + // Let's force device pixel ratio to 1 otherwise screenshots will vary in size |
| 36 | + // depending on the machine running this script (double for Retina Macbook Pro) |
| 37 | + // https://github.com/segmentio/nightmare/issues/498#issuecomment-265948400 |
| 38 | + 'force-device-scale-factor': '1', |
| 39 | + }, |
| 40 | + }); |
| 41 | + |
| 42 | + const renderPromise = co(function* generator() { |
| 43 | + const categories = Object.keys(componentsForNav); |
| 44 | + for (let cat_idx = 0; cat_idx < categories.length; cat_idx += 1) { |
| 45 | + const category = categories[cat_idx]; |
| 46 | + console.log(chalk.yellow('- Processing category:'), category); |
| 47 | + |
| 48 | + for (let comp_idx = 0; comp_idx < componentsForNav[category].length; comp_idx += 1) { |
| 49 | + const component = componentsForNav[category][comp_idx]; |
| 50 | + let selector = screenshotSelector; |
| 51 | + if (component.frontMatter.screenshot && component.frontMatter.screenshot.selector) { |
| 52 | + selector = `${selector} ${component.frontMatter.screenshot.selector}`; |
| 53 | + } |
| 54 | + |
| 55 | + // Grab the size of the component enclosing rectangle |
| 56 | + // https://github.com/segmentio/nightmare/issues/498#issuecomment-189156529 |
| 57 | + const componentRect = yield nightmare |
| 58 | + .viewport(screenshotTargetWidth, screenshotMaxHeight) |
| 59 | + // .wait(1000) |
| 60 | + .goto(`http://localhost:${app.get('port')}${component.href}`) |
| 61 | + .wait(selector) |
| 62 | + .evaluate((_selector) => { |
| 63 | + // Hide scrollbar that could pop up due to .scrollTo |
| 64 | + const sheet = document.styleSheets[0]; |
| 65 | + sheet.insertRule('::-webkit-scrollbar { display:none; }'); |
| 66 | + const element = document.querySelector(_selector); |
| 67 | + if (element) { |
| 68 | + const rect = element.getBoundingClientRect(); |
| 69 | + return { |
| 70 | + x: Math.round(rect.left), |
| 71 | + y: Math.round(rect.top), |
| 72 | + width: Math.round(rect.width), |
| 73 | + height: Math.round(rect.height), |
| 74 | + }; |
| 75 | + } |
| 76 | + return false; |
| 77 | + }, selector); |
| 78 | + |
| 79 | + // Capture the component |
| 80 | + if (componentRect !== false) { |
| 81 | + componentRect.height = Math.min(componentRect.height, screenshotMaxHeight); |
| 82 | + yield nightmare |
| 83 | + // we can not use .screenshot() with componentRect, so constrain the viewport instead |
| 84 | + .viewport(componentRect.width, componentRect.height) |
| 85 | + .scrollTo(componentRect.y, componentRect.x) |
| 86 | + // .wait(1000) |
| 87 | + // do *not* use componentRect in .screenshot() below or risk distortions |
| 88 | + .screenshot(component.screenshot.path) |
| 89 | + .then(() => { |
| 90 | + console.log(' * Created screenshot: ', component.screenshot.path); |
| 91 | + }); |
| 92 | + } |
| 93 | + } // Loop over components |
| 94 | + } // Loop over categories |
| 95 | + yield nightmare.end(); |
| 96 | + }).then(() => { |
| 97 | + console.log('- Closed static HTML server'); |
| 98 | + server.close(); |
| 99 | + console.log(chalk.magenta('Done with components screenshots!')); |
| 100 | + }, (err) => { |
| 101 | + console.error(err); |
| 102 | + reject(err); |
| 103 | + }); |
| 104 | + |
| 105 | + return resolve(renderPromise); |
| 106 | + |
| 107 | + // TODO: optimize all PNG files, eventually resize to smaller width |
| 108 | + // const imagemin = require('imagemin'); |
| 109 | + // const imageminPngquant = require('imagemin-pngquant'); |
| 110 | + // imagemin(['components/*.{jpg,png}'], ??, { |
| 111 | + // plugins: [ |
| 112 | + // imageminPngquant({quality: '65-80'}) |
| 113 | + // ] |
| 114 | + // }).then(files => { |
| 115 | + // console.log(files); |
| 116 | + // //=> [{data: <Buffer 89 50 4e …>, path: 'build/images/foo.jpg'}, …] |
| 117 | + // }); |
| 118 | + // |
| 119 | +}); // return promise |
0 commit comments