paged 3.36 KB
Newer Older
Fred Chasen's avatar
Fred Chasen committed
1 2
#!/usr/bin/env node
const program = require('commander');
Fred Chasen's avatar
Fred Chasen committed
3
const Printer = require("../");
Fred Chasen's avatar
Fred Chasen committed
4 5
const path = require('path');
const fs = require('fs');
Fred Chasen's avatar
Fred Chasen committed
6 7
const { promisify } = require('util');
const writeFileAsync = promisify(fs.writeFile);
Fred Chasen's avatar
Fred Chasen committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
const replaceExt = require('replace-ext');

program
  .version(require('../package.json').version)
  .arguments('[inputPath]')
  .option('-i, --inputs [inputs]', 'Inputs')
  .option('-o, --output [output]', 'Output')
  .option('-d, --debug', 'Debug')
  .option('-l, --landscape', 'Landscape printing', false)
  .option('-s, --page-size [size]', 'Print to Page Size [size]')
  .option('-w, --width [size]', 'Print to Page Width [width] in MM')
  .option('-h --height [size]', 'Print to Page Height [weight] in MM')
  // .option('-m, --page-margin [margin]', 'Print with margin [margin]')
  // .option('-n, --hyphenate [lang]', 'Hyphenate with language [language], defaults to "en-us"')
  // .option('-hi, --hypher_ignore [str]', 'Ignore passed element selectors, such as ".class_to_ignore, h1"')
  // .option('-ho, --hypher_only [str]', 'Only hyphenate passed elements selector, such as ".hyphenate, aside"')
  // .option('-e, --encoding [type]', 'Set the encoding of the input html, defaults to "utf-8"')
  .option('-t, --timeout [ms]', 'Set a max timeout of [ms]')
Fred Chasen's avatar
Fred Chasen committed
26
  .option('-x, --html', 'output html file')
Fred Chasen's avatar
Fred Chasen committed
27 28 29 30 31 32 33 34 35 36
  .parse(process.argv);


let input = program.inputs || program.args[0];

let dir = process.cwd();
let relativePath = path.resolve(dir, input);
let output;
let tmpFile, tmpPath;

Fred Chasen's avatar
Fred Chasen committed
37 38
let headless = typeof program.debug === "undefined";

Fred Chasen's avatar
Fred Chasen committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
// var hyphenator;
// var hyphenateOptions;

if (!input) {
  console.error("You must include an input path");
  return process.exit(1);
}

if (['.html', '.xhtml'].indexOf(path.extname(relativePath)) === -1) {
  console.error("Must pass a html or xhtml file as input");
  return process.exit(1);
}

try {
    fs.accessSync(relativePath, fs.F_OK);
} catch (e) {
    console.error("Input cannot be found", e);
    return process.exit(1);
}

if (typeof(program.output) === "string") {
  output = path.resolve(dir, program.output);
} else if (typeof(program.output) !== "undefined") {
  output = './' + replaceExt(path.basename(input), '.pdf');
} else {
  output = "output.pdf";
}

/*
if (program.hyphenate) {
  hyphenateOptions = {
    ignore: program.hypher_ignore || undefined,
    only: program.hypher_only || undefined,
    encoding: program.encoding || undefined
  }

  tmpPath = replaceExt(relativePath, ".hyphenated.html");

  // tmpFile = temp.openSync({suffix: '.html'});
  // tmpPath = tmpFile.path;
  // Create a new Hyphenator, with passed language
  hyphenator = new Hyphenator(program.hyphenate);
  hyphenator.process(relativePath, tmpPath, hyphenateOptions);

  console.log("Hyphenated for", typeof(program.hyphenate) === "string" ? program.hyphenate : "en-us");

  if (program.debug && tmpPath) {
    console.log("Hyphenated file at:", tmpPath);
  }

}
*/


(async () => {
Fred Chasen's avatar
Fred Chasen committed
94 95 96 97 98 99 100 101 102 103 104 105 106 107
  let printer = new Printer(headless);

  let file;
  if (headless) {
    let options = {};
    if (program.html) {
      file = await printer.html(input, options);
      output = replaceExt(output, '.html');
    } else {
      file = await printer.pdf(input, options);
    }
  } else {
    printer.preview(input);
  }
Fred Chasen's avatar
Fred Chasen committed
108

Fred Chasen's avatar
Fred Chasen committed
109 110 111 112 113 114 115
  if (file) {
    fs.writeFile(output, file, (err) => {
      if (err) throw err;
      console.log('Saved to', output);
      process.exit(0);
    });
  }
Fred Chasen's avatar
Fred Chasen committed
116
})();