@d-zero/print 1.2.0 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -48,7 +48,7 @@ IDが指定されていない場合は、連番がIDとして使用されます
48
48
 
49
49
  ## ページフック
50
50
 
51
- [Frontmatter](https://jekyllrb.com/docs/front-matter/)の`hooks`に配列としてスクリプトファイルのパスを渡すと、ページを開いた後(厳密にはPuppetterの`waitUntil: 'networkidle0'`のタイミング直後)にそれらのスクリプトを実行します。スクリプトは配列の順番通りに逐次実行されます。
51
+ [Frontmatter](https://jekyllrb.com/docs/front-matter/)の`hooks`に配列としてスクリプトファイルのパスを渡すと、ページを開いた後(厳密にはPuppeteerの`waitUntil: 'networkidle0'`のタイミング直後)にそれらのスクリプトを実行します。スクリプトは配列の順番通りに逐次実行されます。
52
52
 
53
53
  ```txt
54
54
  ---
package/dist/cli.js CHANGED
@@ -1,29 +1,44 @@
1
1
  #!/usr/bin/env node
2
- import minimist from 'minimist';
3
- import { print } from './print.js';
2
+ import { createCLI, parseCommonOptions } from '@d-zero/cli-core';
3
+ import { print } from './print-main-process.js';
4
4
  import { readConfig } from './read-config.js';
5
- const cli = minimist(process.argv.slice(2), {
6
- alias: {
5
+ const { options, args, hasConfigFile } = createCLI({
6
+ aliases: {
7
7
  f: 'listfile',
8
8
  t: 'type',
9
9
  },
10
+ usage: [
11
+ 'Usage:',
12
+ '\tprint -f <listfile> [--type <png|pdf|note>] [--limit <number>] [--debug]',
13
+ '\tprint <url>... [--type <png|pdf|note>] [--limit <number>] [--debug]',
14
+ ],
15
+ parseArgs: (cli) => ({
16
+ ...parseCommonOptions(cli),
17
+ listfile: cli.listfile,
18
+ type: cli.type,
19
+ }),
20
+ validateArgs: (options, cli) => {
21
+ return !!(options.listfile?.length || cli._.length > 0);
22
+ },
10
23
  });
11
- const limit = cli.limit ? Number.parseInt(cli.limit) : undefined;
12
- const debug = !!cli.debug;
13
- const verbose = !!cli.verbose;
14
- const type = cli.type === 'note' ? 'note' : cli.type === 'pdf' ? 'pdf' : 'png';
15
- if (cli.listfile?.length) {
16
- const { urlList, hooks } = await readConfig(cli.listfile);
17
- await print(urlList, { type, limit, debug, verbose, hooks });
24
+ const type = options.type === 'note' ? 'note' : options.type === 'pdf' ? 'pdf' : 'png';
25
+ if (hasConfigFile) {
26
+ const { urlList, hooks } = await readConfig(options.listfile);
27
+ await print(urlList, {
28
+ type,
29
+ limit: options.limit,
30
+ debug: options.debug,
31
+ verbose: options.verbose,
32
+ hooks,
33
+ });
18
34
  process.exit(0);
19
35
  }
20
- if (cli._.length > 0) {
21
- await print(cli._, { type, limit, verbose, debug });
36
+ if (args.length > 0) {
37
+ await print(args, {
38
+ type,
39
+ limit: options.limit,
40
+ verbose: options.verbose,
41
+ debug: options.debug,
42
+ });
22
43
  process.exit(0);
23
44
  }
24
- process.stderr.write([
25
- 'Usage:',
26
- '\tprint -f <listfile> [--type <png|pdf|note>] [--limit <number>] [--debug]',
27
- '\tprint <url>... [--type <png|pdf|note>] [--limit <number>] [--debug]',
28
- ].join('\n') + '\n');
29
- process.exit(1);
@@ -1,5 +1,5 @@
1
- import type { Page } from '@d-zero/puppeteer-page';
2
1
  import type { Screenshot } from '@d-zero/puppeteer-screenshot';
2
+ import type { Page } from 'puppeteer';
3
3
  /**
4
4
  *
5
5
  * @param page
@@ -1,5 +1,5 @@
1
- import type { Page } from '@d-zero/puppeteer-page';
2
1
  import type { Screenshot } from '@d-zero/puppeteer-screenshot';
2
+ import type { Page } from 'puppeteer';
3
3
  /**
4
4
  *
5
5
  * @param page
@@ -1,5 +1,5 @@
1
- import type { Page } from '@d-zero/puppeteer-page';
2
1
  import type { PageHook } from '@d-zero/puppeteer-page-scan';
2
+ import type { Page } from 'puppeteer';
3
3
  /**
4
4
  *
5
5
  * @param page
@@ -1,5 +1,5 @@
1
- import type { Page } from '@d-zero/puppeteer-page';
2
1
  import type { PageHook } from '@d-zero/puppeteer-screenshot';
2
+ import type { Page } from 'puppeteer';
3
3
  /**
4
4
  *
5
5
  * @param page
@@ -0,0 +1,7 @@
1
+ import type { PrintType } from './types.js';
2
+ import type { PageHook } from '@d-zero/puppeteer-page-scan';
3
+ export type ChildProcessParams = {
4
+ dir: string;
5
+ type: PrintType;
6
+ hooks?: readonly PageHook[];
7
+ };
@@ -0,0 +1,27 @@
1
+ import path from 'node:path';
2
+ import { createChildProcess } from '@d-zero/puppeteer-dealer';
3
+ import { pngToPdf } from './modules/png-to-pdf.js';
4
+ import { printPdf } from './modules/print-pdf.js';
5
+ import { printPng } from './modules/print-png.js';
6
+ createChildProcess((param) => {
7
+ const { dir, type, hooks } = param;
8
+ return {
9
+ async eachPage({ page, id, url }, logger) {
10
+ const ext = type === 'pdf' ? 'pdf' : 'png';
11
+ const fileName = `${id}.${ext}`;
12
+ const filePath = path.resolve(dir, fileName);
13
+ if (type === 'pdf') {
14
+ await printPdf(page, url, filePath, logger, hooks);
15
+ logger('🔚 Closing');
16
+ return;
17
+ }
18
+ const result = await printPng(page, url, id, filePath, logger, hooks);
19
+ if (type === 'png') {
20
+ logger('🔚 Closing');
21
+ return;
22
+ }
23
+ await pngToPdf(page, result, logger);
24
+ logger('🔚 Closing');
25
+ },
26
+ };
27
+ });
@@ -1,5 +1,6 @@
1
1
  import type { PrintType } from './types.js';
2
2
  import type { PageHook } from '@d-zero/puppeteer-page-scan';
3
+ import type { LaunchOptions } from 'puppeteer';
3
4
  export interface PrintOptions {
4
5
  readonly type?: PrintType;
5
6
  readonly limit?: number;
@@ -15,4 +16,4 @@ export interface PrintOptions {
15
16
  export declare function print(urlList: readonly (string | {
16
17
  id: string | null;
17
18
  url: string;
18
- })[], options?: PrintOptions): Promise<void>;
19
+ })[], options?: PrintOptions & LaunchOptions): Promise<void>;
@@ -0,0 +1,30 @@
1
+ import { mkdir } from 'node:fs/promises';
2
+ import path from 'node:path';
3
+ import { createProcess, deal } from '@d-zero/puppeteer-dealer';
4
+ import c from 'ansi-colors';
5
+ /**
6
+ *
7
+ * @param urlList
8
+ * @param options
9
+ */
10
+ export async function print(urlList, options) {
11
+ const dir = path.resolve(process.cwd(), '.print');
12
+ await mkdir(dir, { recursive: true }).catch(() => { });
13
+ const type = options?.type ?? 'png';
14
+ await deal(urlList.map((url) => {
15
+ if (typeof url === 'string') {
16
+ return { id: null, url };
17
+ }
18
+ return url;
19
+ }), (_, done, total) => {
20
+ return `${c.bold.magenta('🎨 Print pages')} ${c.bgBlueBright(` ${type} `)} ${done}/${total}`;
21
+ }, () => {
22
+ return createProcess(path.resolve(import.meta.dirname, 'print-child-process.js'), {
23
+ dir,
24
+ type,
25
+ hooks: options?.hooks,
26
+ }, {
27
+ ...options,
28
+ });
29
+ });
30
+ }
@@ -1,22 +1,16 @@
1
- import fs from 'node:fs/promises';
2
- import path from 'node:path';
3
1
  import { readPageHooks } from '@d-zero/puppeteer-page-scan';
4
2
  import { toKvList } from '@d-zero/readtext/list';
5
- import fm from 'front-matter';
3
+ import { readConfigFile } from '@d-zero/shared/config-reader';
6
4
  /**
7
5
  *
8
6
  * @param filePath
9
7
  */
10
8
  export async function readConfig(filePath) {
11
- const fileContent = await fs.readFile(filePath, 'utf8');
12
- const content =
13
- // @ts-ignore
14
- fm(fileContent);
9
+ const { content, baseDir } = await readConfigFile(filePath);
15
10
  const urlList = toKvList(content.body).map((kv) => ({
16
11
  id: kv.value ? kv.key : null,
17
12
  url: kv.value || kv.key,
18
13
  }));
19
- const baseDir = path.dirname(filePath);
20
14
  const hooks = await readPageHooks(content.attributes?.hooks ?? [], baseDir);
21
15
  return {
22
16
  urlList,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@d-zero/print",
3
- "version": "1.2.0",
3
+ "version": "2.1.0",
4
4
  "description": "Print web pages to PDF or image files.",
5
5
  "author": "D-ZERO",
6
6
  "license": "MIT",
@@ -27,18 +27,18 @@
27
27
  "clean": "tsc --build --clean"
28
28
  },
29
29
  "dependencies": {
30
- "@d-zero/html-distiller": "1.0.2",
31
- "@d-zero/puppeteer-dealer": "0.3.0",
32
- "@d-zero/puppeteer-page-scan": "3.0.0",
33
- "@d-zero/puppeteer-screenshot": "3.0.0",
34
- "@d-zero/readtext": "1.1.2",
30
+ "@d-zero/cli-core": "1.1.0",
31
+ "@d-zero/html-distiller": "1.0.3",
32
+ "@d-zero/puppeteer-dealer": "0.5.0",
33
+ "@d-zero/puppeteer-page-scan": "4.0.1",
34
+ "@d-zero/puppeteer-screenshot": "3.1.0",
35
+ "@d-zero/readtext": "1.1.4",
36
+ "@d-zero/shared": "0.9.0",
35
37
  "ansi-colors": "4.1.3",
36
38
  "dayjs": "1.11.13",
37
39
  "front-matter": "4.0.2",
38
- "minimist": "1.2.8"
40
+ "minimist": "1.2.8",
41
+ "puppeteer": "24.10.1"
39
42
  },
40
- "devDependencies": {
41
- "@d-zero/puppeteer-page": "0.3.0"
42
- },
43
- "gitHead": "e4fd17857e31022d121527b00fd7f009dbdb2142"
43
+ "gitHead": "04c6969564182c36ee38ef41e78130936dfa4863"
44
44
  }
package/dist/print.js DELETED
@@ -1,44 +0,0 @@
1
- import { mkdir } from 'node:fs/promises';
2
- import path from 'node:path';
3
- import { deal } from '@d-zero/puppeteer-dealer';
4
- import c from 'ansi-colors';
5
- import { pngToPdf } from './png-to-pdf.js';
6
- import { printPdf } from './print-pdf.js';
7
- import { printPng } from './print-png.js';
8
- /**
9
- *
10
- * @param urlList
11
- * @param options
12
- */
13
- export async function print(urlList, options) {
14
- const dir = path.resolve(process.cwd(), '.print');
15
- await mkdir(dir, { recursive: true }).catch(() => { });
16
- const type = options?.type ?? 'png';
17
- const hooks = options?.hooks;
18
- await deal(urlList.map((url) => {
19
- if (typeof url === 'string') {
20
- return { id: null, url };
21
- }
22
- return url;
23
- }), (_, done, total) => {
24
- return `${c.bold.magenta('🎨 Print pages')} ${c.bgBlueBright(` ${type} `)} ${done}/${total}`;
25
- }, {
26
- async deal(page, id, url, logger) {
27
- const ext = type === 'pdf' ? 'pdf' : 'png';
28
- const fileName = `${id}.${ext}`;
29
- const filePath = path.resolve(dir, fileName);
30
- if (type === 'pdf') {
31
- await printPdf(page, url, filePath, logger, hooks);
32
- logger('🔚 Closing');
33
- return;
34
- }
35
- const result = await printPng(page, url, id, filePath, logger, hooks);
36
- if (type === 'png') {
37
- logger('🔚 Closing');
38
- return;
39
- }
40
- await pngToPdf(page, result, logger);
41
- logger('🔚 Closing');
42
- },
43
- }, options);
44
- }
File without changes
File without changes
File without changes