From d7b8005551769e53711696242f694e20e1cf1cdc Mon Sep 17 00:00:00 2001 From: Tim Santeford Date: Sat, 10 Nov 2018 18:24:51 -0800 Subject: [PATCH] Added ability to save as jpeg --- index.js | 21 ++++++++++++++++++--- tests/run.test.js | 48 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 07ef209a..17fe9b6f 100644 --- a/index.js +++ b/index.js @@ -107,8 +107,8 @@ const defaults = userOptions => { console.log("🔥 asyncJs option renamed to asyncScriptTags"); options.asyncScriptTags = options.asyncJs; } - if (options.saveAs !== "html" && options.saveAs !== "png") { - console.log("🔥 saveAs supported values are html and png"); + if (options.saveAs !== "html" && options.saveAs !== "png" && options.saveAs !== "jpeg") { + console.log("🔥 saveAs supported values are html, png, and jpeg"); exit = true; } if (exit) throw new Error(); @@ -485,13 +485,26 @@ const saveAsPng = ({ page, filePath, options, route }) => { if (route.endsWith(".html")) { screenshotPath = filePath.replace(/\.html$/, ".png"); } else if (route === "/") { - screenshotPath = `${filePath}/index.png`; + screenshotPath = `${filePath}index.png`; } else { screenshotPath = `${filePath.replace(/\/$/, "")}.png`; } return page.screenshot({ path: screenshotPath }); }; +const saveAsJpeg = ({ page, filePath, options, route }) => { + mkdirp.sync(path.dirname(filePath)); + let screenshotPath; + if (route.endsWith(".html")) { + screenshotPath = filePath.replace(/\.html$/, ".jpeg"); + } else if (route === "/") { + screenshotPath = `${filePath}index.jpeg`; + } else { + screenshotPath = `${filePath.replace(/\/$/, "")}.jpeg`; + } + return page.screenshot({ path: screenshotPath }); +}; + const run = async (userOptions, { fs } = { fs: nativeFs }) => { let options; try { @@ -668,6 +681,8 @@ const run = async (userOptions, { fs } = { fs: nativeFs }) => { await saveAsHtml({ page, filePath, options, route, fs }); } else if (options.saveAs === "png") { await saveAsPng({ page, filePath, options, route, fs }); + } else if (options.saveAs === "jpeg") { + await saveAsJpeg({ page, filePath, options, route, fs }); } }, onEnd: () => { diff --git a/tests/run.test.js b/tests/run.test.js index 40122d57..0656a46c 100644 --- a/tests/run.test.js +++ b/tests/run.test.js @@ -1,5 +1,9 @@ // FIX: tests are slow - use unit tests instead of integration tests // TODO: capture console log from run function +const fs = require('fs'); +const writeFileSpy = jest.spyOn(fs, 'writeFile'); +writeFileSpy.mockImplementation((file, data, cb) => cb()); + const { mockFs } = require("./helper.js"); const { run } = require("./../index.js"); const snapRun = (fs, options) => @@ -61,6 +65,50 @@ describe("one page", () => { }); }); +describe("saveAs png", () => { + const source = "tests/examples/one-page"; + const cwd = process.cwd(); + const { + fs: mockedFs, + createReadStreamMock, + createWriteStreamMock, + } = mockFs(); + beforeAll(() => snapRun(mockedFs, { source, saveAs: 'png' })); + afterAll(() => writeFileSpy.mockClear()) + test("crawls / and saves as index.png to the same folder", () => { + expect(writeFileSpy).toHaveBeenCalledTimes(1); + expect(writeFileSpy.mock.calls[0][0]).toEqual(cwd + `/${source}/index.png`); + }); + test("copies (original) index.html to 200.html", () => { + expect(createReadStreamMock.mock.calls).toEqual([ + [`/${source}/index.html`] + ]); + expect(createWriteStreamMock.mock.calls).toEqual([[`/${source}/200.html`]]); + }); +}); + +describe("saveAs jpeg", () => { + const source = "tests/examples/one-page"; + const cwd = process.cwd(); + const { + fs: mockedFs, + createReadStreamMock, + createWriteStreamMock, + } = mockFs(); + beforeAll(() => snapRun(mockedFs, { source, saveAs: 'jpeg' })); + afterAll(() => writeFileSpy.mockClear()) + test("crawls / and saves as index.png to the same folder", () => { + expect(writeFileSpy).toHaveBeenCalledTimes(1); + expect(writeFileSpy.mock.calls[0][0]).toEqual(cwd + `/${source}/index.jpeg`); + }); + test("copies (original) index.html to 200.html", () => { + expect(createReadStreamMock.mock.calls).toEqual([ + [`/${source}/index.html`] + ]); + expect(createWriteStreamMock.mock.calls).toEqual([[`/${source}/200.html`]]); + }); +}); + describe("respects destination", () => { const source = "tests/examples/one-page"; const destination = "tests/examples/destination";