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 fs from "fs";
import logger from "gulplog";
import { mkTempFile } from "../util/tempdir";
import { obj } from "through2";
import parallel from "../util/parallel";
import PluginError from "plugin-error";
import { withBinary } from "../util/optional";
......@@ -14,41 +13,39 @@ const drawio = withBinary(
"drawio",
(DRAWIO_BINARY) =>
function drawio() {
return obj(
callbackify(async function (input) {
try {
const output = await mkTempFile(input);
output.extname = ".svg";
return parallel(async function (input) {
try {
const output = await mkTempFile(input);
output.extname = ".svg";
const args = [
"--export",
"--format",
"svg",
"--width",
"1024",
"--output",
output.path,
input.path,
];
const args = [
"--export",
"--format",
"svg",
"--width",
"1024",
"--output",
output.path,
input.path,
];
await exec(PLUGIN_NAME, DRAWIO_BINARY, args);
await exec(PLUGIN_NAME, DRAWIO_BINARY, args);
logger.info(
"%s: %s -> %s",
PLUGIN_NAME,
input.relative,
output.relative
);
logger.info(
"%s: %s -> %s",
PLUGIN_NAME,
input.relative,
output.relative
);
output.contents = fs.createReadStream(output.path, {
encoding: "UTF-8",
});
this.push(output);
} catch (error) {
throw new PluginError(PLUGIN_NAME, error, { showStack: true });
}
})
);
output.contents = fs.createReadStream(output.path, {
encoding: "UTF-8",
});
this.push(output);
} catch (error) {
throw new PluginError(PLUGIN_NAME, error, { showStack: true });
}
});
}
);
......
import { callbackify, promisify } from "util";
import exec from "../util/exec";
import fs from "fs";
import { merge } from "merge-anything";
import { mkTempFile } from "../util/tempdir";
import { obj } from "through2";
import parallel from "../util/parallel";
import path from "path";
import PluginError from "plugin-error";
import { promisify } from "util";
const writeFile = promisify(fs.writeFile);
const exists = promisify(fs.exists);
......@@ -87,41 +87,39 @@ export default function pandoc(options = {}) {
);
const revealJSURLResolver = getRevealJSResolver(revealJSURL);
return obj(
callbackify(
/**
* @param {import("vinyl")} input
*/
async function (input) {
const output = input.clone({ contents: false });
output.extname = ".html";
output.contents = null;
return parallel(
/**
* @param {import("vinyl")} input
*/
async function (input) {
const output = input.clone({ contents: false });
output.extname = ".html";
output.contents = null;
const src = await onDisk(input);
const src = await onDisk(input);
const args = [
"--from=markdown+backtick_code_blocks+pandoc_title_block+yaml_metadata_block",
"--to=revealjs",
"--standalone",
`--template=${template}`,
`--slide-level=${slideLevel}`,
"--toc",
`--toc-depth=${tocDepth}`,
`--variable=revealjs-url:${revealJSURLResolver(output)}`,
...Object.getOwnPropertyNames(variables).map(
(name) => `--variable=${name}:${variables[name]}`
),
src.path,
];
const args = [
"--from=markdown+backtick_code_blocks+pandoc_title_block+yaml_metadata_block",
"--to=revealjs",
"--standalone",
`--template=${template}`,
`--slide-level=${slideLevel}`,
"--toc",
`--toc-depth=${tocDepth}`,
`--variable=revealjs-url:${revealJSURLResolver(output)}`,
...Object.getOwnPropertyNames(variables).map(
(name) => `--variable=${name}:${variables[name]}`
),
src.path,
];
const { stdout } = await exec(PLUGIN_NAME, "pandoc", args, {
encoding: "buffer",
cwd: input.cwd,
});
output.contents = stdout;
const { stdout } = await exec(PLUGIN_NAME, "pandoc", args, {
encoding: "buffer",
cwd: input.cwd,
});
output.contents = stdout;
this.push(output);
}
)
this.push(output);
}
);
}
import { callbackify } from "util";
import { createReadStream } from "fs";
import exec from "../util/exec";
import { mkTempFile } from "../util/tempdir";
import { obj } from "through2";
import parallel from "../util/parallel";
import path from "path";
import PluginError from "plugin-error";
import { withBinary } from "../util/optional";
......@@ -46,8 +45,7 @@ export const wkhtmltopdf = withBinary(
(WKHTMLTOPDF_BINARY) =>
function wkhtmltopdf(options = {}) {
const { args } = Object.assign({}, DEFAULT_OPTIONS, options);
return obj(
callbackify(async function (input) {
return parallel(async function (input) {
try {
const output = await mkTempFile(getPDFOutput(input));
const execArgs = [
......@@ -64,7 +62,7 @@ export const wkhtmltopdf = withBinary(
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 @@
"p-map": "^3.0.0",
"rimraf": "^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": {
......@@ -8308,9 +8318,9 @@
}
},
"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==",
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/p-map/-/p-map-4.0.0.tgz",
"integrity": "sha512-/bjOqmgETBYB5BoEeGVea8dmvHb2m9GLy1E9W43yeyfP6QQCZGFNa+XRceJEuDB6zqr+gKpIAmlLebMpykw/MQ==",
"requires": {
"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