@helloao/cli 0.0.4 → 0.0.5

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/README.md CHANGED
@@ -1,37 +1,126 @@
1
- ## Hello AO CLI
2
-
3
- A Command Line Interface (CLI) that makes it easy to generate and manage your own [Free Use Bible API](https://bible.helloao.org/).
4
-
5
- Additionally, it includes many functions and utilities that can make working with commonly formatted Bible data much easier.
6
-
7
- ### Features
8
-
9
- - Supports [USFM](https://ubsicap.github.io/usfm/), [USX](https://ubsicap.github.io/usx/), and Codex (A JSON format).
10
- - Download over 1000 Bible translations from [fetch.bible](https://fetch.bible/).
11
- - Import Bible translations into a SQLite database.
12
- - Upload to S3, a zip file, or a local directory.
13
-
14
- ### Usage
15
-
16
- ```
17
- Usage: helloao [options] [command]
18
-
19
- A CLI for managing a Free Use Bible API.
20
-
21
- Options:
22
- -V, --version output the version number
23
- -h, --help display help for command
24
-
25
- Commands:
26
- init [options] [path] Initialize a new Bible API DB.
27
- import-translation [options] <dir> [dirs...] Imports a translation from the given directory into the database.
28
- import-translations [options] <dir> Imports all translations from the given directory into the database.
29
- generate-translation-files [options] <input> <dir> Generates API files from the given input translation.
30
- generate-translations-files [options] <input> <dir> Generates API files from the given input translations.
31
- upload-api-files [options] <dest> Uploads API files to the specified destination. For S3, use the format s3://bucket-name/path/to/folder.
32
- fetch-translations [options] <dir> [translations...] Fetches the specified translations from fetch.bible and places them in the given directory.
33
- fetch-audio [options] <dir> [translations...] Fetches the specified audio translations and places them in the given directory.
34
- Translations should be in the format "translationId/audioId". e.g. "BSB/gilbert"
35
- fetch-bible-metadata <dir> Fetches the Theographic bible metadata and places it in the given directory.
36
- help [command] display help for command
1
+ ## Hello AO CLI
2
+
3
+ A Command Line Interface (CLI) that makes it easy to generate and manage your own [Free Use Bible API](https://bible.helloao.org/).
4
+
5
+ Additionally, it includes many functions and utilities that can make working with commonly formatted Bible data much easier.
6
+
7
+ ### Features
8
+
9
+ - Supports [USFM](https://ubsicap.github.io/usfm/), [USX](https://ubsicap.github.io/usx/), and Codex (A JSON format).
10
+ - Download over 1000 Bible translations from [fetch.bible](https://fetch.bible/).
11
+ - Import Bible translations into a SQLite database.
12
+ - Upload to S3, a zip file, or a local directory.
13
+
14
+ ### Usage
15
+
16
+ ```
17
+ Usage: helloao [options] [command]
18
+
19
+ A CLI for managing a Free Use Bible API.
20
+
21
+ Options:
22
+ -V, --version output the version number
23
+ -h, --help display help for command
24
+
25
+ Commands:
26
+ init [options] [path] Initialize a new Bible API DB.
27
+ import-translation [options] <dir> [dirs...] Imports a translation from the given directory into the database.
28
+ import-translations [options] <dir> Imports all translations from the given directory into the database.
29
+ generate-translation-files [options] <input> <dir> Generates API files from the given input translation.
30
+ generate-translations-files [options] <input> <dir> Generates API files from the given input translations.
31
+ upload-api-files [options] <dest> Uploads API files to the specified destination. For S3, use the format s3://bucket-name/path/to/folder.
32
+ fetch-translations [options] <dir> [translations...] Fetches the specified translations from fetch.bible and places them in the given directory.
33
+ fetch-audio [options] <dir> [translations...] Fetches the specified audio translations and places them in the given directory.
34
+ Translations should be in the format "translationId/audioId". e.g. "BSB/gilbert"
35
+ fetch-bible-metadata <dir> Fetches the Theographic bible metadata and places it in the given directory.
36
+ help [command] display help for command
37
+ ```
38
+
39
+ The `@helloao/cli` package can also be used as a library.
40
+
41
+ The library exports a variety of actions, utilities, and supporting classes designed to assist with generating and managing a Free Use Bible API.
42
+
43
+ There are 6 main exports:
44
+
45
+ - `actions` - This export contains function versions of the CLI commands. They make it easy to call a CLI command from a script.
46
+ - `db` - This export contains functions that make working with a database easier. It supports operations like importing translations into a database, inserting chapters, verses, etc. and getting an updated database instance from a path.
47
+ - `downloads` - This export contains functions that make downloading files easier.
48
+ - `files` - This export contains functions that make working with files easier. It has functions to load files from a translation, discover translation metadata from the filesystem, and classes that support uploading API files to the local file system or to a zip archive.
49
+ - `uploads` - This export contains functions that make it easy to upload an API to a destination like S3, the local filesystem, or a zip archive.
50
+ - `s3` - This export contains a class that can upload files to S3.
51
+
52
+
53
+ Here are some common operations that you might want to perform:
54
+
55
+ #### Get a SQL Database
56
+
57
+ ```typescript
58
+ import { db } from '@helloao/cli';
59
+
60
+ const pathToDb = './bible-database.db';
61
+ const database = await db.getDb(pathToDb);
62
+
63
+ // do work on the database
64
+
65
+ // Close it when you are done.
66
+ database.close();
67
+ ```
68
+
69
+ #### Import a translation into a database from a directory
70
+
71
+ ```typescript
72
+ import { db } from '@helloao/cli';
73
+
74
+ const pathToDb = './bible-database.db';
75
+ const database = await db.getDb(pathToDb);
76
+
77
+ // Get a DOMParser for parsing USX.
78
+ // On Node.js, you may have to import jsdom or linkedom.
79
+ const parser = new DOMParser();
80
+
81
+ const pathToTranslation = './path/to/translation';
82
+
83
+ // Whether to overwrite files that already exist in the database.
84
+ // The system will automatically determine the hashes of the input files and overwrite changed files if needed, so this is only needed
85
+ // when you know that they need to be overwritten.
86
+ const overwrite = false;
87
+ await db.importTranslations(database, pathToTranslation, parser, overwrite);
88
+ ```
89
+
90
+ #### Generate an API from a translation
91
+
92
+ ```typescript
93
+ import { files, uploads } from '@helloao/cli';
94
+ import { generation } from '@helloao/tools';
95
+ import { toAsyncIterable } from '@helloao/tools/parser/iterators';
96
+
97
+ const translationPath = './path/to/translation';
98
+ const translationFiles = await files.loadTranslationFiles(translationPath);
99
+
100
+ // Used to parse XML
101
+ const domParser = new DOMParser();
102
+
103
+ // Generate a dataset from the files
104
+ // Datasets organize all the files and their content
105
+ // by translation, book, chapter, and verse
106
+ const dataset = generation.dataset.generateDataset(files, parser);
107
+
108
+ // You can optionally specifiy a prefix that should be added to all API
109
+ // links
110
+ const pathPrefix = '';
111
+
112
+ // Generate an API representation from the files
113
+ // This adds links between chapters and additional metadata.
114
+ const api = generation.api.generateApiForDataset(dataset, {
115
+ pathPrefix
116
+ });
117
+
118
+ // Generate output files from the API representation.
119
+ // This will give us a list of files and file paths that represent
120
+ // the entire API.
121
+ const outputFiles = generation.api.generateFilesForApi(api);
122
+
123
+ // Optionally upload files by using:
124
+ // const dest = 's3://my-bucket';
125
+ // await uploads.serializeAndUploadDatasets(dest, toAsyncIterable(outputFiles));
37
126
  ```
package/actions.js CHANGED
@@ -57,46 +57,46 @@ async function initDb(dbPath, options) {
57
57
  if (options.language) {
58
58
  console.log('Copying only the following languages:', options.language);
59
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};
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
87
  `);
88
88
  }
89
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;
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
100
  `);
101
101
  }
102
102
  console.log('Done.');
package/cli.js CHANGED
@@ -14,6 +14,7 @@ const actions_1 = require("./actions");
14
14
  const files_1 = require("./files");
15
15
  const dataset_1 = require("@helloao/tools/generation/dataset");
16
16
  const iterators_1 = require("@helloao/tools/parser/iterators");
17
+ const db_1 = require("./db");
17
18
  async function start() {
18
19
  const parser = new linkedom_1.DOMParser();
19
20
  globalThis.DOMParser = linkedom_1.DOMParser;
@@ -60,7 +61,7 @@ async function start() {
60
61
  globalThis.Node = linkedom_1.Node;
61
62
  const files = await (0, files_1.loadTranslationFiles)(path_1.default.resolve(input));
62
63
  const dataset = (0, dataset_1.generateDataset)(files, parser);
63
- await (0, uploads_1.uploadApiFiles)(path_1.default.resolve(dest), options, (0, iterators_1.toAsyncIterable)([dataset]));
64
+ await (0, uploads_1.serializeAndUploadDatasets)(path_1.default.resolve(dest), (0, iterators_1.toAsyncIterable)([dataset]), options);
64
65
  });
65
66
  program.command('generate-translations-files <input> <dir>')
66
67
  .description('Generates API files from the given input translations.')
@@ -83,7 +84,7 @@ async function start() {
83
84
  for (let b of (0, iterators_1.batch)(dirs, batchSize)) {
84
85
  const files = await (0, files_1.loadTranslationsFiles)(b);
85
86
  const dataset = (0, dataset_1.generateDataset)(files, parser);
86
- await (0, uploads_1.uploadApiFiles)(dest, options, (0, iterators_1.toAsyncIterable)([dataset]));
87
+ await (0, uploads_1.serializeAndUploadDatasets)(dest, (0, iterators_1.toAsyncIterable)([dataset]), options);
87
88
  }
88
89
  });
89
90
  program.command('upload-api-files')
@@ -99,7 +100,13 @@ async function start() {
99
100
  .option('--profile <profile>', 'The AWS profile to use for uploading to S3.')
100
101
  .option('--pretty', 'Whether to generate pretty-printed JSON files.')
101
102
  .action(async (dest, options) => {
102
- await (0, uploads_1.uploadApiFilesFromDatabase)(dest, options);
103
+ const db = (0, db_1.getPrismaDbFromDir)(process.cwd());
104
+ try {
105
+ await (0, uploads_1.uploadApiFilesFromDatabase)(db, dest, options);
106
+ }
107
+ finally {
108
+ db.$disconnect();
109
+ }
103
110
  });
104
111
  program.command('fetch-translations <dir> [translations...]')
105
112
  .description('Fetches the specified translations from fetch.bible and places them in the given directory.')
package/db.d.ts CHANGED
@@ -2,9 +2,9 @@ import { PrismaClient } from "./prisma-gen";
2
2
  import { Database } from 'better-sqlite3';
3
3
  import { DatasetOutput, DatasetTranslation, DatasetTranslationBook } from "@helloao/tools/generation/dataset";
4
4
  import { InputFile, TranslationBookChapter } from "@helloao/tools/generation";
5
- import { GenerateApiOptions } from "@helloao/tools/generation/api";
5
+ import { SerializeApiOptions, SerializedFile } from "./files";
6
6
  import { DOMParser } from "linkedom";
7
- import { Readable } from "stream";
7
+ import { GenerateApiOptions } from "@helloao/tools/generation/api";
8
8
  /**
9
9
  * Imports the translations from the given directories into the database.
10
10
  * @param db The database to import the translations into.
@@ -50,14 +50,6 @@ export declare function getPrismaDbFromDir(dir: string): PrismaClient<{
50
50
  }, never, import("prisma-gen/runtime/library").DefaultArgs>;
51
51
  export declare function getDbFromDir(dir: string): Promise<Database>;
52
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
53
  /**
62
54
  * Loads the datasets from the database in a series of batches.
63
55
  * @param db The database.
@@ -65,46 +57,23 @@ export interface SerializedFile {
65
57
  * @param translationsToLoad The list of translations to load. If not provided, all translations will be loaded.
66
58
  */
67
59
  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
- }
60
+ export type SerializeDatasetOptions = SerializeApiOptions & GenerateApiOptions;
74
61
  /**
75
- * Generates and serializes the API files for the dataset that is stored in the database.
62
+ * Generates and serializes the API files for the datasets that are stored in the database.
76
63
  * Yields each batch of serialized files.
77
64
  * @param db The database that the dataset should be loaded from.
78
- * @param options The options to use for generating the API.
65
+ * @param options The options to use for serializing the files.
66
+ * @param apiOptions The options to use for generating the API files.
79
67
  * @param translationsPerBatch The number of translations that should be loaded and written per batch.
80
68
  * @param translations The list of translations that should be loaded. If not provided, all translations will be loaded.
81
69
  */
82
- export declare function serializeFilesForDataset(db: PrismaClient, options: SerializeApiOptions, translationsPerBatch?: number, translations?: string[]): AsyncGenerator<SerializedFile[]>;
70
+ export declare function serializeFilesFromDatabase(db: PrismaClient, options?: SerializeDatasetOptions, translationsPerBatch?: number, translations?: string[]): AsyncGenerator<SerializedFile[]>;
83
71
  /**
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.
72
+ * Generates and serializes the API files for the given datasets.
73
+ * Yields each batch of serialized files.
74
+ *
75
+ * @param datasets The datasets to serialize.
76
+ * @param options The options to use for generating and serializing the files.
91
77
  */
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
- }
78
+ export declare function serializeDatasets(datasets: AsyncIterable<DatasetOutput>, options?: SerializeDatasetOptions): AsyncGenerator<SerializedFile[]>;
110
79
  //# sourceMappingURL=db.d.ts.map