Skip to content

Commit

Permalink
Add support for WebP image type
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Oct 8, 2021
1 parent 1773572 commit 9cdb399
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 48 deletions.
75 changes: 38 additions & 37 deletions cli.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env node
import process from 'node:process';
import meow from 'meow';
import captureWebsite from 'capture-website';
import arrify from 'arrify';
Expand All @@ -14,8 +15,8 @@ const cli = meow(`
--output Image file path (writes it to stdout if omitted)
--width Page width [default: 1280]
--height Page height [default: 800]
--type Image type: png|jpeg [default: png]
--quality Image quality: 0...1 (Only for JPEG) [default: 1]
--type Image type: png|jpeg|webp [default: png]
--quality Image quality: 0...1 (Only for JPEG and WebP) [default: 1]
--scale-factor Scale the webpage \`n\` times [default: 2]
--list-devices Output a list of supported devices to emulate
--emulate-device Capture as if it were captured on the given device
Expand Down Expand Up @@ -83,109 +84,109 @@ const cli = meow(`
importMeta: import.meta,
flags: {
output: {
type: 'string'
type: 'string',
},
width: {
type: 'number'
type: 'number',
},
height: {
type: 'number'
type: 'number',
},
type: {
type: 'string'
type: 'string',
},
quality: {
type: 'number'
type: 'number',
},
scaleFactor: {
type: 'number'
type: 'number',
},
listDevices: {
type: 'boolean'
type: 'boolean',
},
emulateDevice: {
type: 'string'
type: 'string',
},
fullPage: {
type: 'boolean'
type: 'boolean',
},
defaultBackground: {
type: 'boolean'
type: 'boolean',
},
timeout: {
type: 'number'
type: 'number',
},
delay: {
type: 'number'
type: 'number',
},
waitForElement: {
type: 'string'
type: 'string',
},
element: {
type: 'string'
type: 'string',
},
hideElements: {
type: 'string',
isMultiple: true
isMultiple: true,
},
removeElements: {
type: 'string',
isMultiple: true
isMultiple: true,
},
clickElement: {
type: 'string'
type: 'string',
},
scrollToElement: {
type: 'string'
type: 'string',
},
disableAnimations: {
type: 'boolean'
type: 'boolean',
},
javascript: {
type: 'boolean',
default: true
default: true,
},
module: {
type: 'string',
isMultiple: true
isMultiple: true,
},
script: {
type: 'string',
isMultiple: true
isMultiple: true,
},
style: {
type: 'string',
isMultiple: true
isMultiple: true,
},
header: {
type: 'string'
type: 'string',
},
userAgent: {
type: 'string'
type: 'string',
},
cookie: {
type: 'string',
isMultiple: true
isMultiple: true,
},
authentication: {
type: 'string'
type: 'string',
},
debug: {
type: 'boolean'
type: 'boolean',
},
darkMode: {
type: 'boolean'
type: 'boolean',
},
launchOptions: {
type: 'string'
type: 'string',
},
overwrite: {
type: 'boolean'
type: 'boolean',
},
inset: {
type: 'string'
}
}
type: 'string',
},
},
});

let [input] = cli.input;
Expand Down Expand Up @@ -244,7 +245,7 @@ options.isJavaScriptEnabled = options.javascript;
const {
internalPrintFlags,
listDevices,
output
output,
} = options;

if (internalPrintFlags) {
Expand Down
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,17 @@
],
"dependencies": {
"arrify": "^3.0.0",
"capture-website": "^2.0.0",
"capture-website": "^2.1.0",
"get-stdin": "^9.0.0",
"meow": "^10.0.0",
"meow": "^10.1.1",
"split-on-first": "^3.0.0"
},
"devDependencies": {
"ava": "^3.15.0",
"create-test-server": "^3.0.1",
"execa": "^5.0.0",
"file-type": "^12.4.0",
"xo": "^0.39.1"
"execa": "^5.1.1",
"file-type": "^16.5.3",
"typescript": "^4.4.3",
"xo": "^0.45.0"
}
}
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ $ capture-website --help
--output Image file path (writes it to stdout if omitted)
--width Page width [default: 1280]
--height Page height [default: 800]
--type Image type: png|jpeg [default: png]
--quality Image quality: 0...1 (Only for JPEG) [default: 1]
--type Image type: png|jpeg|webp [default: png]
--quality Image quality: 0...1 (Only for JPEG and WebP) [default: 1]
--scale-factor Scale the webpage `n` times [default: 2]
--list-devices Output a list of supported devices to emulate
--emulate-device Capture as if it were captured on the given device
Expand Down
8 changes: 4 additions & 4 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import test from 'ava';
import execa from 'execa';
import createTestServer from 'create-test-server';
import fileType from 'file-type';
import FileType from 'file-type';

test('main', async t => {
const server = await createTestServer();
Expand All @@ -12,17 +12,17 @@ test('main', async t => {

const {stdout} = await execa('./cli.js', [server.url], {encoding: 'buffer'});

t.is(fileType(stdout).mime, 'image/png');
t.is((await FileType.fromBuffer(stdout)).mime, 'image/png');

await server.close();
});

test('support HTML input', async t => {
const {stdout} = await execa('./cli.js', [], {
input: '<h1>Unicorn</h1>',
encoding: 'buffer'
encoding: 'buffer',
});
t.is(fileType(stdout).mime, 'image/png');
t.is((await FileType.fromBuffer(stdout)).mime, 'image/png');
});

test('check flags', async t => {
Expand Down

0 comments on commit 9cdb399

Please sign in to comment.