Commit a11c60bf authored by Fred Chasen's avatar Fred Chasen
Browse files

Fix options, exit on error, ci publish

parent c3e83c8e
Pipeline #862 failed
......@@ -22,4 +22,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
......@@ -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);
......
......@@ -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;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment