-
Notifications
You must be signed in to change notification settings - Fork 13
/
cli.js
executable file
·99 lines (85 loc) · 2.22 KB
/
cli.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
#!/usr/bin/env node
"use strict";
const fs = require("fs");
const path = require("path");
const argv = require("minimist")(process.argv.slice(2));
const PizZip = require("pizzip");
const Docxtemplater = require("docxtemplater");
const expressions = require("angular-expressions");
function transformError(error) {
const e = {
message: error.message,
name: error.name,
stack: error.stack,
properties: error.properties,
};
if (e.properties && e.properties.rootError) {
e.properties.rootError = transformError(error.properties.rootError);
}
if (e.properties && e.properties.errors) {
e.properties.errors = e.properties.errors.map(transformError);
}
return e;
}
function printErrorAndRethrow(error) {
const e = transformError(error);
// The error thrown here contains additional information when logged with JSON.stringify (it contains a property object).
console.error(JSON.stringify({ error: e }, null, 2));
throw error;
}
function showHelp() {
console.log("Usage: docxtemplater input.docx data.json output.docx");
process.exit(1);
}
function parser(tag) {
const expr = expressions.compile(tag.replace(/’/g, "'"));
return {
get(scope) {
return expr(scope);
},
};
}
const args = argv._;
if (argv.help || args.length !== 3) {
showHelp();
}
let options = {};
if (argv.options) {
try {
options = JSON.parse(argv.options);
} catch (e) {
console.error("Arguments passed in --options is not valid JSON");
throw e;
}
}
if (argv.modules) {
try {
options.modules = require(path.resolve(process.cwd(), argv.modules));
} catch (e) {
console.error("Arguments passed in --modules is not a js file");
throw e;
}
if (!(options.modules instanceof Array)) {
console.log("Modules should be an array");
process.exit(1);
}
}
const [inputFile, dataFile, outputFile] = args;
const input = fs.readFileSync(inputFile, "binary");
const data = JSON.parse(fs.readFileSync(dataFile, "utf-8"));
options.parser = parser;
let doc;
try {
doc = new Docxtemplater(new PizZip(input), options);
} catch (e) {
printErrorAndRethrow();
}
try {
doc.render(data);
} catch (error) {
printErrorAndRethrow();
}
const generated = doc
.getZip()
.generate({ type: "nodebuffer", compression: "DEFLATE" });
fs.writeFileSync(outputFile, generated);