@helloao/cli 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/actions.d.ts +57 -0
- package/actions.js +262 -0
- package/cli.d.ts +2 -0
- package/cli.js +139 -0
- package/db.d.ts +110 -0
- package/db.js +754 -0
- package/downloads.d.ts +2 -0
- package/downloads.js +12 -0
- package/files.d.ts +56 -0
- package/files.js +232 -0
- package/index.d.ts +8 -0
- package/index.js +38 -0
- package/migrations/20240420231455_initial/migration.sql +66 -0
- package/migrations/20240623183848_add_book_order/migration.sql +26 -0
- package/migrations/20240629194121_add_chapter_links/migration.sql +45 -0
- package/migrations/20240629194513_add_chapter_content/migration.sql +30 -0
- package/migrations/20240705221833_remove_unused_columns/migration.sql +27 -0
- package/migrations/20240711173108_add_chapter_audio/migration.sql +13 -0
- package/migrations/20240724212651_add_hashing/migration.sql +25 -0
- package/node_modules/@zip.js/zip.js/LICENSE +28 -0
- package/node_modules/@zip.js/zip.js/README.md +173 -0
- package/node_modules/@zip.js/zip.js/deno.json +8 -0
- package/node_modules/@zip.js/zip.js/dist/README.md +28 -0
- package/node_modules/@zip.js/zip.js/dist/z-worker-fflate.js +1 -0
- package/node_modules/@zip.js/zip.js/dist/z-worker-pako.js +1 -0
- package/node_modules/@zip.js/zip.js/dist/z-worker.js +1 -0
- package/node_modules/@zip.js/zip.js/dist/zip-fs-full.js +11935 -0
- package/node_modules/@zip.js/zip.js/dist/zip-fs-full.min.js +1 -0
- package/node_modules/@zip.js/zip.js/dist/zip-fs.js +6079 -0
- package/node_modules/@zip.js/zip.js/dist/zip-fs.min.js +1 -0
- package/node_modules/@zip.js/zip.js/dist/zip-full.js +9463 -0
- package/node_modules/@zip.js/zip.js/dist/zip-full.min.js +1 -0
- package/node_modules/@zip.js/zip.js/dist/zip-no-worker-deflate.min.js +1 -0
- package/node_modules/@zip.js/zip.js/dist/zip-no-worker-inflate.min.js +1 -0
- package/node_modules/@zip.js/zip.js/dist/zip-no-worker.min.js +1 -0
- package/node_modules/@zip.js/zip.js/dist/zip.js +5240 -0
- package/node_modules/@zip.js/zip.js/dist/zip.min.js +1 -0
- package/node_modules/@zip.js/zip.js/index-fflate.js +82 -0
- package/node_modules/@zip.js/zip.js/index.cjs +11927 -0
- package/node_modules/@zip.js/zip.js/index.d.ts +2048 -0
- package/node_modules/@zip.js/zip.js/index.js +87 -0
- package/node_modules/@zip.js/zip.js/index.min.js +1 -0
- package/node_modules/@zip.js/zip.js/lib/core/codec-pool.js +127 -0
- package/node_modules/@zip.js/zip.js/lib/core/codec-worker.js +348 -0
- package/node_modules/@zip.js/zip.js/lib/core/configuration.js +127 -0
- package/node_modules/@zip.js/zip.js/lib/core/constants.js +114 -0
- package/node_modules/@zip.js/zip.js/lib/core/io.js +749 -0
- package/node_modules/@zip.js/zip.js/lib/core/streams/aes-crypto-stream.js +326 -0
- package/node_modules/@zip.js/zip.js/lib/core/streams/codec-stream.js +154 -0
- package/node_modules/@zip.js/zip.js/lib/core/streams/codecs/crc32.js +63 -0
- package/node_modules/@zip.js/zip.js/lib/core/streams/codecs/deflate.js +2063 -0
- package/node_modules/@zip.js/zip.js/lib/core/streams/codecs/inflate.js +2167 -0
- package/node_modules/@zip.js/zip.js/lib/core/streams/codecs/sjcl.js +827 -0
- package/node_modules/@zip.js/zip.js/lib/core/streams/common-crypto.js +55 -0
- package/node_modules/@zip.js/zip.js/lib/core/streams/crc32-stream.js +56 -0
- package/node_modules/@zip.js/zip.js/lib/core/streams/stream-adapter.js +55 -0
- package/node_modules/@zip.js/zip.js/lib/core/streams/zip-crypto-stream.js +162 -0
- package/node_modules/@zip.js/zip.js/lib/core/streams/zip-entry-stream.js +165 -0
- package/node_modules/@zip.js/zip.js/lib/core/util/cp437-decode.js +48 -0
- package/node_modules/@zip.js/zip.js/lib/core/util/decode-text.js +43 -0
- package/node_modules/@zip.js/zip.js/lib/core/util/default-mime-type.js +38 -0
- package/node_modules/@zip.js/zip.js/lib/core/util/encode-text.js +48 -0
- package/node_modules/@zip.js/zip.js/lib/core/util/mime-type.js +1639 -0
- package/node_modules/@zip.js/zip.js/lib/core/util/stream-codec-shim.js +91 -0
- package/node_modules/@zip.js/zip.js/lib/core/z-worker-core.js +176 -0
- package/node_modules/@zip.js/zip.js/lib/core/zip-entry.js +86 -0
- package/node_modules/@zip.js/zip.js/lib/core/zip-fs-core.js +865 -0
- package/node_modules/@zip.js/zip.js/lib/core/zip-reader.js +757 -0
- package/node_modules/@zip.js/zip.js/lib/core/zip-writer.js +1186 -0
- package/node_modules/@zip.js/zip.js/lib/z-worker-bootstrap-fflate.js +40 -0
- package/node_modules/@zip.js/zip.js/lib/z-worker-bootstrap-pako.js +39 -0
- package/node_modules/@zip.js/zip.js/lib/z-worker-fflate.js +40 -0
- package/node_modules/@zip.js/zip.js/lib/z-worker-inline-template.js +42 -0
- package/node_modules/@zip.js/zip.js/lib/z-worker-inline.js +1 -0
- package/node_modules/@zip.js/zip.js/lib/z-worker.js +38 -0
- package/node_modules/@zip.js/zip.js/lib/zip-data-uri.js +53 -0
- package/node_modules/@zip.js/zip.js/lib/zip-fflate-shim.js +37 -0
- package/node_modules/@zip.js/zip.js/lib/zip-fs.js +53 -0
- package/node_modules/@zip.js/zip.js/lib/zip-full-fflate.js +53 -0
- package/node_modules/@zip.js/zip.js/lib/zip-full.js +54 -0
- package/node_modules/@zip.js/zip.js/lib/zip-no-worker-deflate.js +42 -0
- package/node_modules/@zip.js/zip.js/lib/zip-no-worker-fflate-deflate.js +42 -0
- package/node_modules/@zip.js/zip.js/lib/zip-no-worker-fflate-inflate.js +42 -0
- package/node_modules/@zip.js/zip.js/lib/zip-no-worker-fflate.js +43 -0
- package/node_modules/@zip.js/zip.js/lib/zip-no-worker-inflate.js +42 -0
- package/node_modules/@zip.js/zip.js/lib/zip-no-worker.js +44 -0
- package/node_modules/@zip.js/zip.js/lib/zip.js +52 -0
- package/node_modules/@zip.js/zip.js/package.json +86 -0
- package/package.json +43 -0
- package/prisma-gen/default.d.ts +1 -0
- package/prisma-gen/default.js +1 -0
- package/prisma-gen/edge.d.ts +1 -0
- package/prisma-gen/edge.js +242 -0
- package/prisma-gen/index-browser.js +236 -0
- package/prisma-gen/index.d.ts +13248 -0
- package/prisma-gen/index.js +265 -0
- package/prisma-gen/runtime/edge-esm.js +28 -0
- package/prisma-gen/runtime/edge.js +28 -0
- package/prisma-gen/runtime/index-browser.d.ts +365 -0
- package/prisma-gen/runtime/index-browser.js +13 -0
- package/prisma-gen/runtime/library.d.ts +3168 -0
- package/prisma-gen/runtime/library.js +140 -0
- package/prisma-gen/runtime/wasm.js +29 -0
- package/prisma-gen/wasm.d.ts +1 -0
- package/prisma-gen/wasm.js +236 -0
- package/s3.d.ts +14 -0
- package/s3.js +76 -0
- package/schema.prisma +154 -0
- package/uploads.d.ts +54 -0
- package/uploads.js +141 -0
package/actions.d.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
export interface InitDbOptions {
|
|
2
|
+
/**
|
|
3
|
+
* The path to the source database to copy the schema from.
|
|
4
|
+
*/
|
|
5
|
+
source?: string;
|
|
6
|
+
/**
|
|
7
|
+
* The languages to copy from the source database. If not specified, then all languages will be copied.
|
|
8
|
+
*/
|
|
9
|
+
language?: string[];
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Initializes a new Bible API DB.
|
|
13
|
+
* @param dbPath The path to the database. If null or empty, then the "bible-api.db" will be used from the current working directory.
|
|
14
|
+
* @param options The options for the initialization.
|
|
15
|
+
*/
|
|
16
|
+
export declare function initDb(dbPath: string | null, options: InitDbOptions): Promise<void>;
|
|
17
|
+
export interface ImportTranslationOptions {
|
|
18
|
+
/**
|
|
19
|
+
* Whether to forcibly import the translations, even if they have already been imported.
|
|
20
|
+
*/
|
|
21
|
+
overwrite?: boolean;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Imports a translation from the given directory into the database in the current working directory.
|
|
25
|
+
* @param dir The directory that the translation is located in.
|
|
26
|
+
* @param dirs Any extra directories that should be imported.
|
|
27
|
+
* @param options The options for the import.
|
|
28
|
+
*/
|
|
29
|
+
export declare function importTranslation(dir: string, dirs: string[], options: ImportTranslationOptions): Promise<void>;
|
|
30
|
+
/**
|
|
31
|
+
* Imports all the translations from the given directory into the database in the current working directory.
|
|
32
|
+
* @param dir The directory that the translations are located in.
|
|
33
|
+
* @param options The options.
|
|
34
|
+
*/
|
|
35
|
+
export declare function importTranslations(dir: string, options: ImportTranslationOptions): Promise<void>;
|
|
36
|
+
export interface FetchTranslationsOptions {
|
|
37
|
+
/**
|
|
38
|
+
* Fetch all translations. If omitted, only undownloaded translations will be fetched.
|
|
39
|
+
*/
|
|
40
|
+
all?: boolean;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Fetches the specified translations from fetch.bible and places them in the given directory.
|
|
44
|
+
* @param dir The directory that the translations should be placed in.
|
|
45
|
+
* @param translations The translations that should be downloaded. If not specified, then all translations will be downloaded.
|
|
46
|
+
* @param options The options.
|
|
47
|
+
*/
|
|
48
|
+
export declare function fetchTranslations(dir: string, translations?: string[], options?: FetchTranslationsOptions): Promise<void>;
|
|
49
|
+
/**
|
|
50
|
+
* Fetches the specified audio translations and places them in the given directory.
|
|
51
|
+
* Translations should be in the format "translationId/audioId". e.g. "BSB/gilbert"
|
|
52
|
+
* @param dir The directory that the translations should be placed in.
|
|
53
|
+
* @param translations The translations that should be downloaded.
|
|
54
|
+
* @param options The options.
|
|
55
|
+
*/
|
|
56
|
+
export declare function fetchAudio(dir: string, translations: string[], options?: FetchTranslationsOptions): Promise<void>;
|
|
57
|
+
//# sourceMappingURL=actions.d.ts.map
|
package/actions.js
ADDED
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
+
};
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
exports.initDb = initDb;
|
|
30
|
+
exports.importTranslation = importTranslation;
|
|
31
|
+
exports.importTranslations = importTranslations;
|
|
32
|
+
exports.fetchTranslations = fetchTranslations;
|
|
33
|
+
exports.fetchAudio = fetchAudio;
|
|
34
|
+
const node_path_1 = __importStar(require("node:path"));
|
|
35
|
+
const database = __importStar(require("./db"));
|
|
36
|
+
const better_sqlite3_1 = __importDefault(require("better-sqlite3"));
|
|
37
|
+
const linkedom_1 = require("linkedom");
|
|
38
|
+
const promises_1 = require("node:fs/promises");
|
|
39
|
+
const fetch_client_1 = require("@gracious.tech/fetch-client");
|
|
40
|
+
const utils_1 = require("@helloao/tools/utils");
|
|
41
|
+
const fs_extra_1 = require("fs-extra");
|
|
42
|
+
const audio_1 = require("@helloao/tools/generation/audio");
|
|
43
|
+
const book_order_1 = require("@helloao/tools/generation/book-order");
|
|
44
|
+
const downloads_1 = require("./downloads");
|
|
45
|
+
/**
|
|
46
|
+
* Initializes a new Bible API DB.
|
|
47
|
+
* @param dbPath The path to the database. If null or empty, then the "bible-api.db" will be used from the current working directory.
|
|
48
|
+
* @param options The options for the initialization.
|
|
49
|
+
*/
|
|
50
|
+
async function initDb(dbPath, options) {
|
|
51
|
+
console.log('Initializing new Bible API DB...');
|
|
52
|
+
if (options.source) {
|
|
53
|
+
const db = new better_sqlite3_1.default(database.getDbPath(dbPath), {});
|
|
54
|
+
const sourcePath = node_path_1.default.resolve(options.source);
|
|
55
|
+
try {
|
|
56
|
+
console.log('Copying schema from source DB...');
|
|
57
|
+
if (options.language) {
|
|
58
|
+
console.log('Copying only the following languages:', options.language);
|
|
59
|
+
const languages = `(${options.language.map((l) => `'${l}'`).join(', ')})`;
|
|
60
|
+
db.exec(`
|
|
61
|
+
ATTACH DATABASE "${sourcePath}" AS source;
|
|
62
|
+
|
|
63
|
+
CREATE TABLE "_prisma_migrations" AS SELECT * FROM source._prisma_migrations;
|
|
64
|
+
|
|
65
|
+
CREATE TABLE "Translation" AS SELECT * FROM source.Translation
|
|
66
|
+
WHERE language IN ${languages};
|
|
67
|
+
|
|
68
|
+
CREATE TABLE "Book" AS SELECT * FROM source.Book
|
|
69
|
+
INNER JOIN source.Translation ON source.Translation.id = source.Book.translationId
|
|
70
|
+
WHERE source.Translation.language IN ${languages};
|
|
71
|
+
|
|
72
|
+
CREATE TABLE "Chapter" AS SELECT * FROM source.Chapter
|
|
73
|
+
INNER JOIN source.Translation ON source.Translation.id = source.Chapter.translationId
|
|
74
|
+
WHERE source.Translation.language IN ${languages};
|
|
75
|
+
|
|
76
|
+
CREATE TABLE "ChapterVerse" AS SELECT * FROM source.ChapterVerse
|
|
77
|
+
INNER JOIN source.Translation ON source.Translation.id = source.ChapterVerse.translationId
|
|
78
|
+
WHERE source.Translation.language IN ${languages};
|
|
79
|
+
|
|
80
|
+
CREATE TABLE "ChapterFootnote" AS SELECT * FROM source.ChapterFootnote
|
|
81
|
+
INNER JOIN source.Translation ON source.Translation.id = source.ChapterFootnote.translationId
|
|
82
|
+
WHERE source.Translation.language IN ${languages};
|
|
83
|
+
|
|
84
|
+
CREATE TABLE "ChapterAudioUrl" AS SELECT * FROM source.ChapterAudioUrl
|
|
85
|
+
INNER JOIN source.Translation ON source.Translation.id = source.ChapterAudioUrl.translationId
|
|
86
|
+
WHERE source.Translation.language IN ${languages};
|
|
87
|
+
`);
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
db.exec(`
|
|
91
|
+
ATTACH DATABASE "${sourcePath}" AS source;
|
|
92
|
+
|
|
93
|
+
CREATE TABLE "_prisma_migrations" AS SELECT * FROM source._prisma_migrations;
|
|
94
|
+
CREATE TABLE "Translation" AS SELECT * FROM source.Translation;
|
|
95
|
+
CREATE TABLE "Book" AS SELECT * FROM source.Book;
|
|
96
|
+
CREATE TABLE "Chapter" AS SELECT * FROM source.Chapter;
|
|
97
|
+
CREATE TABLE "ChapterVerse" AS SELECT * FROM source.ChapterVerse;
|
|
98
|
+
CREATE TABLE "ChapterFootnote" AS SELECT * FROM source.ChapterFootnote;
|
|
99
|
+
CREATE TABLE "ChapterAudioUrl" AS SELECT * FROM source.ChapterAudioUrl;
|
|
100
|
+
`);
|
|
101
|
+
}
|
|
102
|
+
console.log('Done.');
|
|
103
|
+
}
|
|
104
|
+
finally {
|
|
105
|
+
db.close();
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
const db = await database.getDb(database.getDbPath(dbPath));
|
|
110
|
+
db.close();
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Imports a translation from the given directory into the database in the current working directory.
|
|
115
|
+
* @param dir The directory that the translation is located in.
|
|
116
|
+
* @param dirs Any extra directories that should be imported.
|
|
117
|
+
* @param options The options for the import.
|
|
118
|
+
*/
|
|
119
|
+
async function importTranslation(dir, dirs, options) {
|
|
120
|
+
const parser = new linkedom_1.DOMParser();
|
|
121
|
+
globalThis.DOMParser = linkedom_1.DOMParser;
|
|
122
|
+
globalThis.Element = linkedom_1.Element;
|
|
123
|
+
globalThis.Node = linkedom_1.Node;
|
|
124
|
+
const db = await database.getDbFromDir(process.cwd());
|
|
125
|
+
try {
|
|
126
|
+
await database.importTranslations(db, [dir, ...dirs], parser, !!options.overwrite);
|
|
127
|
+
}
|
|
128
|
+
finally {
|
|
129
|
+
db.close();
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Imports all the translations from the given directory into the database in the current working directory.
|
|
134
|
+
* @param dir The directory that the translations are located in.
|
|
135
|
+
* @param options The options.
|
|
136
|
+
*/
|
|
137
|
+
async function importTranslations(dir, options) {
|
|
138
|
+
const parser = new linkedom_1.DOMParser();
|
|
139
|
+
globalThis.DOMParser = linkedom_1.DOMParser;
|
|
140
|
+
globalThis.Element = linkedom_1.Element;
|
|
141
|
+
globalThis.Node = linkedom_1.Node;
|
|
142
|
+
const db = await database.getDbFromDir(process.cwd());
|
|
143
|
+
try {
|
|
144
|
+
const files = await (0, promises_1.readdir)(dir);
|
|
145
|
+
const translationDirs = files.map(f => node_path_1.default.resolve(dir, f));
|
|
146
|
+
console.log(`Importing ${translationDirs.length} translations`);
|
|
147
|
+
await database.importTranslations(db, translationDirs, parser, !!options.overwrite);
|
|
148
|
+
}
|
|
149
|
+
finally {
|
|
150
|
+
db.close();
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Fetches the specified translations from fetch.bible and places them in the given directory.
|
|
155
|
+
* @param dir The directory that the translations should be placed in.
|
|
156
|
+
* @param translations The translations that should be downloaded. If not specified, then all translations will be downloaded.
|
|
157
|
+
* @param options The options.
|
|
158
|
+
*/
|
|
159
|
+
async function fetchTranslations(dir, translations, options = {}) {
|
|
160
|
+
const translationsSet = new Set(translations);
|
|
161
|
+
const client = new fetch_client_1.BibleClient({
|
|
162
|
+
remember_fetches: false,
|
|
163
|
+
});
|
|
164
|
+
const collection = await client.fetch_collection();
|
|
165
|
+
const collectionTranslations = collection.get_translations();
|
|
166
|
+
console.log(`Discovered ${collectionTranslations.length} translations`);
|
|
167
|
+
const filtered = translations && translations.length <= 0 ? collectionTranslations : collectionTranslations.filter(t => translationsSet.has(t.id));
|
|
168
|
+
let batches = [];
|
|
169
|
+
while (filtered.length > 0) {
|
|
170
|
+
batches.push(filtered.splice(0, 10));
|
|
171
|
+
}
|
|
172
|
+
console.log(`Downloading ${filtered.length} translations in ${batches.length} batches`);
|
|
173
|
+
for (let i = 0; i < batches.length; i++) {
|
|
174
|
+
const batch = batches[i];
|
|
175
|
+
console.log(`Downloading batch ${i + 1} of ${batches.length}`);
|
|
176
|
+
const translations = await Promise.all(batch.map(async (t) => {
|
|
177
|
+
const id = (0, utils_1.getTranslationId)(t.id);
|
|
178
|
+
const translation = {
|
|
179
|
+
id,
|
|
180
|
+
name: (0, utils_1.getFirstNonEmpty)(t.name_local, t.name_english, t.name_abbrev),
|
|
181
|
+
direction: (0, utils_1.getFirstNonEmpty)(t.direction, 'ltr'),
|
|
182
|
+
englishName: (0, utils_1.getFirstNonEmpty)(t.name_english, t.name_abbrev, t.name_local),
|
|
183
|
+
language: (0, utils_1.normalizeLanguage)(t.language),
|
|
184
|
+
licenseUrl: t.attribution_url,
|
|
185
|
+
shortName: (0, utils_1.getFirstNonEmpty)(t.name_abbrev, id),
|
|
186
|
+
website: t.attribution_url,
|
|
187
|
+
};
|
|
188
|
+
const books = await Promise.all(collection.get_books(t.id).map(async (b) => {
|
|
189
|
+
const name = `${b.id}.usx`;
|
|
190
|
+
if (!options.all && await (0, fs_extra_1.exists)(node_path_1.default.resolve(dir, translation.id, name))) {
|
|
191
|
+
return null;
|
|
192
|
+
}
|
|
193
|
+
const content = await collection.fetch_book(t.id, b.id, 'usx');
|
|
194
|
+
const contentString = content.get_whole();
|
|
195
|
+
const file = {
|
|
196
|
+
fileType: 'usx',
|
|
197
|
+
content: contentString,
|
|
198
|
+
metadata: {
|
|
199
|
+
translation
|
|
200
|
+
},
|
|
201
|
+
name,
|
|
202
|
+
};
|
|
203
|
+
return file;
|
|
204
|
+
}));
|
|
205
|
+
return {
|
|
206
|
+
translation,
|
|
207
|
+
books,
|
|
208
|
+
};
|
|
209
|
+
}));
|
|
210
|
+
console.log(`Writing batch ${i + 1} of ${batches.length}`);
|
|
211
|
+
let promises = [];
|
|
212
|
+
for (let { translation, books } of translations) {
|
|
213
|
+
for (let book of books) {
|
|
214
|
+
if (!book) {
|
|
215
|
+
continue;
|
|
216
|
+
}
|
|
217
|
+
if (!book.name) {
|
|
218
|
+
throw new Error('Book name is required');
|
|
219
|
+
}
|
|
220
|
+
const fullPath = node_path_1.default.resolve(dir, translation.id, book.name);
|
|
221
|
+
await (0, promises_1.mkdir)(node_path_1.default.dirname(fullPath), { recursive: true });
|
|
222
|
+
const promise = (0, promises_1.writeFile)(fullPath, book.content);
|
|
223
|
+
promises.push(promise);
|
|
224
|
+
}
|
|
225
|
+
const translationPath = node_path_1.default.resolve(dir, translation.id, 'metadata.json');
|
|
226
|
+
await (0, promises_1.mkdir)(node_path_1.default.dirname(translationPath), { recursive: true });
|
|
227
|
+
const translationData = JSON.stringify(translation, null, 2);
|
|
228
|
+
promises.push((0, promises_1.writeFile)(translationPath, translationData));
|
|
229
|
+
}
|
|
230
|
+
await Promise.all(promises);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* Fetches the specified audio translations and places them in the given directory.
|
|
235
|
+
* Translations should be in the format "translationId/audioId". e.g. "BSB/gilbert"
|
|
236
|
+
* @param dir The directory that the translations should be placed in.
|
|
237
|
+
* @param translations The translations that should be downloaded.
|
|
238
|
+
* @param options The options.
|
|
239
|
+
*/
|
|
240
|
+
async function fetchAudio(dir, translations, options = {}) {
|
|
241
|
+
for (let translation of translations) {
|
|
242
|
+
const [translationId, reader] = translation.split('/');
|
|
243
|
+
const generator = audio_1.KNOWN_AUDIO_TRANSLATIONS.get(translationId)?.get(reader);
|
|
244
|
+
if (!generator) {
|
|
245
|
+
console.warn('Unknown translation:', translation);
|
|
246
|
+
continue;
|
|
247
|
+
}
|
|
248
|
+
for (let [bookId, chapters] of book_order_1.bookChapterCountMap) {
|
|
249
|
+
for (let chapter = 1; chapter <= chapters; chapter++) {
|
|
250
|
+
const url = generator(bookId, chapter);
|
|
251
|
+
const ext = (0, node_path_1.extname)(url);
|
|
252
|
+
const [translationId, reader] = translation.split('/');
|
|
253
|
+
const name = `${chapter}.${reader}${ext}`;
|
|
254
|
+
const fullPath = node_path_1.default.resolve(dir, 'audio', translationId, bookId, name);
|
|
255
|
+
if (!options.all && await (0, fs_extra_1.exists)(fullPath)) {
|
|
256
|
+
continue;
|
|
257
|
+
}
|
|
258
|
+
await (0, downloads_1.downloadFile)(url, fullPath);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
}
|
package/cli.d.ts
ADDED
package/cli.js
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const commander_1 = require("commander");
|
|
7
|
+
const path_1 = __importDefault(require("path"));
|
|
8
|
+
const promises_1 = require("fs/promises");
|
|
9
|
+
const linkedom_1 = require("linkedom");
|
|
10
|
+
const downloads_1 = require("./downloads");
|
|
11
|
+
const uploads_1 = require("./uploads");
|
|
12
|
+
const actions_1 = require("./actions");
|
|
13
|
+
const files_1 = require("./files");
|
|
14
|
+
const dataset_1 = require("@helloao/tools/generation/dataset");
|
|
15
|
+
const iterators_1 = require("@helloao/tools/parser/iterators");
|
|
16
|
+
async function start() {
|
|
17
|
+
const parser = new linkedom_1.DOMParser();
|
|
18
|
+
globalThis.DOMParser = linkedom_1.DOMParser;
|
|
19
|
+
globalThis.Element = linkedom_1.Element;
|
|
20
|
+
globalThis.Node = linkedom_1.Node;
|
|
21
|
+
const program = new commander_1.Command();
|
|
22
|
+
program.name('bible-api')
|
|
23
|
+
.description('A CLI for managing a Bible API.')
|
|
24
|
+
.version('0.0.1');
|
|
25
|
+
program.command('init [path]')
|
|
26
|
+
.description('Initialize a new Bible API DB.')
|
|
27
|
+
.option('--source <path>', 'The source database to copy from.')
|
|
28
|
+
.option('--language <languages...>', 'The language(s) that the database should be initialized with.')
|
|
29
|
+
.action(async (dbPath, options) => {
|
|
30
|
+
await (0, actions_1.initDb)(dbPath, options);
|
|
31
|
+
});
|
|
32
|
+
program.command('import-translation <dir> [dirs...]')
|
|
33
|
+
.description('Imports a translation from the given directory into the database.')
|
|
34
|
+
.option('--overwrite', 'Whether to overwrite existing files.')
|
|
35
|
+
.action(async (dir, dirs, options) => {
|
|
36
|
+
await (0, actions_1.importTranslation)(dir, dirs, options);
|
|
37
|
+
});
|
|
38
|
+
program.command('import-translations <dir>')
|
|
39
|
+
.description('Imports all translations from the given directory into the database.')
|
|
40
|
+
.option('--overwrite', 'Whether to overwrite existing files.')
|
|
41
|
+
.action(async (dir, options) => {
|
|
42
|
+
await (0, actions_1.importTranslations)(dir, options);
|
|
43
|
+
});
|
|
44
|
+
program.command('generate-translation-files <input> <dir>')
|
|
45
|
+
.description('Generates API files from the given input translation.')
|
|
46
|
+
.option('--batch-size <size>', 'The number of translations to generate API files for in each batch.', '50')
|
|
47
|
+
.option('--translations <translations...>', 'The translations to generate API files for.')
|
|
48
|
+
.option('--overwrite', 'Whether to overwrite existing files.')
|
|
49
|
+
.option('--overwrite-common-files', 'Whether to overwrite only common files.')
|
|
50
|
+
.option('--file-pattern <pattern>', 'The file pattern regex that should be used to filter the files that are generated.')
|
|
51
|
+
.option('--use-common-name', 'Whether to use the common name for the book chapter API link. If false, then book IDs are used.')
|
|
52
|
+
.option('--generate-audio-files', 'Whether to replace the audio URLs in the dataset with ones that are hosted locally.')
|
|
53
|
+
.option('--profile <profile>', 'The AWS profile to use for uploading to S3.')
|
|
54
|
+
.option('--pretty', 'Whether to generate pretty-printed JSON files.')
|
|
55
|
+
.action(async (input, dest, options) => {
|
|
56
|
+
const parser = new linkedom_1.DOMParser();
|
|
57
|
+
globalThis.DOMParser = linkedom_1.DOMParser;
|
|
58
|
+
globalThis.Element = linkedom_1.Element;
|
|
59
|
+
globalThis.Node = linkedom_1.Node;
|
|
60
|
+
const files = await (0, files_1.loadTranslationFiles)(path_1.default.resolve(input));
|
|
61
|
+
const dataset = (0, dataset_1.generateDataset)(files, parser);
|
|
62
|
+
await (0, uploads_1.uploadApiFiles)(path_1.default.resolve(dest), options, (0, iterators_1.toAsyncIterable)([dataset]));
|
|
63
|
+
});
|
|
64
|
+
program.command('generate-translations-files <input> <dir>')
|
|
65
|
+
.description('Generates API files from the given input translations.')
|
|
66
|
+
.option('--batch-size <size>', 'The number of translations to generate API files for in each batch.', '50')
|
|
67
|
+
.option('--translations <translations...>', 'The translations to generate API files for.')
|
|
68
|
+
.option('--overwrite', 'Whether to overwrite existing files.')
|
|
69
|
+
.option('--overwrite-common-files', 'Whether to overwrite only common files.')
|
|
70
|
+
.option('--file-pattern <pattern>', 'The file pattern regex that should be used to filter the files that are uploaded.')
|
|
71
|
+
.option('--use-common-name', 'Whether to use the common name for the book chapter API link. If false, then book IDs are used.')
|
|
72
|
+
.option('--generate-audio-files', 'Whether to replace the audio URLs in the dataset with ones that are hosted locally.')
|
|
73
|
+
.option('--profile <profile>', 'The AWS profile to use for uploading to S3.')
|
|
74
|
+
.option('--pretty', 'Whether to generate pretty-printed JSON files.')
|
|
75
|
+
.action(async (input, dest, options) => {
|
|
76
|
+
const parser = new linkedom_1.DOMParser();
|
|
77
|
+
globalThis.DOMParser = linkedom_1.DOMParser;
|
|
78
|
+
globalThis.Element = linkedom_1.Element;
|
|
79
|
+
globalThis.Node = linkedom_1.Node;
|
|
80
|
+
const dirs = await (0, promises_1.readdir)(path_1.default.resolve(input));
|
|
81
|
+
const batchSize = parseInt(options.batchSize);
|
|
82
|
+
for (let b of (0, iterators_1.batch)(dirs, batchSize)) {
|
|
83
|
+
const files = await (0, files_1.loadTranslationsFiles)(b);
|
|
84
|
+
const dataset = (0, dataset_1.generateDataset)(files, parser);
|
|
85
|
+
await (0, uploads_1.uploadApiFiles)(dest, options, (0, iterators_1.toAsyncIterable)([dataset]));
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
program.command('upload-api-files')
|
|
89
|
+
.argument('<dest>', 'The destination to upload the API files to.')
|
|
90
|
+
.description('Uploads API files to the specified destination. For S3, use the format s3://bucket-name/path/to/folder.')
|
|
91
|
+
.option('--batch-size <size>', 'The number of translations to generate API files for in each batch.', '50')
|
|
92
|
+
.option('--translations <translations...>', 'The translations to generate API files for.')
|
|
93
|
+
.option('--overwrite', 'Whether to overwrite existing files.')
|
|
94
|
+
.option('--overwrite-common-files', 'Whether to overwrite only common files.')
|
|
95
|
+
.option('--file-pattern <pattern>', 'The file pattern regex that should be used to filter the files that are uploaded.')
|
|
96
|
+
.option('--use-common-name', 'Whether to use the common name for the book chapter API link. If false, then book IDs are used.')
|
|
97
|
+
.option('--generate-audio-files', 'Whether to replace the audio URLs in the dataset with ones that are hosted locally.')
|
|
98
|
+
.option('--profile <profile>', 'The AWS profile to use for uploading to S3.')
|
|
99
|
+
.option('--pretty', 'Whether to generate pretty-printed JSON files.')
|
|
100
|
+
.action(async (dest, options) => {
|
|
101
|
+
await (0, uploads_1.uploadApiFilesFromDatabase)(dest, options);
|
|
102
|
+
});
|
|
103
|
+
program.command('fetch-translations <dir> [translations...]')
|
|
104
|
+
.description('Fetches the specified translations from fetch.bible and places them in the given directory.')
|
|
105
|
+
.option('-a, --all', 'Fetch all translations. If omitted, only undownloaded translations will be fetched.')
|
|
106
|
+
.action(async (dir, translations, options) => {
|
|
107
|
+
await (0, actions_1.fetchTranslations)(dir, translations, options);
|
|
108
|
+
});
|
|
109
|
+
program.command('fetch-audio <dir> [translations...]')
|
|
110
|
+
.description('Fetches the specified audio translations and places them in the given directory.\nTranslations should be in the format "translationId/audioId". e.g. "BSB/gilbert"')
|
|
111
|
+
.option('-a, --all', 'Fetch all translations. If omitted, only undownloaded translations will be fetched.')
|
|
112
|
+
.action(async (dir, translations, options) => {
|
|
113
|
+
await (0, actions_1.fetchAudio)(dir, translations, options);
|
|
114
|
+
});
|
|
115
|
+
program.command('fetch-bible-metadata <dir>')
|
|
116
|
+
.description('Fetches the Theographic bible metadata and places it in the given directory.')
|
|
117
|
+
.action(async (dir) => {
|
|
118
|
+
let files = [
|
|
119
|
+
'books.json',
|
|
120
|
+
'chapters.json',
|
|
121
|
+
'easton.json',
|
|
122
|
+
'events.json',
|
|
123
|
+
'people.json',
|
|
124
|
+
'peopleGroups.json',
|
|
125
|
+
'periods.json',
|
|
126
|
+
'places.json',
|
|
127
|
+
'verses.json',
|
|
128
|
+
];
|
|
129
|
+
await (0, promises_1.mkdir)(dir, { recursive: true });
|
|
130
|
+
let promises = files.map(async (file) => {
|
|
131
|
+
const url = `https://raw.githubusercontent.com/robertrouse/theographic-bible-metadata/master/json/${file}`;
|
|
132
|
+
const fullPath = path_1.default.resolve(dir, file);
|
|
133
|
+
await (0, downloads_1.downloadFile)(url, fullPath);
|
|
134
|
+
});
|
|
135
|
+
await Promise.all(promises);
|
|
136
|
+
});
|
|
137
|
+
await program.parseAsync(process.argv);
|
|
138
|
+
}
|
|
139
|
+
start();
|
package/db.d.ts
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { PrismaClient } from "./prisma-gen";
|
|
2
|
+
import { Database } from 'better-sqlite3';
|
|
3
|
+
import { DatasetOutput, DatasetTranslation, DatasetTranslationBook } from "@helloao/tools/generation/dataset";
|
|
4
|
+
import { InputFile, TranslationBookChapter } from "@helloao/tools/generation";
|
|
5
|
+
import { GenerateApiOptions } from "@helloao/tools/generation/api";
|
|
6
|
+
import { DOMParser } from "linkedom";
|
|
7
|
+
import { Readable } from "stream";
|
|
8
|
+
/**
|
|
9
|
+
* Imports the translations from the given directories into the database.
|
|
10
|
+
* @param db The database to import the translations into.
|
|
11
|
+
* @param dirs The directories to import the translations from.
|
|
12
|
+
* @param parser The DOM parser that should be used for USX files.
|
|
13
|
+
* @param overwrite Whether to force a reload of the translations.
|
|
14
|
+
*/
|
|
15
|
+
export declare function importTranslations(db: Database, dirs: string[], parser: DOMParser, overwrite: boolean): Promise<void>;
|
|
16
|
+
/**
|
|
17
|
+
* Imports a batch of translations from the given directories into the database.
|
|
18
|
+
* @param db The database to import the translations into.
|
|
19
|
+
* @param dirs The directories that contain the translations.
|
|
20
|
+
* @param parser The DOM parser that should be used for USX files.
|
|
21
|
+
* @param overwrite Whether to force a reload of the translations.
|
|
22
|
+
*/
|
|
23
|
+
export declare function importTranslationBatch(db: Database, dirs: string[], parser: DOMParser, overwrite: boolean): Promise<void>;
|
|
24
|
+
/**
|
|
25
|
+
* Parses and imports the given files into the database.
|
|
26
|
+
* @param db The database to import the files into.
|
|
27
|
+
* @param files The files that should be parsed.
|
|
28
|
+
* @param parser The DOM parser that should be used for USX files.
|
|
29
|
+
* @param overwrite Whether to force a reload of the translations.
|
|
30
|
+
*/
|
|
31
|
+
export declare function importTranslationFileBatch(db: Database, files: InputFile[], parser: DOMParser, overwrite: boolean): Promise<void>;
|
|
32
|
+
/**
|
|
33
|
+
* Filters the given input files to only include those that have changed.
|
|
34
|
+
* @param db The database to check for changes.
|
|
35
|
+
* @param files The files to filter.
|
|
36
|
+
*/
|
|
37
|
+
export declare function getChangedOrNewInputFiles(db: Database, files: InputFile[]): InputFile[];
|
|
38
|
+
export declare function insertFileMetadata(db: Database, files: InputFile[]): void;
|
|
39
|
+
export declare function insertTranslations(db: Database, translations: DatasetTranslation[]): void;
|
|
40
|
+
export declare function insertTranslationBooks(db: Database, translation: DatasetTranslation, translationBooks: DatasetTranslationBook[]): void;
|
|
41
|
+
export declare function insertTranslationContent(db: Database, translation: DatasetTranslation, book: DatasetTranslationBook, chapters: TranslationBookChapter[]): void;
|
|
42
|
+
export declare function getDbPathFromDir(dir: string): string;
|
|
43
|
+
export declare function getDbPath(p: string | null): string;
|
|
44
|
+
export declare function getPrismaDbFromDir(dir: string): PrismaClient<{
|
|
45
|
+
datasources: {
|
|
46
|
+
db: {
|
|
47
|
+
url: string;
|
|
48
|
+
};
|
|
49
|
+
};
|
|
50
|
+
}, never, import("prisma-gen/runtime/library").DefaultArgs>;
|
|
51
|
+
export declare function getDbFromDir(dir: string): Promise<Database>;
|
|
52
|
+
export declare function getDb(dbPath: string): Promise<Database>;
|
|
53
|
+
export interface SerializedFile {
|
|
54
|
+
path: string;
|
|
55
|
+
content: string | Readable;
|
|
56
|
+
/**
|
|
57
|
+
* Gets the base64-encoded SHA256 hash of the content of the file.
|
|
58
|
+
*/
|
|
59
|
+
sha256?(): string;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Loads the datasets from the database in a series of batches.
|
|
63
|
+
* @param db The database.
|
|
64
|
+
* @param translationsPerBatch The number of translations to load per batch.
|
|
65
|
+
* @param translationsToLoad The list of translations to load. If not provided, all translations will be loaded.
|
|
66
|
+
*/
|
|
67
|
+
export declare function loadDatasets(db: PrismaClient, translationsPerBatch?: number, translationsToLoad?: string[]): AsyncGenerator<DatasetOutput>;
|
|
68
|
+
export interface SerializeApiOptions extends GenerateApiOptions {
|
|
69
|
+
/**
|
|
70
|
+
* Whether the output should be pretty-printed.
|
|
71
|
+
*/
|
|
72
|
+
pretty?: boolean;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Generates and serializes the API files for the dataset that is stored in the database.
|
|
76
|
+
* Yields each batch of serialized files.
|
|
77
|
+
* @param db The database that the dataset should be loaded from.
|
|
78
|
+
* @param options The options to use for generating the API.
|
|
79
|
+
* @param translationsPerBatch The number of translations that should be loaded and written per batch.
|
|
80
|
+
* @param translations The list of translations that should be loaded. If not provided, all translations will be loaded.
|
|
81
|
+
*/
|
|
82
|
+
export declare function serializeFilesForDataset(db: PrismaClient, options: SerializeApiOptions, translationsPerBatch?: number, translations?: string[]): AsyncGenerator<SerializedFile[]>;
|
|
83
|
+
/**
|
|
84
|
+
* Serializes the API files for the given datasets.
|
|
85
|
+
* @param datasets The dataasets to serialize.
|
|
86
|
+
* @param options The options to use for serializing the files.
|
|
87
|
+
*/
|
|
88
|
+
export declare function serializeFiles(datasets: AsyncIterable<DatasetOutput>, options: SerializeApiOptions): AsyncGenerator<SerializedFile[]>;
|
|
89
|
+
/**
|
|
90
|
+
* Defines an interface that contains information about a serialized file.
|
|
91
|
+
*/
|
|
92
|
+
export interface Uploader {
|
|
93
|
+
/**
|
|
94
|
+
* Gets the ideal batch size for the uploader.
|
|
95
|
+
* Null if the uploader does not need batching.
|
|
96
|
+
*/
|
|
97
|
+
idealBatchSize: number | null;
|
|
98
|
+
/**
|
|
99
|
+
* Uploads the given file.
|
|
100
|
+
* @param file The file to upload.
|
|
101
|
+
* @param overwrite Whether the file should be overwritten if it already exists.
|
|
102
|
+
* @returns True if the file was uploaded. False if the file was skipped due to already existing.
|
|
103
|
+
*/
|
|
104
|
+
upload(file: SerializedFile, overwrite: boolean): Promise<boolean>;
|
|
105
|
+
/**
|
|
106
|
+
* Disposes resources that the uploader uses.
|
|
107
|
+
*/
|
|
108
|
+
dispose?(): Promise<void>;
|
|
109
|
+
}
|
|
110
|
+
//# sourceMappingURL=db.d.ts.map
|