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

Automatically create a file or smartly send the data to stdout when piped #36

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
91 changes: 81 additions & 10 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@ import captureWebsite from 'capture-website';
import arrify from 'arrify';
import splitOnFirst from 'split-on-first';
import getStdin from 'get-stdin';
import filenamify from 'filenamify';
import { URL } from 'url';
import { fstat, accessSync, constants } from 'fs';
import { basename, extname } from 'path';

const cli = meow(`
Usage
$ capture-website <url|file>
$ echo "<h1>Unicorn</h1>" | capture-website

Options
--output Image file path (writes it to stdout if omitted)
--output Image file path (names file based on URL if omitted)
--width Page width [default: 1280]
--height Page height [default: 800]
--type Image type: png|jpeg|webp [default: png]
Expand Down Expand Up @@ -46,10 +50,12 @@ const cli = meow(`
--inset Inset the screenshot relative to the viewport or \`--element\`. Accepts a number or four comma-separated numbers for top, right, left, and bottom.

Examples
$ capture-website https://sindresorhus.com --output=screenshot.png
$ capture-website index.html --output=screenshot.png
$ echo "<h1>Unicorn</h1>" | capture-website --output=screenshot.png
$ capture-website https://sindresorhus.com | open -f -a Preview
$ capture-website https://sindresorhus.com
$ capture-website https://sindresorhus.com --output=screenshot.png
$ capture-website index.html --output=screenshot.png
$ capture-website file:///tmp/index.html --full-page --output=-
$ echo "<h1>Unicorn</h1>" | capture-website --output=screenshot.png
$ capture-website https://sindresorhus.com | open -f -a Preview

Flag examples
--output=screenshot.png
Expand Down Expand Up @@ -94,6 +100,7 @@ const cli = meow(`
},
type: {
type: 'string',
default: 'png',
},
quality: {
type: 'number',
Expand Down Expand Up @@ -246,6 +253,8 @@ options.isJavaScriptEnabled = options.javascript;
internalPrintFlags,
listDevices,
output,
type,
overwrite,
} = options;

if (internalPrintFlags) {
Expand All @@ -268,9 +277,71 @@ options.isJavaScriptEnabled = options.javascript;
process.exit(1);
}

if (output) {
await captureWebsite.file(input, output, options);
} else {
process.stdout.write(await captureWebsite.buffer(input, options));
}
// Performing stat over stdout (file descriptor 1)
await fstat(1, async function(e, stat) {
if (e) {
console.error('Error: ' + e.message);
process.exit(1);
}

// Check if output is piped
if (!output && stat.isFIFO() || output == '-') {
try {
process.stdout.write(await captureWebsite.buffer(input, options));
} catch (e) {
console.error('Error: ' + e.message);
process.exit(1);
}
} else {
let filename = output;

if (!filename) {
if (options.inputType == 'html') {
filename = 'stdin';
} else {
let url;

try {
// Check if input is a URL
url = new URL(input);
} catch(e) {}

if (url) {
// Input is a URL
filename = filenamify(url.hostname + url.pathname, { replacement: '-' });
} else {
// Input is a file path
try {
// Check if input file exists
accessSync(input, constants.F_OK);
} catch (e) {
console.error('Error: ' + e.message);
process.exit(1);
}

filename = basename(input, extname(input));
}
}

filename += '.' + type;
}

try {
// Check if output file already exists
accessSync(filename, constants.F_OK);

if (!overwrite) {
console.error("'" + filename + "' already exists, use --overwrite to bypass this error");
process.exit(1);
}
} catch (e) {}

try {
await captureWebsite.file(input, filename, options);
} catch (e) {
console.error('Error: ' + e.message);
process.exit(1);
}
}
});
})();
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@
"capture-website": "^2.1.0",
"get-stdin": "^9.0.0",
"meow": "^10.1.1",
"split-on-first": "^3.0.0"
"split-on-first": "^3.0.0",
"filenamify": "^5.0.1"
},
"devDependencies": {
"ava": "^3.15.0",
Expand Down
7 changes: 6 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ $ capture-website --help
$ echo "<h1>Unicorn</h1>" | capture-website

Options
--output Image file path (writes it to stdout if omitted)
--output Image file path (names file based on URL if omitted)
--width Page width [default: 1280]
--height Page height [default: 800]
--type Image type: png|jpeg|webp [default: png]
Expand Down Expand Up @@ -56,8 +56,10 @@ $ capture-website --help
--inset Inset the screenshot relative to the viewport or \`--element\`. Accepts a number or four comma-separated numbers for top, right, left, and bottom.

Examples
$ capture-website https://sindresorhus.com
$ capture-website https://sindresorhus.com --output=screenshot.png
$ capture-website index.html --output=screenshot.png
$ capture-website file:///tmp/index.html --full-page --output=-
$ echo "<h1>Unicorn</h1>" | capture-website --output=screenshot.png
$ capture-website https://sindresorhus.com | open -f -a Preview

Expand Down Expand Up @@ -109,6 +111,9 @@ You can run this:
```sh
filename='urls.txt'

< "$filename" xargs -I{} capture-website "{}"

# Or, for custom output file naming
while read url; do
capture-website "$url" --output "screenshot-$(echo "$url" | sed -e 's/[^A-Za-z0-9._-]//g').png"
done < "$filename"
Expand Down