Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ability to save as jpeg #288

Merged
merged 1 commit into from
Nov 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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: () => {
Expand Down
48 changes: 48 additions & 0 deletions tests/run.test.js
Original file line number Diff line number Diff line change
@@ -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) =>
Expand Down Expand Up @@ -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";
Expand Down