Skip to content

Commit

Permalink
Rename .src() method to .source() and .dest() to .destination()
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Aug 30, 2022
1 parent 64cd962 commit 29ad6d6
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 82 deletions.
10 changes: 6 additions & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@ jobs:
fail-fast: false
matrix:
node-version:
- 18
- 16
- 14
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm test
- uses: codecov/codecov-action@v1
if: matrix.node-version == 14
- uses: codecov/codecov-action@v3
if: matrix.node-version == 16
with:
fail_ci_if_error: true
10 changes: 10 additions & 0 deletions example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import Pageres from './dist/index.js';

await new Pageres({delay: 2})
.source('https://github.com/sindresorhus/pageres', ['480x320', '1024x768'], {crop: true})
.source('https://sindresorhus.com', ['1280x1024', '1920x1080'])
.source('data:text/html,<h1>Awesome!</h1>', ['1024x768'])
.destination('screenshots')
.run();

console.log('Finished generating screenshots!');
16 changes: 8 additions & 8 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ Note to Linux users: If you get a "No usable sandbox!" error, you need to enable
import Pageres from 'pageres';

await new Pageres({delay: 2})
.src('https://github.com/sindresorhus/pageres', ['480x320', '1024x768'], {crop: true})
.src('https://sindresorhus.com', ['1280x1024', '1920x1080'])
.src('data:text/html,<h1>Awesome!</h1>', ['1024x768'])
.dest('screenshots')
.source('https://github.com/sindresorhus/pageres', ['480x320', '1024x768'], {crop: true})
.source('https://sindresorhus.com', ['1280x1024', '1920x1080'])
.source('data:text/html,<h1>Awesome!</h1>', ['1024x768'])
.destination('screenshots')
.run();

console.log('Finished generating screenshots!');
Expand Down Expand Up @@ -197,14 +197,14 @@ await new Pageres({
await page.waitForSelector('.finished');
}
})
.src('https://github.com/sindresorhus/pageres', ['480x320', '1024x768', 'iphone 5s'], {crop: true})
.dest(__dirname)
.source('https://github.com/sindresorhus/pageres', ['480x320', '1024x768', 'iphone 5s'], {crop: true})
.destination('screenshots')
.run();

console.log('Finished generating screenshots!');
```

### pageres.src(url, sizes, options?)
### pageres.source(url, sizes, options?)

Add a page to screenshot.

Expand Down Expand Up @@ -232,7 +232,7 @@ Type: `object`

Options set here will take precedence over the ones set in the constructor.

### pageres.dest(directory)
### pageres.destination(directory)

Set the destination directory.

Expand Down
48 changes: 24 additions & 24 deletions source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,8 @@ export type Options = {
await page.waitForSelector('.finished');
}
})
.src('https://github.com/sindresorhus/pageres', ['480x320', '1024x768'], {crop: true})
.dest('screenshots')
.source('https://github.com/sindresorhus/pageres', ['480x320', '1024x768'], {crop: true})
.destination('screenshots')
.run();
console.log('Finished generating screenshots!');
Expand All @@ -190,7 +190,7 @@ export type Options = {
};

/**
A page to screenshot added in {@link Pageres.src}.
A page to screenshot added in {@link Pageres.source}.
*/
export type Source = {
/**
Expand All @@ -210,7 +210,7 @@ export type Source = {
};

/**
A destination directory set in {@link Pageres.dest}.
A destination directory set in {@link Pageres.destination}.
*/
export type Destination = string;

Expand Down Expand Up @@ -241,7 +241,7 @@ Capture screenshots of websites in various resolutions. A good way to make sure
*/
export default class Pageres extends EventEmitter {
readonly #options: Writable<Options>;
#stats: Stats = {} as Stats;
#stats: Stats = {} as Stats; // eslint-disable-line @typescript-eslint/consistent-type-assertions
readonly #items: Screenshot[] = [];
readonly #sizes: string[] = [];
readonly #urls: string[] = [];
Expand All @@ -266,7 +266,7 @@ export default class Pageres extends EventEmitter {
@returns List of pages that have been already been added.
*/
src(): Source[];
source(): Source[];

/**
Add a page to screenshot.
Expand All @@ -283,14 +283,14 @@ export default class Pageres extends EventEmitter {
import Pageres from 'pageres';
const pageres = new Pageres({delay: 2})
.src('https://github.com/sindresorhus/pageres', ['480x320', '1024x768'], {crop: true})
.src('https://sindresorhus.com', ['1280x1024', '1920x1080'])
.src('data:text/html,<h1>Awesome!</h1>', ['1024x768'], {delay: 1});
.source('https://github.com/sindresorhus/pageres', ['480x320', '1024x768'], {crop: true})
.source('https://sindresorhus.com', ['1280x1024', '1920x1080'])
.source('data:text/html,<h1>Awesome!</h1>', ['1024x768'], {delay: 1});
```
*/
src(url: string, sizes: readonly string[], options?: Options): this;
source(url: string, sizes: readonly string[], options?: Options): this;

