-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
executable file
·153 lines (134 loc) · 4.75 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/env node
const path = require('path');
const fs = require('fs');
const puppeteer = require('puppeteer');
const proc = require('process');
function parseArgs() {
return require('yargs')
.command('$0 <file>', 'render a bpmn diagram', (yargs) => {
yargs.positional('file', {describe: 'a BPMN-XML process definition file '})
.usage('Usage: $0 [options] <file>')
})
.option('t', {
describe: 'file type of the new diagram',
choices: ['svg', 'png', 'jpeg', 'pdf'],
alias: 'type',
default: 'svg'
})
.option('o', {
describe: 'output path for the rendered diagram.',
alias: 'output',
defaultDescription: '<input>.<type>'
})
.option('w', {
describe: 'width of the diagram (does not apply to svg)',
alias: 'width',
default: 1024,
type: 'number'
})
.option('h', {
describe: 'height of the diagram (does not apply to svg)',
alias: 'height',
default: 768,
type: 'number'
})
.wrap(120)
.version()
.help()
.argv;
}
async function renderDiagram(bpmnXML, options) {
let browser;
try {
browser = await puppeteer.launch({
defaultViewport: {
width: options.width,
height: options.height,
landscape: true,
deviceScaleFactor: 2
},
args: ['--no-sandbox'],
executablePath: process.env.CHROMIUM_PATH
});
const page = await browser.newPage();
page.on('console', msg => console.log("BPMN-js: " + msg.text()));
await page.goto(`file://${path.join(__dirname, 'index.html')}`);
const bpmnJsDist = path.resolve(require.resolve('bpmn-js'), '../dist');
await page.addScriptTag({path: path.resolve(bpmnJsDist, 'bpmn-viewer.production.min.js')});
const svg = await page.$eval('#container', (container, bpmnXML, options) => {
const viewer = new BpmnJS({container: '#container'});
function loadDiagram() {
return new Promise((resolve, reject) => {
viewer.importXML(bpmnXML, function (err) {
if (err) {
reject(err);
} else {
console.log('Diagram looks valid');
resolve();
}
});
});
}
function exportSVG() {
return new Promise((resolve, reject) => {
viewer.saveSVG((err, svg) => {
if (err) {
console.log('Failed to export', err);
reject(err);
} else {
resolve(svg);
}
});
});
}
return loadDiagram().then(() => {
if (options.type === 'svg') {
return exportSVG();
} else {
const canvas = viewer.get('canvas');
canvas.zoom('fit-viewport', 'auto');
}
}).catch((err) => {
throw err;
});
}, bpmnXML, options);
switch (options.type) {
case 'svg':
fs.writeFileSync(options.output, svg);
break;
case 'png':
case 'jpeg':
await page.screenshot({omitBackground: true, type: options.type})
.then(image => fs.writeFileSync(options.output, image));
break;
case 'pdf':
await page.pdf({landscape: true, format: 'a4', pageRange: 1, preferCSSPageSize: true})
.then(pdf => fs.writeFileSync(options.output, pdf));
break;
}
} catch (e) {
error( e);
} finally {
browser.close();
}
}
function error(e) {
console.error('Failed to render diagram\n', e);
process.exit(1);
}
if (require.main === module) {
const options = parseArgs();
if (!fs.existsSync(options.file)) {
error(`File ${options.file} does not exist.`);
}
if (!options.output) {
let baseName = path.parse(options.file).name;
let directoryName = path.parse(options.file).dir;
options.output = `${directoryName}/${baseName}.${options.type}`;
}
const bpmnXML = fs.readFileSync(options.file, {encoding: 'utf-8'});
renderDiagram(bpmnXML, options)
.then(() => console.log("Export complete"))
.catch(error);
}
module.exports = renderDiagram;