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

Utilise de l'ESM et applique un coding style.

parent fe6e04b0
const { src, dest, parallel, series, watch } = require("gulp");
const server = require("gulp-server-livereload");
const del = require("del");
const pandoc = require("./lib/pandoc");
const drawio = require("./lib/drawio");
const index = require("./lib/index");
import { dest, parallel, series, src, watch } from "gulp";
import del from "del";
import drawio from "./lib/drawio";
import index from "./lib/index";
import pandoc from "./lib/pandoc";
import server from "gulp-server-livereload";
const SRC_DIR = process.env.SRC || "src";
const DEST_DIR = process.env.OUTPUT || "public";
......@@ -16,19 +15,14 @@ const ASSET_GLOB = [
const PREZ_GLOB = `${SRC_DIR}/**/index.md`;
const GRAPH_GLOB = `${SRC_DIR}/**/*.drawio`;
const clean = () => del(`${DEST_DIR}/**`);
exports.clean = clean;
export const clean = () => del(`${DEST_DIR}/**`);
const assets = () => src(ASSET_GLOB).pipe(dest(DEST_DIR));
const graphs = () => src(GRAPH_GLOB).pipe(drawio()).pipe(dest(DEST_DIR));
const prez = () =>
export const assets = () => src(ASSET_GLOB).pipe(dest(DEST_DIR));
export const graphs = () => src(GRAPH_GLOB).pipe(drawio()).pipe(dest(DEST_DIR));
export const prez = () =>
src(PREZ_GLOB).pipe(index()).pipe(pandoc()).pipe(dest(DEST_DIR));
exports.graphs = graphs;
exports.assets = assets;
exports.prez = prez;
exports.default = exports.build = series(clean, parallel(assets, prez, graphs));
export const build = series(clean, parallel(assets, prez, graphs));
const serve = () => {
src(DEST_DIR).pipe(
......@@ -44,7 +38,9 @@ const watch_prez = () => watch(PREZ_GLOB, { ignoreInitial: false }, prez);
const watch_assets = () => watch(ASSET_GLOB, { ignoreInitial: false }, assets);
const watch_graphs = () => watch(GRAPH_GLOB, { ignoreInitial: false }, graphs);
exports.dev = series(
export const dev = series(
clean,
parallel(serve, watch_prez, watch_assets, watch_graphs)
);
export default build;
const { obj } = require("through2");
const { promisify, callbackify } = require("util");
const execFile = promisify(require("child_process").execFile);
const fs = require("fs");
const os = require("os");
const PluginError = require("plugin-error");
const File = require("vinyl");
import { callbackify, promisify } from "util";
import child_process from "child_process";
import File from "vinyl";
import fs from "fs";
import logger from "gulplog";
import { obj } from "through2";
import os from "os";
import PluginError from "plugin-error";
const PLUGIN_NAME = "drawio";
const execFile = promisify(child_process.execFile);
const mkdir = promisify(fs.mkdir);
const { mkdtempSync } = fs;
module.exports = (options = {}) => {
export default function (options = {}) {
const verbose = options.verbose || false;
const tmpDir = mkdtempSync(`${os.tmpdir()}/drawio-`) + "/";
const tmpDir = fs.mkdtempSync(`${os.tmpdir()}/drawio-`) + "/";
return obj(
callbackify(async function (input, _enc) {
callbackify(async function (input) {
try {
const output = new File({
path: tmpDir + input.relative,
......@@ -36,14 +40,12 @@ module.exports = (options = {}) => {
];
await mkdir(output.dirname, { recursive: true, mode: 0o700 });
if (verbose) {
console.log(input.path, "=>", output.path);
}
logger.debug(PLUGIN_NAME, input.path, "=>", output.path);
const { stdout, stderr } = await execFile("drawio", args);
if (verbose) {
options.log("Done:", input.path);
console.log(stdout);
console.log(stderr);
logger.debug(PLUGIN_NAME, "stdout:", stdout);
logger.debug(PLUGIN_NAME, "stderr:", stderr);
}
output.contents = fs.createReadStream(output.path, {
......@@ -51,8 +53,8 @@ module.exports = (options = {}) => {
});
this.push(output);
} catch (error) {
throw new PluginError("drawio", error);
throw new PluginError(PLUGIN_NAME, error);
}
})
);
};
}
const { obj } = require("through2");
const { callbackify } = require("util");
const File = require("vinyl");
const PluginError = require("plugin-error");
import { callbackify } from "util";
import File from "vinyl";
import logger from "gulplog";
import { obj } from "through2";
import PluginError from "plugin-error";
const PLUGIN_NAME = "index";
/**
* @param {ReadableStream} stream
......@@ -31,7 +34,7 @@ async function vinylToString(file) {
return "";
}
const pandocTitleBlockRegExp = /^%( .+(?:\n .+)*)?\n%( .+(?:\n .+)*)?\n%( .+)?\n/u;
const pandocTitleBlockRegExp = /^%( .+(?:\n {2}.+)*)?\n%( .+(?:\n {2}.+)*)?\n%( .+)?\n/u;
/**
* @param {string} text
......@@ -54,7 +57,7 @@ function parseYamlTitleBlock(text) {
return null;
}
throw new PluginError(
"index",
PLUGIN_NAME,
new Error("yaml title block is not supported"),
{
lineNumber: 1,
......@@ -62,8 +65,8 @@ function parseYamlTitleBlock(text) {
);
}
const renderIndex = (inputs) =>
`% Présentations
function renderIndex(inputs) {
return `% Présentations
% Dev@Science
% ${new Date().toLocaleDateString("fr-FR", {
year: "numeric",
......@@ -91,11 +94,12 @@ ${inputs
})`
)
.join("\n")}`;
}
module.exports = () => {
export default function index() {
const inputs = [];
return obj(
callbackify(async function (input, _enc) {
callbackify(async function (input) {
const text = await vinylToString(input);
input.titleBlock = parsePandocTitleBlock(text) ||
parseYamlTitleBlock(text) || { title: input.basename };
......@@ -104,7 +108,7 @@ module.exports = () => {
}),
callbackify(async function () {
if (inputs.find((file) => file.relative === "index.md")) {
console.log("found index.md, bailing out");
logger.info(`${PLUGIN_NAME}: will not override existing index.md`);
return;
}
inputs.sort((a, b) =>
......@@ -120,4 +124,4 @@ module.exports = () => {
);
})
);
};
}
const pandoc = require("gulp-pandoc");
import gulpPandoc from "gulp-pandoc";
const REVEALJS_URL =
process.env.REVEALJS_URL ||
"https://pole-is.gitlab.irstea.page/tools/reveal.js";
module.exports = () =>
pandoc({
export default function pandoc() {
return gulpPandoc({
from:
"markdown+backtick_code_blocks+pandoc_title_block+yaml_metadata_block",
to: "revealjs",
......@@ -25,3 +25,4 @@ module.exports = () =>
"--variable=slideNumber:true",
],
});
}
This diff is collapsed.
......@@ -4,7 +4,8 @@
"description": "Build INRAE-themed reveal.js presentatiokns.",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "echo \"Error: no test specified\" && exit 1",
"lint": "eslint --color ."
},
"keywords": [
"reveal.js",
......@@ -14,14 +15,58 @@
"license": "MIT",
"dependencies": {
"del": "^5.1.0",
"esm": "^3.2.25",
"gulp": "^4.0.2",
"gulp-pandoc": "^0.2.2",
"gulp-server-livereload": "^1.9.2",
"gulplog": "^1.0.0",
"plugin-error": "^1.0.1",
"through2": "^3.0.1",
"vinyl": "^2.2.0"
},
"devDependencies": {
"eslint": "^6.8.0",
"eslint-config-prettier": "^6.11.0",
"eslint-plugin-prettier": "^3.1.3",
"husky": "^4.2.5",
"lint-staged": "^10.2.2",
"prettier": "^2.0.5"
},
"eslintConfig": {
"extends": [
"eslint:recommended",
"plugin:prettier/recommended"
],
"parserOptions": {
"sourceType": "module"
},
"env": {
"node": true,
"es2017": true
},
"rules": {
"no-console": "warn",
"no-unused-vars": [
"error",
{
"args": "after-used"
}
],
"sort-imports": [
"error",
{
"ignoreCase": true
}
]
}
},
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"lint-staged": {
"*.js": "eslint",
"*.json": "prettier --check"
}
}
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