Commit a8cfed21 authored by Guillaume Perréal's avatar Guillaume Perréal
Browse files

Met en place une exécution parallèle pour les filtres drawio, pandoc et wkhtmltopdf.

parent 0010bba8
import { callbackify } from "util";
import exec from "../util/exec"; import exec from "../util/exec";
import fs from "fs"; import fs from "fs";
import logger from "gulplog"; import logger from "gulplog";
import { mkTempFile } from "../util/tempdir"; import { mkTempFile } from "../util/tempdir";
import { obj } from "through2"; import parallel from "../util/parallel";
import PluginError from "plugin-error"; import PluginError from "plugin-error";
import { withBinary } from "../util/optional"; import { withBinary } from "../util/optional";
...@@ -14,41 +13,39 @@ const drawio = withBinary( ...@@ -14,41 +13,39 @@ const drawio = withBinary(
"drawio", "drawio",
(DRAWIO_BINARY) => (DRAWIO_BINARY) =>
function drawio() { function drawio() {
return obj( return parallel(async function (input) {
callbackify(async function (input) { try {
try { const output = await mkTempFile(input);
const output = await mkTempFile(input); output.extname = ".svg";
output.extname = ".svg";
const args = [ const args = [
"--export", "--export",
"--format", "--format",
"svg", "svg",
"--width", "--width",
"1024", "1024",
"--output", "--output",
output.path, output.path,
input.path, input.path,
]; ];
await exec(PLUGIN_NAME, DRAWIO_BINARY, args); await exec(PLUGIN_NAME, DRAWIO_BINARY, args);
logger.info( logger.info(
"%s: %s -> %s", "%s: %s -> %s",
PLUGIN_NAME, PLUGIN_NAME,
input.relative, input.relative,
output.relative output.relative
); );
output.contents = fs.createReadStream(output.path, { output.contents = fs.createReadStream(output.path, {
encoding: "UTF-8", encoding: "UTF-8",
}); });
this.push(output); this.push(output);
} catch (error) { } catch (error) {
throw new PluginError(PLUGIN_NAME, error, { showStack: true }); throw new PluginError(PLUGIN_NAME, error, { showStack: true });
} }
}) });
);
} }
); );
......
import { callbackify, promisify } from "util";
import exec from "../util/exec"; import exec from "../util/exec";
import fs from "fs"; import fs from "fs";
import { merge } from "merge-anything"; import { merge } from "merge-anything";
import { mkTempFile } from "../util/tempdir"; import { mkTempFile } from "../util/tempdir";
import { obj } from "through2"; import parallel from "../util/parallel";
import path from "path"; import path from "path";
import PluginError from "plugin-error"; import PluginError from "plugin-error";
import { promisify } from "util";
const writeFile = promisify(fs.writeFile); const writeFile = promisify(fs.writeFile);
const exists = promisify(fs.exists); const exists = promisify(fs.exists);
...@@ -87,41 +87,39 @@ export default function pandoc(options = {}) { ...@@ -87,41 +87,39 @@ export default function pandoc(options = {}) {
); );
const revealJSURLResolver = getRevealJSResolver(revealJSURL); const revealJSURLResolver = getRevealJSResolver(revealJSURL);
return obj( return parallel(
callbackify( /**
/** * @param {import("vinyl")} input
* @param {import("vinyl")} input */
*/ async function (input) {
async function (input) { const output = input.clone({ contents: false });
const output = input.clone({ contents: false }); output.extname = ".html";
output.extname = ".html"; output.contents = null;
output.contents = null;
const src = await onDisk(input); const src = await onDisk(input);
const args = [ const args = [
"--from=markdown+backtick_code_blocks+pandoc_title_block+yaml_metadata_block", "--from=markdown+backtick_code_blocks+pandoc_title_block+yaml_metadata_block",
"--to=revealjs", "--to=revealjs",
"--standalone", "--standalone",
`--template=${template}`, `--template=${template}`,
`--slide-level=${slideLevel}`, `--slide-level=${slideLevel}`,
"--toc", "--toc",
`--toc-depth=${tocDepth}`, `--toc-depth=${tocDepth}`,
`--variable=revealjs-url:${revealJSURLResolver(output)}`, `--variable=revealjs-url:${revealJSURLResolver(output)}`,
...Object.getOwnPropertyNames(variables).map( ...Object.getOwnPropertyNames(variables).map(
(name) => `--variable=${name}:${variables[name]}` (name) => `--variable=${name}:${variables[name]}`
), ),
src.path, src.path,
]; ];
const { stdout } = await exec(PLUGIN_NAME, "pandoc", args, { const { stdout } = await exec(PLUGIN_NAME, "pandoc", args, {
encoding: "buffer", encoding: "buffer",
cwd: input.cwd, cwd: input.cwd,
}); });
output.contents = stdout; output.contents = stdout;
this.push(output); this.push(output);
} }
)
); );
} }
import { callbackify } from "util";
import { createReadStream } from "fs"; import { createReadStream } from "fs";
import exec from "../util/exec"; import exec from "../util/exec";
import { mkTempFile } from "../util/tempdir"; import { mkTempFile } from "../util/tempdir";
import { obj } from "through2"; import parallel from "../util/parallel";
import path from "path"; import path from "path";
import PluginError from "plugin-error"; import PluginError from "plugin-error";
import { withBinary } from "../util/optional"; import { withBinary } from "../util/optional";
...@@ -46,8 +45,7 @@ export const wkhtmltopdf = withBinary( ...@@ -46,8 +45,7 @@ export const wkhtmltopdf = withBinary(
(WKHTMLTOPDF_BINARY) => (WKHTMLTOPDF_BINARY) =>
function wkhtmltopdf(options = {}) { function wkhtmltopdf(options = {}) {
const { args } = Object.assign({}, DEFAULT_OPTIONS, options); const { args } = Object.assign({}, DEFAULT_OPTIONS, options);
return obj( return parallel(async function (input) {
callbackify(async function (input) {
try { try {
const output = await mkTempFile(getPDFOutput(input)); const output = await mkTempFile(getPDFOutput(input));
const execArgs = [ const execArgs = [
...@@ -64,7 +62,7 @@ export const wkhtmltopdf = withBinary( ...@@ -64,7 +62,7 @@ export const wkhtmltopdf = withBinary(
throw new PluginError(PLUGIN_NAME, err, { filename: input.path }); throw new PluginError(PLUGIN_NAME, err, { filename: input.path });
} }
}) })
); ;
} }
); );
......
import { cpus } from "os";
import { obj } from "through2";
import pMap from "p-map";
const concurrency = cpus().length;
/**
*
* @param {(file) => Promise<any>} mapper
* @return {import('stream').Transform}
*/
export default function parallel(mapper) {
const inputs = [];
return obj(
function collect(input, _enc, done) {
inputs.push(input);
done();
},
function mapAll(done) {
pMap(inputs, mapper.bind(this), { concurrency, stopOnError: false }).then(
() => done(),
error => done(error)
);
}
);
}
...@@ -2916,6 +2916,16 @@ ...@@ -2916,6 +2916,16 @@
"p-map": "^3.0.0", "p-map": "^3.0.0",
"rimraf": "^3.0.0", "rimraf": "^3.0.0",
"slash": "^3.0.0" "slash": "^3.0.0"
},
"dependencies": {
"p-map": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/p-map/-/p-map-3.0.0.tgz",
"integrity": "sha512-d3qXVTF/s+W+CdJ5A29wywV2n8CQQYahlgz2bFiA+4eVNJbHJodPZ+/gXwPGh0bOqA+j8S+6+ckmvLGPk1QpxQ==",
"requires": {
"aggregate-error": "^3.0.0"
}
}
} }
}, },
"delayed-stream": { "delayed-stream": {
...@@ -8308,9 +8318,9 @@ ...@@ -8308,9 +8318,9 @@
} }
}, },
"p-map": { "p-map": {
"version": "3.0.0", "version": "4.0.0",
"resolved": "https://registry.npmjs.org/p-map/-/p-map-3.0.0.tgz", "resolved": "https://registry.npmjs.org/p-map/-/p-map-4.0.0.tgz",
"integrity": "sha512-d3qXVTF/s+W+CdJ5A29wywV2n8CQQYahlgz2bFiA+4eVNJbHJodPZ+/gXwPGh0bOqA+j8S+6+ckmvLGPk1QpxQ==", "integrity": "sha512-/bjOqmgETBYB5BoEeGVea8dmvHb2m9GLy1E9W43yeyfP6QQCZGFNa+XRceJEuDB6zqr+gKpIAmlLebMpykw/MQ==",
"requires": { "requires": {
"aggregate-error": "^3.0.0" "aggregate-error": "^3.0.0"
} }
......
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