forked from Adventech/sabbath-school-lessons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy-pdf.js
133 lines (118 loc) Β· 4.04 KB
/
deploy-pdf.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
#!/usr/bin/env node
let firebase = require("firebase-admin"),
glob = require("glob"),
yamljs = require("js-yaml"),
fs = require("fs-extra"),
crypto = require('crypto'),
path = require("path");
const { getCompilationQuarterValue, getInfoFromPath } = require('./deploy-helper')
let argv = require("optimist").usage("Compile & deploy pdf - DON'T USE IF YOU DON'T KNOW WHAT IT DOES\n" +
"Usage: $0 -b [string]")
.alias({"b": "branch"})
.describe({
"b": "branch",
"l": "language",
"q": "quarter",
"m": "mode"
})
.demand(["b"])
.argv;
let branch = argv.b,
compile_language = argv.l || "*",
compile_quarter = argv.q || getCompilationQuarterValue(),
mode = argv.m || "gen";
let API_HOST = "https://sabbath-school.adventech.io/api/",
PDF_HOST = "https://sabbath-school-pdf.adventech.io/",
SOURCE_DIR = "src/",
SOURCE_PDF_FILE = "pdf.yml";
let db
if (branch.toLowerCase() === "master") {
API_HOST = "https://sabbath-school.adventech.io/api/";
PDF_HOST = "https://sabbath-school-pdf.adventech.io/";
firebase.initializeApp({
databaseURL: "https://blistering-inferno-8720.firebaseio.com",
credential: firebase.credential.cert(require('./deploy-creds.json')),
databaseAuthVariableOverride: {
uid: "deploy"
}
});
db = firebase.database();
} else if (branch.toLowerCase() === "stage") {
API_HOST = "https://sabbath-school-stage.adventech.io/api/";
PDF_HOST = "https://sabbath-school-pdf-stage.adventech.io/";
firebase.initializeApp({
databaseURL: "https://sabbath-school-stage.firebaseio.com",
credential: firebase.credential.cert(require('./deploy-creds-stage.json')),
databaseAuthVariableOverride: {
uid: "deploy"
}
});
db = firebase.database();
} else {
firebase = {
app: function () {
return {
delete: function () {}
}
}
};
db = {
ref: function () {
return {
set: function () {},
child: function () {
return {
set: function (a) {},
once: function () {}
}
}
}
},
goOffline: function () {}
}
}
let pdfAPI = async function (mode) {
console.log('Deploying pdf API');
let pdfInfoFiles = glob.sync(`${SOURCE_DIR}/${compile_language}/${compile_quarter}/${SOURCE_PDF_FILE}`);
let curlConfig = ""
for (let pdfInfo of pdfInfoFiles) {
let pdfs = yamljs.load(fs.readFileSync(`${pdfInfo}`)),
info = getInfoFromPath(pdfInfo);
for (let [i, pdf] of pdfs.pdf.entries()) {
if (!pdf['src']) { continue }
if (!pdf['target']) {
pdf['target'] = `${info.language}/${info.quarterly}/${String(i+1).padStart(2, '0')}`
}
pdf.id = crypto.createHash('sha256').update(pdf['target'] + pdf['src']).digest('hex');
let extname = ".pdf"
if (mode === "keep" && fs.pathExistsSync(`pdf/pdf/${info.language}/${info.quarterly}/${pdf.id}/${pdf.id}${extname}`)) {
let stats = fs.statSync(`pdf/pdf/${info.language}/${info.quarterly}/${pdf.id}/${pdf.id}${extname}`);
if (stats.size > 0) {
fs.outputFileSync(`pdf/pdf/${info.language}/${info.quarterly}/${pdf.id}/.keep`, "");
}
}
if (mode === "gen" && !fs.pathExistsSync(`pdf/pdf/${info.language}/${info.quarterly}/${pdf.id}/`)) {
curlConfig += `
url = "${pdf.src}"
output = "pdf/pdf/${info.language}/${info.quarterly}/${pdf.id}/${pdf.id}${extname}"
-C -
--create-dirs
-L
`
}
}
}
if (mode === "gen" && curlConfig.trim().length > 1) {
fs.outputFileSync(`curl-config.txt`, curlConfig);
}
};
((async function () {
try {
await pdfAPI(mode);
} catch (e) {
console.error(e)
}
})()).then(() => {
db.goOffline();
firebase.app().delete();
});