-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.cjs
842 lines (712 loc) · 23.1 KB
/
build.cjs
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
/**
* Copyright (C) zxyz 2023
* This Source Code Form is subject to the terms
* of the Mozilla Public License, v. 2.0. If a
* copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* Build system for regitra-parody
* This is absolute and total hell.
* I hate callbacks so much.
*/
const CURRENT_SCHEMA_VERSION = "v4";
const DB_NAME = "./content.db";
/**
* Chapters:
*
* 1 - START
* 2 - ARGS
* 3 - HELP
* 4 - VERSION
* 5 - NEW DATABASE
* 6 - ITERATOR
* 7 - BUILD
* 8 - DOWNLOAD
* 9 - IMPORT JSON
* 10 - IMPORT IMAGES
*
* To navigate to the beginning of a chapter, cmd+f using its name
* To navigate to the end of a chapter, cmd+f its name again
*/
/* --- START --- */
if (process.argv.length === 2) {
PrintHelp();
process.exit(1);
}
const fs = require("fs");
const path = require("path");
const sqlite3 = require("sqlite3").verbose();
const { exec, execSync } = require("child_process");
const https = require("https");
const dbName = DB_NAME;
/* --- START --- */
/* --- ARGS --- */
switch (process.argv[2]) {
case "new_db":
NewDatabase();
break;
case "build_src":
BuildSrc();
break;
case "build":
Build();
break;
case "download":
DownloadDB();
break;
case "import":
InvokeImport();
break;
case "version":
CheckVersion();
break;
case "help":
default:
PrintHelp();
break;
}
function InvokeImport() {
switch (process.argv[3]) {
case "questions":
ImportV3JSON();
break;
case "images":
ImportV3Images(false);
break;
case "all":
ImportV3Images(true);
break;
default:
PrintImportHelp();
break;
}
}
/* --- ARGS --- */
/* --- HELP --- */
function PrintHelp() {
console.log(
`The build system for regitra-parody for schema version ${CURRENT_SCHEMA_VERSION}
Usage: node build.cjs [command]
Available commands: will be performed on ${DB_NAME}
new_db Creates a new database
build_src Builds ./src/generated
build Builds ./public/generated
download Downloads .db file from env/DATABASE_URL
import Imports data in bulk
version Check the schema version of current database
help Prints this message
regitra-parody is in development, some features you expect may not be there.
regitra-parody is licensed under MPL-2.0 and includes no warranty.`,
);
}
function PrintImportHelp() {
console.log(
`Import system for regitra-parody schema ${CURRENT_SCHEMA_VERSION}
Usage: node build.cjs import [command]
Import commands: (usage: node build.js import [command])
all Import all data
images Import images exclusively
questions Import questions exclusively
To see how to arrange your data for import, see the hitchhiker's guide.`,
);
}
/* --- HELP --- */
/* --- VERSION --- */
function CheckVersion() {
let db = new sqlite3.Database(dbName);
db.get(`select value from meta where key = 'version'`, (err, value) => {
if (err) {
if (err.message.includes("no such table: meta")) {
console.log("v0");
} else {
console.error(err);
process.exit(1);
}
process.exit(0);
} else {
console.log(value.value);
}
});
}
/* --- VERSION --- */
/* --- NEW DATABASE --- */
function NewDatabase() {
if (fs.existsSync(dbName)) {
console.log(
"\x1b[31mNO!\x1b[0m\n" +
"A database " +
dbName +
" \x1b[31malready exists\x1b[0m!\n" +
"Either delete it manually or pick a different database name.",
);
process.exit(1);
}
const db = new sqlite3.Database(dbName);
db.serialize(() => {
db.run(`
CREATE TABLE languages (
language_code TEXT PRIMARY KEY,
display_name TEXT
);
`);
db.run(`
CREATE TABLE category (
name TEXT PRIMARY KEY,
display_name TEXT,
makeup TEXT,
question_amount INTEGER
);
`);
db.run(`
CREATE TABLE meta (
key TEXT PRIMARY KEY,
value TEXT
);
`);
db.run(`
CREATE TABLE images (
image_id INTEGER PRIMARY KEY,
image_name TEXT,
image_data_uri TEXT,
alt_text INTEGER
);
`);
db.run(`
CREATE TABLE image_alt_text (
id INTEGER PRIMARY KEY,
image_id INTEGER,
language TEXT,
alt_text TEXT,
FOREIGN KEY (image_id) REFERENCES images (image_id)
);
`);
db.run(`
CREATE TABLE questions (
id INTEGER PRIMARY KEY,
language TEXT,
category TEXT,
question_text TEXT,
image_id INTEGER,
relative_answers INTEGER,
FOREIGN KEY (image_id) REFERENCES images (image_id)
);
`);
db.run(`
CREATE TABLE possible_answers (
id INTEGER PRIMARY KEY,
question_id INTEGER,
answer_text TEXT,
answer_order INTEGER,
FOREIGN KEY (question_id) REFERENCES questions (id)
);
`);
db.run(`
CREATE TABLE correct_answers (
id INTEGER PRIMARY KEY,
question_id INTEGER,
answer_id INTEGER,
answer_id_relative INTEGER,
FOREIGN KEY (question_id) REFERENCES questions (id),
FOREIGN KEY (answer_id) REFERENCES possible_answers (id)
);
`);
db.run(`
INSERT INTO meta (key, value)
VALUES
('version', '${CURRENT_SCHEMA_VERSION}');
`);
db.close((error) => {
if (error) {
return console.error(error.message);
}
console.log("\x1b[32mDatabase created successfully\x1b[0m as " + dbName);
console.log(
"\nYour database was created with the default settings.\n" +
"It only contains the schema. \x1b[31mIt does not contain any questions or answers.\x1b[0m\n" +
"In order to make those, modify the database accordingly.\n" +
"For more information, consult the hitchhiker's guide.\n\n" +
"\x1b[31mRegitra Parody's site in hosted or source form, nor the build tool, nor the\n" +
"database are given any warranty or legal protection.\n" +
"Do everything at your own risk.\x1b[0m",
);
});
});
}
/* --- NEW DATABASE --- */
/* --- ITERATOR --- */
/**
* @param {import("sqlite3").Database} db
* @param {(lang: Language, cat: Category) => void} callback
*/
function iterateCatsAndDogs(db, callback) {
db.all(
`select * from languages`,
/**
* @param {Language[]} langs
*/
(err, langs) => {
if (err) {
console.error("Error executing the SELECT query:", err);
} else {
// 'rows' will contain the result of the query as an array of objects
db.all(
`select * from category`,
/**
* @param {Category[]} cats
*/
(err, cats) => {
if (err) {
console.error("Error executing the SELECT query:", err);
} else {
langs.forEach((lang) => {
cats.forEach((cat) => {
callback(lang, cat);
});
});
}
},
);
}
},
);
}
/* --- ITERATOR --- */
/* --- BUILD --- */
function BuildCount() {
/**
* @type {{[language: string]: {[category: string]: number}}}
*/
let counts = {};
let db = new sqlite3.Database(dbName);
execSync(`mkdir -p ./src/generated`);
iterateCatsAndDogs(db, (lang, cat) => {
if (!(lang.language_code in counts)) counts[lang.language_code] = {};
db.get(
`select count(*) from questions where language = ? and category = ?`,
[lang.language_code, cat.name],
/**
* @param {{"count(*)": number}} row
*/
(err, row) => {
counts[lang.language_code][cat.name] = row["count(*)"];
fs.writeFileSync("./src/generated/count.json", JSON.stringify(counts));
},
);
});
}
function BuildSrc() {
console.log("[build.cjs] Building ./src/generated...");
BuildCount();
let db = new sqlite3.Database(dbName);
const packageJson = JSON.parse(fs.readFileSync("./package.json"));
db.serialize(() => {
db.all(`select * from meta`, (err, meta_db) => {
let meta = {};
for (let m of meta_db) {
meta[m.key] = m.value;
}
const versionsJson = JSON.stringify({
version: packageJson.version,
schemaVersion: meta["version"],
});
let defaultsMap = {
l: meta["default_language"],
c: meta["default_category"],
};
fs.writeFileSync(
"./src/generated/defaults.json",
JSON.stringify(defaultsMap),
);
fs.writeFileSync("./src/generated/versions.json", versionsJson);
});
db.all(`select * from languages`, (err, languages) => {
fs.writeFileSync(
"./src/generated/languages.json",
JSON.stringify(languages.map((l) => l.language_code)),
);
});
db.all(`select * from category`, (err, categories) => {
let categoryMap = {};
for (const c of categories) {
categoryMap[c["name"]] = {
makeup: JSON.parse(c["makeup"]),
qNum: c["question_amount"],
};
}
fs.writeFileSync(
"./src/generated/categories.json",
JSON.stringify(categoryMap),
);
});
db.close((err) => {
if (err) console.log(err);
});
});
}
function resizeCalc(width, height, maxWidth, maxHeight) {
const aspectRatio = height / width;
let newDim = [0, 0];
if (width > maxWidth) {
newDim[0] = maxWidth;
newDim[1] = Math.round(maxWidth * aspectRatio);
}
if (height > maxHeight) {
newDim[1] = maxHeight;
newDim[0] = Math.round(maxHeight / aspectRatio);
}
if (newDim[0] > maxWidth || newDim[1] > maxHeight)
return resizeCalc(newDim[0], newDim[1]);
if (newDim[0] == 0 && newDim[1] == 0) newDim = [width, height];
return newDim;
}
function Build() {
console.log("[build.cjs] Building ./public/generated...");
const sharp = require("sharp");
let db = new sqlite3.Database(dbName);
execSync("mkdir -p ./public/generated/img");
iterateCatsAndDogs(db, (lang, cat) => {
execSync(
`mkdir -p ./public/generated/questions/${lang.language_code}/${cat.name}`,
);
execSync(
`mkdir -p ./public/generated/answers/${lang.language_code}/${cat.name}`,
);
db.all(
`select * from questions where language = ? and category = ?`,
[lang.language_code, cat.name],
/**
* @param {Question[]} rows
*/
(err, rows) => {
rows.forEach((row, idx) => {
/**
* @type {{
* q: string,
* a: string[],
* i: string | null,
* alt: string | null
* }}
*/
let question = {
q: row.question_text,
};
db.all(
`select * from possible_answers where question_id = ?`,
[row.id],
/**
* @param {PossibleAnswer[]} answers
*/
(err, answers) => {
if (row.relative_answers) {
// New style relative answer IDs
answers = answers.sort((a, b) =>
a.answer_order > b.answer_order
? 1
: a.answer_order < b.answer_order
? -1
: 0,
);
} else {
// Legacy absolute answer IDs
answers = answers.sort((a, b) =>
a.id > b.id ? 1 : a.id < b.id ? -1 : 0,
);
}
question["a"] = answers.map((v) => v.answer_text);
db.all(
`select * from correct_answers where question_id = ?`,
[row.id],
/**
* @param {CorrectAnswer[]} corrects
*/
(err, corrects) => {
if (row.relative_answers) {
corrects = corrects.sort((a, b) =>
a.answer_id_relative > b.answer_id_relative
? 1
: a.answer_id_relative < b.answer_id_relative
? -1
: 0,
);
} else {
corrects = corrects.sort((a, b) =>
a.id > b.id ? 1 : a.id < b.id ? -1 : 0,
);
}
/** @type number[] */
let _corrects = [];
if (row.relative_answers) {
_corrects = corrects.map((v) => v.answer_id_relative);
} else {
_corrects = corrects.map((v) => v.answer_id);
}
/** @type number[] */
let ans = answers.map((v) => v.id);
/** @type number[] */
let cor = [];
if (row.relative_answers) {
cor = _corrects;
} else {
cor = _corrects.map((v) => ans.indexOf(v) + 1);
}
function writeOuts() {
fs.writeFileSync(
`./public/generated/questions/${lang.language_code}/${cat.name}/${idx}.json`,
JSON.stringify(question),
);
fs.writeFileSync(
`./public/generated/answers/${lang.language_code}/${cat.name}/${idx}.json`,
JSON.stringify(cor),
);
}
if (row.image_id) {
db.get(
`select * from images where image_id = ?`,
[row.image_id],
/**
* @param {Image} img
*/
async (err, img) => {
let extension = img.image_data_uri.substring(
img.image_data_uri.indexOf("/") + 1,
img.image_data_uri.indexOf(";"),
);
let b64image = img.image_data_uri.substring(
img.image_data_uri.indexOf(",") + 1,
);
let imgBuffer = Uint8Array.from(atob(b64image), (c) =>
c.charCodeAt(0),
);
const orig_img_sharp = sharp(imgBuffer, {
animated: extension === "gif" ? true : false,
});
const orig_img_metadata =
await orig_img_sharp.metadata();
const orig_img_dimensions = {
w: orig_img_metadata.width,
h: orig_img_metadata.height,
};
// Mobile: Images are viewed in max-width: 300px;max-height: 200px;
// Desktop: Images are viewed in max-width: 400px;max-height: 300px;
const resolution_table = {
sm: { w: 400 * 0.75, h: 300 * 0.75 },
md: { w: 400 * 1.5, h: 300 * 1.5 },
lg: { w: 400 * 2.25, h: 300 * 2.25 },
orig: { w: 400 * 3, h: 300 * 3 },
};
let qualifies_for_sizes = [];
for (let res in resolution_table) {
if (
orig_img_dimensions.w > resolution_table[res].w &&
orig_img_dimensions.h > resolution_table[res].h
) {
qualifies_for_sizes.push(res);
}
}
if (!qualifies_for_sizes.includes("orig")) {
qualifies_for_sizes.push("orig");
}
if (
!fs.existsSync(
`./public/generated/img/${row.image_id}-orig.${extension}`,
)
) {
for (let res of qualifies_for_sizes) {
let max_width = resolution_table[res].w;
let max_height = resolution_table[res].h;
let [new_width, new_height] = resizeCalc(
orig_img_dimensions.w,
orig_img_dimensions.h,
max_width,
max_height,
);
let resized_buffer = await orig_img_sharp.resize(
new_width,
new_height,
);
await resized_buffer
.webp({ quality: 80 })
.toFile(
`./public/generated/img/${row.image_id}-${res}.webp`,
);
await resized_buffer.toFile(
`./public/generated/img/${row.image_id}-${res}.${extension}`,
);
}
}
question["i"] = row.image_id;
question["if"] = ["webp", extension];
question["is"] = qualifies_for_sizes;
if (img.alt_text) {
db.get(
`select * from image_alt_text where image_id = ? and language = ?`,
[img.image_id, lang.language_code],
/**
* @param {ImageAltText} alttext
*/
(err, alttext) => {
question["alt"] = alttext.alt_text;
writeOuts();
},
);
} else {
writeOuts();
}
},
);
} else {
writeOuts();
}
},
);
},
);
});
},
);
});
}
/* --- BUILD --- */
/* --- DOWNLOAD --- */
function DownloadDB() {
const url = process.env.DATABASE_URL;
const file = fs.createWriteStream(dbName);
const request = https.get(url, function (response) {
response.pipe(file);
});
}
/* --- DOWNLOAD --- */
/* --- IMPORT JSON --- */
function ImportV3JSON() {
let db = new sqlite3.Database(dbName);
const text_db = JSON.parse(fs.readFileSync("./import/questions.json"));
db.serialize(() => {
let question_count = 0;
const image_ids = JSON.parse(fs.readFileSync("./import/image_ids.json"));
for (let block of text_db) {
db.run(
`insert into questions
(language, category, question_text, image_id, relative_answers)
values (?,?,?,?,?);`,
[block.l, block.c, block.q, image_ids[block.i], 1],
function (err) {
let question_id = this.lastID;
let answer_count = 0;
let answer_ids = [];
for (let section in block.a) {
db.run(
`insert into possible_answers
(question_id, answer_text, answer_order)
values (?, ?, ?);`,
[question_id, block.a[section], parseInt(section) + 1],
function (err) {
answer_count++;
if (answer_count == block.a.length) {
for (let ca of block.ca) {
db.run(
`insert into correct_answers
(question_id, answer_id_relative)
values (?, ?);`,
[question_id, ca],
);
}
}
},
);
}
},
);
}
});
}
/* --- IMPORT JSON --- */
/* --- IMPORT IMAGES --- */
function ImportV3Images(cont) {
let db = new sqlite3.Database(dbName);
const image_ids_name = {};
const alt_text_db = JSON.parse(fs.readFileSync("./import/alts.json"));
db.serialize(() => {
fs.readdir("./import", (err, files) => {
if (err) {
console.error("Error reading image folder:", err);
return;
}
function importAltText() {
let images_counter = 0;
for (let alt_text of alt_text_db) {
const id_db = JSON.parse(fs.readFileSync("./import/image_ids.json"));
let id = id_db[alt_text.name];
let texts_added = 0;
for (let text in alt_text.alt) {
db.run(
`insert into image_alt_text
(image_id, language, alt_text)
values (?, ?, ?);`,
[id, text, alt_text.alt[text]],
function (err) {
db.run(`update images set alt_text = 1 where image_id = ?`, [
id,
]);
console.log(`Added ${text} alt text for ${alt_text.name}`);
texts_added++;
if (texts_added === Object.keys(alt_text.alt).length) {
images_counter++;
if (cont && images_counter == alt_text_db.length) {
console.log("Continuing onto JSON import...");
ImportV3JSON();
}
}
},
);
}
}
}
let imported = 0;
const image_files = files.filter(
(file) => !file.includes(".json") && !file.includes(".DS_Store"),
);
// Process each image file
for (let file of image_files) {
if (file.includes(".json") || file.includes(".DS_Store")) {
continue;
}
const imagePath = `./import/${file}`;
// Use the 'file' command to determine the MIME type
exec(`file --mime-type -b "${imagePath}"`, (error, stdout) => {
if (error) {
console.error(`Error running 'file' command for ${file}:`, error);
return;
}
// Read the image file and convert it to base64
const imageBuffer = fs.readFileSync(imagePath);
const imageBase64 = imageBuffer.toString("base64");
// Insert the image data into the 'images' table
db.run(
"INSERT INTO images (image_name, image_data_uri) VALUES (?, ?)",
[file, `data:${stdout.trim()};base64,${imageBase64}`],
function (insertError) {
if (insertError) {
console.error(
`Error inserting ${file} into the database:`,
insertError,
);
} else {
console.log(`Inserted ${file} into the database.`);
image_ids_name[file] = this.lastID;
imported++;
if (imported == image_files.length) {
fs.writeFileSync(
"./import/image_ids.json",
JSON.stringify(image_ids_name),
);
importAltText();
}
}
},
);
});
}
});
});
}
/* --- IMPORT IMAGES --- */