From 47e72cf33e99b1e91c142d07b65d96ef83ecc331 Mon Sep 17 00:00:00 2001 From: David Bonnici Date: Fri, 19 Jun 2020 14:20:18 +0200 Subject: [PATCH 1/5] Added support using Docker --- .dockerignore | 8 ++++++++ .gitignore | 4 ++++ Dockerfile | 10 ++++++++++ README.md | 10 +++++++++- convert.sh | 13 +++++++++++++ export.js | 4 +++- index.js | 1 + 7 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100755 convert.sh diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..253ec9b --- /dev/null +++ b/.dockerignore @@ -0,0 +1,8 @@ +./node_modules +./project +.DS_Store +.dockerignore +Dockerfile +.git +./files/* +.gitignore \ No newline at end of file diff --git a/.gitignore b/.gitignore index 54c9e4d..7e025c5 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,7 @@ VERSION.json .DS_Store npm-debug.log yarn-error.log + +./project +project +package-lock.json diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..442debc --- /dev/null +++ b/Dockerfile @@ -0,0 +1,10 @@ +FROM node:lts-alpine3.11 +RUN apk add chromium +ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD true +ENV CHROMIUM_PATH /usr/bin/chromium-browser +RUN mkdir -p /home/node/draw.io-export /files +WORKDIR /home/node/draw.io-export +COPY . . +RUN npm install +VOLUME ["/files"] +ENTRYPOINT [ "sh", "convert.sh" ] \ No newline at end of file diff --git a/README.md b/README.md index df66e8e..e6242ee 100644 --- a/README.md +++ b/README.md @@ -4,10 +4,18 @@ Convert [draw.io](https://app.diagrams.net/) xml file (usually `*.drawio`) to `p Works nicely with `make` and/or `latexmk`. Useful if you are writing a paper or thesis with many figures. -## Usage +## Usage with NPM ```bash npm install --global draw.io-export drawio -o drawio -o ``` + +## Usage with Docker + +``` +docker run --name drawioexport \ +-v [Your folder with draw.io files]:/files \ +davidbonnici1984/draw.io-export:0.1.0 +``` diff --git a/convert.sh b/convert.sh new file mode 100755 index 0000000..fc89aff --- /dev/null +++ b/convert.sh @@ -0,0 +1,13 @@ +echo "DRAW.IO-EXPORT Started." +dirs=$(find /files -name '*.drawio') + +for dir in $dirs +do + echo "Converting ${dir} to ${dir}.pdf" + rm $dir.pdf + node /home/node/draw.io-export/bin/drawio.js $dir -o $dir.pdf + echo "Converting ${dir} to ${dir}.png" + rm $dir.png + node /home/node/draw.io-export/bin/drawio.js $dir -o $dir.png +done +echo "DRAW.IO-EXPORT Finished." diff --git a/export.js b/export.js index 07bb9e5..47aadce 100644 --- a/export.js +++ b/export.js @@ -59,8 +59,10 @@ module.exports = async ({ file, format, path: p }) => { const xml = await readFile(file); const browser = await puppeteer.launch({ + executablePath: process.env.CHROMIUM_PATH, headless: true, - }); + args: ['--no-sandbox'] + }); try { const page = await browser.newPage(); diff --git a/index.js b/index.js index 4d15d3d..200dea4 100644 --- a/index.js +++ b/index.js @@ -5,6 +5,7 @@ const path = require('path'); const run = require('./export'); process.on('unhandledRejection', (e) => { + console.error(e); throw e; }); From 3d5d3df1c08338aed77a1c919ba58effa62ba2f4 Mon Sep 17 00:00:00 2001 From: David Bonnici Date: Sat, 20 Jun 2020 09:43:07 +0200 Subject: [PATCH 2/5] Adding checks before deleting previously generated files. --- convert.sh | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/convert.sh b/convert.sh index fc89aff..47fa240 100755 --- a/convert.sh +++ b/convert.sh @@ -3,11 +3,15 @@ dirs=$(find /files -name '*.drawio') for dir in $dirs do - echo "Converting ${dir} to ${dir}.pdf" - rm $dir.pdf + echo "Converting ${dir} to ${dir}.pdf" + if test -f "${dir}.pdf"; then + rm $dir.pdf + fi node /home/node/draw.io-export/bin/drawio.js $dir -o $dir.pdf echo "Converting ${dir} to ${dir}.png" - rm $dir.png + if test -f "${dir}.png"; then + rm $dir.png + fi node /home/node/draw.io-export/bin/drawio.js $dir -o $dir.png done echo "DRAW.IO-EXPORT Finished." From fc05b8e02a7dc35a3d765c6d5d63815d06befe4c Mon Sep 17 00:00:00 2001 From: b1f6c1c4 Date: Fri, 17 Jul 2020 17:12:48 -0400 Subject: [PATCH 3/5] fix minor coding style problems --- .dockerignore | 2 +- .gitignore | 4 ---- Dockerfile | 4 ++-- convert.sh | 16 +++++++--------- 4 files changed, 10 insertions(+), 16 deletions(-) diff --git a/.dockerignore b/.dockerignore index 253ec9b..9931e43 100644 --- a/.dockerignore +++ b/.dockerignore @@ -5,4 +5,4 @@ Dockerfile .git ./files/* -.gitignore \ No newline at end of file +.gitignore diff --git a/.gitignore b/.gitignore index 7e025c5..54c9e4d 100644 --- a/.gitignore +++ b/.gitignore @@ -12,7 +12,3 @@ VERSION.json .DS_Store npm-debug.log yarn-error.log - -./project -project -package-lock.json diff --git a/Dockerfile b/Dockerfile index 442debc..767819a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,9 +2,9 @@ FROM node:lts-alpine3.11 RUN apk add chromium ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD true ENV CHROMIUM_PATH /usr/bin/chromium-browser -RUN mkdir -p /home/node/draw.io-export /files +RUN mkdir -p /home/node/draw.io-export /files WORKDIR /home/node/draw.io-export COPY . . RUN npm install VOLUME ["/files"] -ENTRYPOINT [ "sh", "convert.sh" ] \ No newline at end of file +ENTRYPOINT ["sh", "convert.sh"] diff --git a/convert.sh b/convert.sh index 47fa240..e8f24e1 100755 --- a/convert.sh +++ b/convert.sh @@ -1,17 +1,15 @@ +#!/bin/bash + echo "DRAW.IO-EXPORT Started." dirs=$(find /files -name '*.drawio') for dir in $dirs do echo "Converting ${dir} to ${dir}.pdf" - if test -f "${dir}.pdf"; then - rm $dir.pdf - fi - node /home/node/draw.io-export/bin/drawio.js $dir -o $dir.pdf - echo "Converting ${dir} to ${dir}.png" - if test -f "${dir}.png"; then - rm $dir.png - fi - node /home/node/draw.io-export/bin/drawio.js $dir -o $dir.png + rm -f "$dir.pdf" + node /home/node/draw.io-export/bin/drawio.js "$dir" -o "$dir.pdf" + echo "Converting ${dir} to ${dir}.png" + rm -f "$dir.png" + node /home/node/draw.io-export/bin/drawio.js "$dir" -o "$dir.png" done echo "DRAW.IO-EXPORT Finished." From 7d3afec30db425bf92b362aef2fab0f52b4070b5 Mon Sep 17 00:00:00 2001 From: b1f6c1c4 Date: Fri, 17 Jul 2020 17:39:47 -0400 Subject: [PATCH 4/5] optimize convert flow --- Dockerfile | 8 ++++---- convert.sh | 20 +++++++------------- 2 files changed, 11 insertions(+), 17 deletions(-) diff --git a/Dockerfile b/Dockerfile index 767819a..21e73a6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,10 +1,10 @@ FROM node:lts-alpine3.11 RUN apk add chromium +WORKDIR /home/node/draw.io-export ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD true ENV CHROMIUM_PATH /usr/bin/chromium-browser -RUN mkdir -p /home/node/draw.io-export /files -WORKDIR /home/node/draw.io-export +COPY package*.json ./ +RUN npm ci --only=production COPY . . -RUN npm install VOLUME ["/files"] -ENTRYPOINT ["sh", "convert.sh"] +ENTRYPOINT /usr/bin/find /files -type f -name '*.drawio' -exec ./convert.sh '{}' \; diff --git a/convert.sh b/convert.sh index e8f24e1..718ce3d 100755 --- a/convert.sh +++ b/convert.sh @@ -1,15 +1,9 @@ -#!/bin/bash +#!/bin/sh -echo "DRAW.IO-EXPORT Started." -dirs=$(find /files -name '*.drawio') +printf "Converting %s to %s\n" "$1" "$1.pdf" +rm -f "$1.pdf" +node ./bin/drawio.js "$1" -o "$1.pdf" -for dir in $dirs -do - echo "Converting ${dir} to ${dir}.pdf" - rm -f "$dir.pdf" - node /home/node/draw.io-export/bin/drawio.js "$dir" -o "$dir.pdf" - echo "Converting ${dir} to ${dir}.png" - rm -f "$dir.png" - node /home/node/draw.io-export/bin/drawio.js "$dir" -o "$dir.png" -done -echo "DRAW.IO-EXPORT Finished." +printf "Converting %s to %s\n" "$1" "$1.png" +rm -f "$1.png" +node ./bin/drawio.js "$1" -o "$1.png" From 81ef556d1e05123d87f33265eebdff422eaf1b09 Mon Sep 17 00:00:00 2001 From: b1f6c1c4 Date: Fri, 17 Jul 2020 18:10:37 -0400 Subject: [PATCH 5/5] minor fix for dockerignore and readme --- .dockerignore | 19 +++++++++++++++---- README.md | 11 ++++++----- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/.dockerignore b/.dockerignore index 9931e43..51c292c 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,8 +1,19 @@ -./node_modules -./project +.cache + +# Artifact +node_modules +coverage +VERSION.json + +# Cruft +*~ +*.swp +*.swo .DS_Store +npm-debug.log +yarn-error.log + .dockerignore -Dockerfile .git -./files/* .gitignore +Dockerfile diff --git a/README.md b/README.md index e6242ee..af53fa0 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ Convert [draw.io](https://app.diagrams.net/) xml file (usually `*.drawio`) to `p Works nicely with `make` and/or `latexmk`. Useful if you are writing a paper or thesis with many figures. -## Usage with NPM +## Usage with `npm` ```bash npm install --global draw.io-export @@ -14,8 +14,9 @@ drawio -o ## Usage with Docker +```bash +docker run --rm \ + -v :/files \ + b1f6c1c4/draw.io-export ``` -docker run --name drawioexport \ --v [Your folder with draw.io files]:/files \ -davidbonnici1984/draw.io-export:0.1.0 -``` +