From fd500a7493d52ba6f290eb04b32d0168f3ee702e Mon Sep 17 00:00:00 2001 From: XLor Date: Mon, 27 Feb 2023 00:54:33 +0800 Subject: [PATCH] feat: simplify user api --- README.md | 6 ++++++ packages/core/README.md | 31 +++++++++++++++++++++++++++---- packages/core/src/bundle/index.ts | 6 +++--- packages/core/src/epub/epub.ts | 10 ++++++---- packages/core/src/epub/opf.ts | 22 ++++++++++++++++++++-- packages/core/test/bundle.test.ts | 11 +++++------ packages/epubook/README.md | 12 ++++++++++++ packages/epubook/build.config.ts | 12 +++++------- packages/epubook/epubook.mjs | 3 +++ packages/epubook/package.json | 8 +++++++- packages/epubook/src/cli.ts | 12 ++++++++++++ packages/epubook/src/index.ts | 4 +--- pnpm-lock.yaml | 12 ++++++++++++ 13 files changed, 119 insertions(+), 30 deletions(-) create mode 100644 packages/epubook/epubook.mjs create mode 100644 packages/epubook/src/cli.ts diff --git a/README.md b/README.md index 2d51f45..04fb565 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,12 @@ It will support the generation of the [latest epub standard](https://www.w3.org/ > 👷‍♂️ Still work in progress. +## Installation + +```bash +npm i -g epubook +``` + ## Resources + [EPUB 3.2 specification](https://www.w3.org/publishing/epub32/) diff --git a/packages/core/README.md b/packages/core/README.md index fe7f83b..387a097 100644 --- a/packages/core/README.md +++ b/packages/core/README.md @@ -1,11 +1,34 @@ -# epubook +# @epubook/core -A Node EPUB generation library. - -It will support the generation of the [latest epub standard](https://www.w3.org/TR/epub-33/) (3.3). +The fundamental module of [epubook](https://github.com/yjl9903/epubook) for generating EPUB books. > 👷‍♂️ Still work in progress. +## Installation + +```bash +npm i @epubook/core +``` + +## Usage + +```ts +import { Epubook } from '@epubook/core' + +const book = new Epubook({ + title: 'Test Book', + date: new Date('2023-02-01T11:00:00.000Z'), + lastModified: new Date('2023-02-26T11:00:00.000Z'), + creator: 'XLor', + description: 'for test usage', + source: 'imagine' +}) + +// ... + +await book.writeFile('./test.epub') +``` + ## License MIT License © 2023 [XLor](https://github.com/yjl9903) diff --git a/packages/core/src/bundle/index.ts b/packages/core/src/bundle/index.ts index d95f8e7..5cb0fd1 100644 --- a/packages/core/src/bundle/index.ts +++ b/packages/core/src/bundle/index.ts @@ -17,11 +17,11 @@ import { toISO8601String } from '../utils'; export async function bundle(epub: Epubook): Promise { return new Promise(async (res, rej) => { const opfs = epub - .packageDocuments() + .packages() .map((opf) => [opf.filename(), fflate.strToU8(makePackageDocument(opf))] as const); const items: Record = {}; - for (const opf of epub.packageDocuments()) { + for (const opf of epub.packages()) { const base = path.dirname(opf.filename()); for (const item of opf.items()) { const name = path.join(base, item.filename()); @@ -77,7 +77,7 @@ export function makeContainer(epub: Epubook): string { unpairedTags: ['rootfile'] }); - const rootfile = epub.packageDocuments().map((p) => ({ + const rootfile = epub.packages().map((p) => ({ '@_full-path': p.filename(), '@_media-type': 'application/oebps-package+xml', '#text': '' diff --git a/packages/core/src/epub/epub.ts b/packages/core/src/epub/epub.ts index 36aa44a..ba07e86 100644 --- a/packages/core/src/epub/epub.ts +++ b/packages/core/src/epub/epub.ts @@ -1,7 +1,7 @@ import * as path from 'pathe'; import { existsSync, mkdirSync, promises as fs } from 'node:fs'; -import { PackageDocument } from './opf'; +import { PackageDocument, PackageDocumentMeta } from './opf'; export class Epubook { /** @@ -13,13 +13,15 @@ export class Epubook { */ private opfs: PackageDocument[] = [new PackageDocument('OEBPS/content.opf')]; - constructor() {} + constructor(meta: Partial = {}) { + this.opfs[0].update(meta); + } - public packageDocuments(): PackageDocument[] { + public packages(): PackageDocument[] { return this.opfs; } - public mainPackageDocument() { + public main() { return this.opfs[0]; } diff --git a/packages/core/src/epub/opf.ts b/packages/core/src/epub/opf.ts index df8f5b0..4cb642a 100644 --- a/packages/core/src/epub/opf.ts +++ b/packages/core/src/epub/opf.ts @@ -11,6 +11,24 @@ const defu = createDefu((obj: any, key, value: any) => { } }); +export interface PackageDocumentMeta { + title: string; + language: string; + contributor: string[]; + coverage: string; + creator: string; + date: Date; + description: string; + format: string; + publisher: string; + relation: string; + rights: string; + source: string; + subject: string; + type: string; + lastModified: Date; +} + export class PackageDocument { private readonly file: string; @@ -20,7 +38,7 @@ export class PackageDocument { private _identifier = randomUUID(); - private _metadata = { + private _metadata: PackageDocumentMeta = { title: '', language: 'zh-CN', contributor: [] as string[], @@ -57,7 +75,7 @@ export class PackageDocument { } // --- metadata --- - public update(info: Partial) { + public update(info: Partial) { // TODO: valiate input data this._metadata = defu(info, this._metadata); return this; diff --git a/packages/core/test/bundle.test.ts b/packages/core/test/bundle.test.ts index 2921705..02d9781 100644 --- a/packages/core/test/bundle.test.ts +++ b/packages/core/test/bundle.test.ts @@ -21,7 +21,7 @@ describe('Bundle Epub', () => { it('generate opf', () => { const epub = new Epubook(); - const opf = epub.mainPackageDocument(); + const opf = epub.main(); opf.setIdentifier('test-book-id', 'BookId'); opf.update({ title: 'Test Book', @@ -63,11 +63,7 @@ describe('Bundle Epub', () => { }); it('write epub', async () => { - const epub = new Epubook(); - - const opf = epub.mainPackageDocument(); - opf.setIdentifier('test-book-id', 'BookId'); - opf.update({ + const epub = new Epubook({ title: 'Test Book', date: new Date('2023-02-01T11:00:00.000Z'), lastModified: new Date('2023-02-26T11:00:00.000Z'), @@ -76,6 +72,9 @@ describe('Bundle Epub', () => { source: 'imagine' }); + const opf = epub.main(); + opf.setIdentifier('test-book-id', 'BookId'); + const content = ` Data URL does not open in top-level context diff --git a/packages/epubook/README.md b/packages/epubook/README.md index fe7f83b..04fb565 100644 --- a/packages/epubook/README.md +++ b/packages/epubook/README.md @@ -6,6 +6,18 @@ It will support the generation of the [latest epub standard](https://www.w3.org/ > 👷‍♂️ Still work in progress. +## Installation + +```bash +npm i -g epubook +``` + +## Resources + ++ [EPUB 3.2 specification](https://www.w3.org/publishing/epub32/) ++ [EPUB 3.3 specification](https://www.w3.org/TR/epub-33/) ++ [EbookLib](https://github.com/aerkalov/ebooklib) + ## License MIT License © 2023 [XLor](https://github.com/yjl9903) diff --git a/packages/epubook/build.config.ts b/packages/epubook/build.config.ts index c9b3bcb..c9c67a9 100644 --- a/packages/epubook/build.config.ts +++ b/packages/epubook/build.config.ts @@ -1,12 +1,10 @@ -import { defineBuildConfig } from 'unbuild' +import { defineBuildConfig } from 'unbuild'; export default defineBuildConfig({ - entries: [ - 'src/index', - ], + entries: ['src/index', 'src/cli.ts'], declaration: true, clean: true, rollup: { - emitCJS: true, - }, -}); \ No newline at end of file + emitCJS: true + } +}); diff --git a/packages/epubook/epubook.mjs b/packages/epubook/epubook.mjs new file mode 100644 index 0000000..8f5a50e --- /dev/null +++ b/packages/epubook/epubook.mjs @@ -0,0 +1,3 @@ +#!/usr/bin/env node + +import('./dist/cli.mjs'); diff --git a/packages/epubook/package.json b/packages/epubook/package.json index 451effa..f13026f 100644 --- a/packages/epubook/package.json +++ b/packages/epubook/package.json @@ -14,6 +14,7 @@ "license": "MIT", "author": "XLor", "sideEffects": false, + "type": "module", "exports": { ".": { "require": "./dist/index.cjs", @@ -24,7 +25,11 @@ "main": "./dist/index.cjs", "module": "./dist/index.mjs", "types": "./dist/index.d.ts", + "bin": { + "epubook": "epubook.mjs" + }, "files": [ + "*.mjs", "dist" ], "scripts": { @@ -35,7 +40,8 @@ "typecheck": "tsc --noEmit" }, "dependencies": { - "@epubook/core": "workspace:*" + "@epubook/core": "workspace:*", + "breadc": "^0.8.8" }, "devDependencies": { "vitest": "^0.29.1" diff --git a/packages/epubook/src/cli.ts b/packages/epubook/src/cli.ts new file mode 100644 index 0000000..58e59d1 --- /dev/null +++ b/packages/epubook/src/cli.ts @@ -0,0 +1,12 @@ +import { breadc } from 'breadc'; + +import { version } from '../package.json'; + +const cli = breadc('epubook', { + version, + description: 'Generate EPUB books' +}); + +cli.command('').action(() => {}); + +cli.run(process.argv.slice(2)).catch((err) => console.error(err)); diff --git a/packages/epubook/src/index.ts b/packages/epubook/src/index.ts index 5cb4f54..6e3f79a 100644 --- a/packages/epubook/src/index.ts +++ b/packages/epubook/src/index.ts @@ -1,3 +1 @@ -export function hello() { - return 'world'; -} \ No newline at end of file +export * from '@epubook/core'; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 2fce9f5..e450764 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -38,9 +38,11 @@ importers: packages/epubook: specifiers: '@epubook/core': workspace:* + breadc: ^0.8.8 vitest: ^0.29.1 dependencies: '@epubook/core': link:../core + breadc: 0.8.8 devDependencies: vitest: 0.29.1 @@ -252,6 +254,10 @@ packages: to-fast-properties: 2.0.0 dev: true + /@breadc/color/0.8.8: + resolution: {integrity: sha512-a3MT+0zpmAHkQACN4NyHSlmVehKt0bo5GiM6Sxe428+ZznUysbTxQHsXfOUoktpvJ8OMVoMUwCxnjf1gKn/0+A==} + dev: false + /@esbuild/android-arm/0.16.17: resolution: {integrity: sha512-N9x1CMXVhtWEAMS7pNNONyA14f71VPQN9Cnavj1XQh6T7bskqiLLrSca4O0Vr8Wdcga943eThxnVp3JLnBMYtw==} engines: {node: '>=12'} @@ -913,6 +919,12 @@ packages: fill-range: 7.0.1 dev: true + /breadc/0.8.8: + resolution: {integrity: sha512-UeaCfZcTye2WLEO38FPXYjYiNQNG5eeDlLvyJiKX4wwMx/J00FD/Su5f5KQT3m8eOSd1PeZA4V5ZOTaGNeOZtA==} + dependencies: + '@breadc/color': 0.8.8 + dev: false + /browserslist/4.21.4: resolution: {integrity: sha512-CBHJJdDmgjl3daYjN5Cp5kbTf1mUhZoS+beLklHIvkOWscs83YAhLlF3Wsh/lciQYAcbBJgTOD44VtG31ZM4Hw==} engines: {node: ^6 || ^7 || ^8 || ^9 || ^10 || ^11 || ^12 || >=13.7}