diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 9345fe53d5b62444a215f76befd19f2c0af8797c..19306eb340e77f1f07dbf68900f8b20740d953be 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -3,6 +3,11 @@ # https://hub.docker.com/r/library/node/tags/ image: node:latest +stages: + - test + - deploy + - publish + # This folder is cached between builds # http://docs.gitlab.com/ce/ci/yaml/README.html#cache cache: @@ -22,4 +27,20 @@ pack: - npm pack artifacts: paths: - - ./*.tgz \ No newline at end of file + - ./*.tgz + +# This job requires to setup GitLab the following way: +# 1. On https://www.npmjs.com/settings/tokens/create +# create a new read/write token (the logged in user must have write access for the `pagedjs` package) +# 2. On https://gitlab.pagedmedia.org/tools/pagedjs/-/settings/ci_cd#js-cicd-variables-settings +# add a new variable named `NPM_TOKEN`, and toggle on _Protected_ and _Masked_ +npm-publish: + stage: publish + before_script: + - 'echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" > ~/.npmrc' + - npm install + - npm publish --public --dry-run + script: + - npm publish --public + only: + - tags \ No newline at end of file diff --git a/bin/paged b/bin/paged index 55858b2fd938b557bd1fa708c259cc40d1d5ac5d..80dfc31ced2cce3a382d82ed1f6bf41b72f05883 100755 --- a/bin/paged +++ b/bin/paged @@ -51,7 +51,9 @@ function collect(value, previous) { return previous.concat(value); } -let input = program.inputs || program.args[0]; +const options = program.opts(); + +let input = options.inputs || program.args[0]; let dir = process.cwd(); @@ -62,12 +64,12 @@ try { allowLocal = false; } catch (error) { relativePath = path.resolve(dir, input); - allowLocal = !program.blockLocal; + allowLocal = !options.blockLocal; } let output; -let headless = typeof program.debug === "undefined"; +let headless = typeof options.debug === "undefined"; // var hyphenator; // var hyphenateOptions; @@ -92,9 +94,9 @@ if (relativePath) { } } -if (typeof(program.output) === "string") { - output = path.resolve(dir, program.output); -} else if (typeof(program.output) !== "undefined") { +if (typeof(options.output) === "string") { + output = path.resolve(dir, options.output); +} else if (typeof(options.output) !== "undefined") { output = "./" + replaceExt(path.basename(input), ".pdf"); } else { output = "output.pdf"; @@ -116,16 +118,16 @@ if (typeof input === "string") { const printerOptions = { headless: headless, allowLocal: allowLocal, - allowRemote: !program.blockRemote, - allowedPaths: program.allowedPaths, - allowedDomains: program.allowedDomains, - additionalScripts: program.additionalScript, - browserEndpoint: program.browserEndpoint, - timeout: program.timeout, - browserArgs: program.browserArgs + allowRemote: !options.blockRemote, + allowedPaths: options.allowedPaths, + allowedDomains: options.allowedDomains, + additionalScripts: options.additionalScript, + browserEndpoint: options.browserEndpoint, + timeout: options.timeout, + browserArgs: options.browserArgs }; - if (program.forceTransparentBackground) { + if (options.forceTransparentBackground) { printerOptions.overrideDefaultBackgroundColor = { r: 0, g: 0, b: 0, a: 0 }; // Workaround to get a transparent background in the resulting PDF. See https://bugs.chromium.org/p/chromium/issues/detail?id=498892 for more information. } @@ -154,12 +156,20 @@ if (typeof input === "string") { let file; if (headless) { let options = {}; - if (program.html) { - file = await printer.html(input, options); + if (options.html) { + file = await printer.html(input, options) + .catch((e) => { + console.error(e); + process.exit(1); + }); output = replaceExt(output, ".html"); } else { - options.outlineTags = !program.outlineTags ? [] : program.outlineTags.split(","); - file = await printer.pdf(input, options); + options.outlineTags = !options.outlineTags ? [] : options.outlineTags.split(","); + file = await printer.pdf(input, options) + .catch((e) => { + console.error(e); + process.exit(1); + }); } } else { printer.preview(input); diff --git a/src/printer.js b/src/printer.js index 08e425eb043c179563acce83bc82abed7fba2ea3..9ca3d378cefc946a434dc63e1adecb9f6dc6270a 100644 --- a/src/printer.js +++ b/src/printer.js @@ -127,10 +127,7 @@ class Printer extends EventEmitter { }); if (html) { - await page.setContent(html) - .catch((e) => { - console.error(e); - }); + await page.setContent(html); if (url) { await page.evaluate((url) => { @@ -144,10 +141,7 @@ class Printer extends EventEmitter { } } else { - await page.goto(url) - .catch((e) => { - console.error(e); - }); + await page.goto(url); } await page.evaluate(() => { @@ -189,7 +183,7 @@ class Printer extends EventEmitter { }); await page.evaluate(async () => { - let done; + let done; window.PagedPolyfill.on("page", (page) => { const { id, width, height, startToken, endToken, breakAfter, breakBefore, position } = page; @@ -236,6 +230,8 @@ class Printer extends EventEmitter { if (window.PagedConfig.after) { await window.PagedConfig.after(done); } + }).catch((error) => { + throw error; }); await rendered; @@ -294,7 +290,10 @@ class Printer extends EventEmitter { } async pdf(input, options={}) { - let page = await this.render(input); + let page = await this.render(input) + .catch((e) => { + throw e; + }); // Get metatags const meta = await page.evaluate(() => { @@ -335,7 +334,7 @@ class Printer extends EventEmitter { let pdf = await page.pdf(settings) .catch((e) => { - console.error(e); + throw e; }); await page.close(); @@ -356,10 +355,7 @@ class Printer extends EventEmitter { async html(input, stayopen) { let page = await this.render(input); - let content = await page.content() - .catch((e) => { - console.error(e); - }); + let content = await page.content(); await page.close(); return content;