npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

node-calibre

v2.1.1

Published

A wrapper for Calibre's command line tools.

Downloads

88

Readme

A simple Node wrapper for Calibre's command line tools.

Built and maintained by the Xyfir Network.

node-calibre is mostly a simple wrapper around Calibre's CLI using Node's child_process.exec(), without many extra features added. In the future this package will contain more methods unique to each of Calibre's binaries and with both better error checking and improved results provided on success.

Examples

import { Calibre } from 'node-calibre';

// Create Calibre instance
const calibre = new Calibre({ library: '/path/to/calibre/library' });

// Convert ebook from pdf to epub
const newFile = await calibre.ebookConvert('/path/to/book.pdf', 'epub', {
  epubFlatten: null
});
console.log(newFile); // "/path/to/book.pdf.epub"

let result: string;

// Add book to Calibre library
result = await calibre.run('calibredb add', ['/path/to/book.epub']);
console.log(result); // "Added book ids: ..."

// List books in Calibre Library
result = await calibre.run('calibredb list', [], { limit: 10 });
console.log(result); // first 10 books

// You can optionally pass `options` as the second parameter
// `forMachine: null` gets converted to `--for-machine`
result = await calibre.run('calibredb list', {
  limit: 10,
  forMachine: null
});
console.log(Array.isArray(JSON.parse(result))); // true

API

constructor(options)

  • options.library: string - optional, default ''
    • Full path to the Calibre library to work with.
    • Only needed if the commands this instance will run will use any of the calibredb commands.
    • The path will be used as the value for the --library-path option for calibredb commands.
  • options.log: boolean - optional, default false
    • If true, the command string that is run by Node's child_process.exec() is logged to console before running.
  • options.execOptions: object - optional, default {maxBuffer: 2048000}
    • The object passed to Node's child_process.exec() as the options argument.

run(command[, args][, options])

Runs a command on one of Calibre's binaries (calibredb, ebook-convert, etc). Find all here.

  • command: string
    • The name of the bin and command to run. For example 'calibredb add' or 'ebook-convert'.
  • args: any[] - optional, default []
    • An array of arguments that the command will accept.
    • All arguments are converted to strings, wrapped in "", and escaped.
  • options: object - optional, default {}
    • A key:value object containing options that the command will accept.
    • All values (but not keys) are wrapped in "" and escaped.
    • If you want to pass on option that doesn't take a value, set the value to null: {'some-option': null}.
    • You can also use camelCase keys and they'll be converted to kebab-case: {forMachine: null} -> --for-machine.
    • If an option can be used multiple times (like --field in calibredb set_metadata), you can pass an array with all of the values: {field: ['tags:tag1,tag2', 'title:Some New Title']} -> --field "tags:tag1,tag2" --field "title:Some New Title".

Return

A promise that is rejected if the callback of Node's child_process.exec() has a value for error or stderr and resolves to the callback's stdout if no error occurred. Due to how Calibre's command line tools work, most of the time the promise should resolve regardless of whether Calibre encountered an issue. It's up to you to check the resolved result to determine if the command was successful.

ebookConvert(input, format, options)

Wrapper for ebook-convert.

  • input: string
    • Path to the input file to convert.
  • format: string
    • The format (file extension) to convert input to.
  • options: object
    • Any CLI options for the ebook-convert command.

Return

Full path to the new file.

exec(command[, options])

This method should only be used if for some reason you need to build your own command string. It's essentially just a Promise-wrapped child_process.exec() that gets passed the execOptions from the constructor.

  • command: string
    • The full command that you want to run.
    • For example: calibredb list --for-machine --limit 10.
  • options: object
    • Will be merged with the execOptions object that was passed to the constructor.
    • Properties passed will override properties with the same name from execOptions.

Return

Same as run(), which builds a command string and passes it to exec().

Notes

  • You should be aware of the maxBuffer property of the options object accepted by Node's child_process.exec(). It limits the size of output that can be received from a process, in this case one of Calibre's binaries. Unless you set maxBuffer as a property in the execOptions object, the maximum buffer size will be increased from the default of 200KB to 2MB. The vast majority of commands will get nowhere near this number, however certain commands like calibredb list with all fields requested on a large library can get close or even surpass that limit in certain cases. If a command's output exceeds the maxBuffer limit, an error will be thrown.
  • You can safely pass use input as a value in the args array and options object of run(), but not as a command or an option name. In other words, args and option values are are wrapped in double quotes and escaped.
  • This package does not install Calibre. You must have Calibre already installed and either have Calibre's bin directory in your system's PATH or use the cwd property of execOptions (see constructor options) to set Calibre's bin directory.
  • Calibre v3 is recommended, but lower versions will most likely work.