src(url?: string, sizes?: readonly string[], options?: Options): this | Source[] {
source(url?: string, sizes?: readonly string[], options?: Options): this | Source[] {
if (url === undefined) {
return this.#_source;
}
Expand All @@ -311,7 +311,7 @@ export default class Pageres extends EventEmitter {
/**
Get the destination directory.
*/
dest(): Destination;
destination(): Destination;

/**
Set the destination directory.
Expand All @@ -321,13 +321,13 @@ export default class Pageres extends EventEmitter {
import Pageres from 'pageres';
const pageres = new Pageres()
.src('https://github.com/sindresorhus/pageres', ['480x320'])
.dest('screenshots');
.source('https://github.com/sindresorhus/pageres', ['480x320'])
.destination('screenshots');
```
*/
dest(directory: Destination): this;
destination(directory: Destination): this;

dest(directory?: Destination): this | Destination {
destination(directory?: Destination): this | Destination {
if (directory === undefined) {
return this.#_destination;
}
Expand All @@ -351,13 +351,13 @@ export default class Pageres extends EventEmitter {
import Pageres from 'pageres';
await new Pageres({delay: 2})
.src('https://sindresorhus.com', ['1280x1024'])
.dest('screenshots')
.source('https://sindresorhus.com', ['1280x1024'])
.destination('screenshots')
.run();
```
*/
async run(): Promise<Screenshot[]> {
await Promise.all(this.src().map(async (source: Source): Promise<void> => {
await Promise.all(this.source().map(async (source: Source): Promise<void> => {
const options = {
...this.#options,
...source.options,
Expand Down Expand Up @@ -393,7 +393,7 @@ export default class Pageres extends EventEmitter {
this.#stats.sizes = arrayUniq(this.#sizes).length;
this.#stats.screenshots = this.#items.length;

if (!this.dest()) {
if (!this.destination()) {
return this.#items;
}

Expand All @@ -410,8 +410,8 @@ export default class Pageres extends EventEmitter {
import Pageres from 'pageres';
const pageres = new Pageres({delay: 2})
.src('https://sindresorhus.com', ['1280x1024', '1920x1080'])
.dest('screenshots');
.source('https://sindresorhus.com', ['1280x1024', '1920x1080'])
.destination('screenshots');
await pageres.run();
Expand Down Expand Up @@ -450,8 +450,8 @@ export default class Pageres extends EventEmitter {

private async save(screenshots: Screenshot[]): Promise<void> {
await Promise.all(screenshots.map(async screenshot => {
await makeDir(this.dest());
const dest = path.join(this.dest(), screenshot.filename);
await makeDir(this.destination());
const dest = path.join(this.destination(), screenshot.filename);
await fsPromises.writeFile(dest, screenshot);
}));
}
Expand Down
2 changes: 1 addition & 1 deletion test/cookie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ async function cookieTest(input: string | Cookie, t: ExecutionContext): Promise<
const server = await createCookieServer();

const screenshots = await new Pageres({cookies: [input]})
.src(server.url, ['320x480'])
.source(server.url, ['320x480'])
.run();

server.close();
Expand Down
Loading

0 comments on commit 29ad6d6

Please sign in to comment.