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

Améliore la gestion et l'affichage des appels aux programmes externes.

parent e17ad22e
import { callbackify, promisify } from "util";
import child_process from "child_process";
import { callbackify } from "util";
import exec from "./exec";
import fs from "fs";
import logger from "gulplog";
import { mkTempFile } from "./tempdir";
......@@ -9,8 +9,6 @@ import { withBinary } from "./optional";
const PLUGIN_NAME = "drawio";
const execFile = promisify(child_process.execFile);
const drawio = withBinary(
"DRAWIO_BINARY",
"drawio",
......@@ -33,23 +31,21 @@ const drawio = withBinary(
input.path,
];
const { stdout, stderr } = await execFile(DRAWIO_BINARY, args);
if (stdout.length > 0) {
logger.info(PLUGIN_NAME, "stdout:", stdout);
}
if (stderr.length > 0) {
logger.info(PLUGIN_NAME, "command:", DRAWIO_BINARY, args);
logger.info(PLUGIN_NAME, "stderr:", stderr);
}
await exec(PLUGIN_NAME, DRAWIO_BINARY, args);
logger.info(
"%s: %s -> %s",
PLUGIN_NAME,
input.relative,
output.relative
);
output.contents = fs.createReadStream(output.path, {
encoding: "UTF-8",
});
logger.info("%s: generated %s", PLUGIN_NAME, output.relative);
this.push(output);
} catch (error) {
throw new PluginError(PLUGIN_NAME, error);
throw new PluginError(PLUGIN_NAME, error, { showStack: true });
}
})
);
......
import { execFile } from "child_process";
import logger from "gulplog";
/**
*
* @param {string} plugin_name
* @param {string} cmd
* @param {string[]} args
* @param {import("child_process").ExecFileOptions} opts
* @return {Promise<{ stdout: string|Buffer, stderr: string|Buffer }>}
*/
export default function exec(plugin_name, cmd, args, opts = {}) {
return new Promise((resolve, reject) => {
execFile(cmd, args, opts, (error, stdout, stderr) => {
if (!error) {
logger.debug("%s: %s terminated successfully", plugin_name, cmd);
return resolve({ stdout, stderr });
}
logger.error("%s: %s failed: %s", plugin_name, cmd, error.message);
logger.error("%s: command: `%s %s`", plugin_name, cmd, args.join(" "));
logger.error("%s: stdout:\n%s", plugin_name, stdout.toString());
logger.error("%s: stderr:\n%s", plugin_name, stderr.toString());
reject(error);
});
});
}
import { callbackify, promisify } from "util";
import child_process from "child_process";
import { callbackify } from "util";
import { createReadStream } from "fs";
import exec from "./exec";
import logger from "gulplog";
import { mkTempFile } from "./tempdir";
import { obj } from "through2";
......@@ -8,8 +8,6 @@ import path from "path";
import PluginError from "plugin-error";
import { withBinary } from "./optional";
const execFile = promisify(child_process.execFile);
const PLUGIN_NAME = "wkhtmltopdf";
const DEFAULT_OPTIONS = {
......@@ -59,17 +57,13 @@ export const wkhtmltopdf = withBinary(
output.path,
];
const { stdout, stderr } = await execFile(
WKHTMLTOPDF_BINARY,
execArgs
await exec(PLUGIN_NAME, WKHTMLTOPDF_BINARY, execArgs);
logger.info(
"%s: %s -> %s",
PLUGIN_NAME,
input.relative,
output.relative
);
logger.info("%s: generated %s", PLUGIN_NAME, output.relative);
if (stdout.length > 0) {
logger.debug(PLUGIN_NAME, "stdout:", stdout);
}
if (stderr.length > 0) {
logger.debug(PLUGIN_NAME, "stderr:", stderr);
}
output.contents = createReadStream(output.path);
this.push(output);
......
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