-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
110 lines (92 loc) · 3.15 KB
/
build.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
const fs = require('fs');
const { groups, indexKeys } = require('./config')
const buildGroupPublicKeyBlocks = () => {
const groupPublicKeyBlocks = {}
const _groups = Object.keys(groups)
_groups.forEach(group => {
const groupMembers = groups[group];
let groupKeysBlock = '';
groupMembers.forEach(member => {
const file = readFile(`./keyring/${member}.asc`)
groupKeysBlock = groupKeysBlock + file + "\n"
})
groupPublicKeyBlocks[group] = groupKeysBlock
})
return groupPublicKeyBlocks;
}
const copyFile = (src, destination) => {
fs.copyFile(src, destination, (err) => {
if (err) {
console.log("build.js:copyFile failed with error: ", err);
}
else {
}
});
}
const readFile = filePath => {
const data = fs.readFileSync(filePath, 'utf8')
return data;
}
const buildKeyBlockMethod = (keys) => {
const openingMethodLines = `
const armoredPublicKeys = async () => {
const keys = [
`
const closingMethodLines = `
]
return keys;
}
`
const keyBlockMethod = openingMethodLines + "`" + keys + "`" + closingMethodLines;
return keyBlockMethod;
}
const buildPageWithKeyBlock = (keys, pageName, templatePath = "./src/template.html") => {
const fileName = `${pageName}.html`
const keyBlockMethod = buildKeyBlockMethod(keys)
console.log('kbm', keyBlockMethod)
const templateFile = readFile(templatePath);
const groupFile = templateFile + "\n" + "<script>" + "\n" + keyBlockMethod + "\n" + "</script>" + "\n"
fs.writeFile(`./_build/${fileName}`, groupFile, (err) => {
if (err)
console.log("build.js:buildPageWithKeyBlock failed with error:", err);
else {
console.log("build.js:buildPageWithKeyBlock successfully wrote file: ", fileName)
}
});
}
const buildGroupFiles = () => {
const groupPublicKeyBlocks = buildGroupPublicKeyBlocks()
const _groups = Object.keys(groups)
_groups.forEach(group => {
const keys = groupPublicKeyBlocks[group];
buildPageWithKeyBlock(keys, group)
})
}
const buildScriptTagWithKeys = () => {
// Build group pages by concatenating the template.html contents with
// the key block of all keys that should be encrypted to on that page
buildGroupFiles()
// Build index page by concatenating the index.html contents with
// the key block of keys defined in the config.js, under the index value
let indexKeyBlock = ''
indexKeys.forEach(key => {
const file = readFile(`./keyring/${key}.asc`)
indexKeyBlock = indexKeyBlock + file + "\n"
})
buildPageWithKeyBlock(indexKeyBlock, 'index')
// Copy all dependencies and additional files that are required
// by the build as well as the "individual.html"
copyFile('./src/individual.html', './_build/individual.html')
copyFile('./src/index.html', './_build/index.html')
copyFile('./style/main.css', './_build/main.css')
copyFile('./deps/methods.js', './_build/methods.js')
copyFile('./deps/openpgp.js', './_build/openpgp.js')
}
const build = () => {
fs.mkdir('./_build', { recursive: true }, (err) => {
if (err) console.log('build.js:build failed with error: ', err)
})
buildGroupPublicKeyBlocks()
buildScriptTagWithKeys()
}
build();