-
Notifications
You must be signed in to change notification settings - Fork 12
/
page-signer.js
executable file
·59 lines (47 loc) · 1.34 KB
/
page-signer.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
#!/usr/bin/env node
/* eslint node: true */
const fs = require('fs');
const child_process = require('child_process');
const Minimize = require('minimize');
function errorAbort(text) {
console.error(text);
process.exit(1);
}
function getSignature(content, callback) {
const tmpfile = `/tmp/${process.pid}`;
fs.writeFileSync(tmpfile, content, 'utf-8');
const gpg = child_process.spawnSync('gpg', ['--armor', '--output', '-', '--detach-sign', tmpfile], {
stdio: [
0,
'pipe',
]
});
fs.unlink(tmpfile, () => {});
callback(gpg.stdout.toString());
}
let args = process.argv.slice(2);
const filename = args.shift();
const outfile = args.shift();
if (!filename) {
errorAbort(`Usage: ${process.argv[1]} <infile> [outfile]`);
}
fs.readFile(filename, 'utf8', (err, data) => {
if (err) {
errorAbort(err);
}
// Minimize and strip the doctype
const content = new Minimize({ spare:true, conditionals: true, empty: true, quotes: true }).parse(data)
.replace(/^\s*<!doctype[^>]*>/i, '');
getSignature(content, (signature) => {
const out = data.replace('%%%SIGNED_PAGES_PGP_SIGNATURE%%%', signature);
if (outfile) {
fs.writeFile(outfile, out, 'utf8', (writeErr) => {
if (writeErr) {
errorAbort(writeErr);
}
});
} else {
process.stdout.write(out);
}
});
});