From 381d3233abdca49cb44db10542441580cbaf1338 Mon Sep 17 00:00:00 2001 From: Benjamin Vigie Date: Tue, 26 Nov 2024 16:35:41 -0500 Subject: [PATCH 1/4] Fix vscode debugging command --- .vscode/launch.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index a837722..889be23 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -8,7 +8,7 @@ "type": "node", "request": "launch", "name": "Debug", - "program": "${workspaceFolder}/src/index.ts", + "program": "${workspaceFolder}/src/news_cli.ts", "preLaunchTask": "tsc", "outFiles": [ "${workspaceFolder}/dist/**/*.js" @@ -18,7 +18,7 @@ "type": "node", "request": "launch", "name": "Debug interactive mode", - "program": "${workspaceFolder}/src/index.ts", + "program": "${workspaceFolder}/src/news_cli.ts", "preLaunchTask": "tsc", "outFiles": [ "${workspaceFolder}/dist/**/*.js" From 0ad010ebae9ad2d3ff4e3b5dfccb6fb6f8c040af Mon Sep 17 00:00:00 2001 From: Benjamin Vigie Date: Tue, 26 Nov 2024 16:36:13 -0500 Subject: [PATCH 2/4] Adding options and default values to `EpubNews` --- src/epub_news.ts | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/epub_news.ts b/src/epub_news.ts index dc3ad77..aa1184a 100644 --- a/src/epub_news.ts +++ b/src/epub_news.ts @@ -11,6 +11,12 @@ const dt: DateTime = DateTime.now().setLocale(LOCALE); export type EpubArticle = EpubContentOptions; +/** MediaSource options */ +export interface Options { + debug?: boolean; + details?: boolean; +} + /** * Describe article list */ @@ -37,10 +43,13 @@ export class NoMediaSourceError extends Error {} * EpubNews is the lib which controls the fetching of articles and generate the epub news */ export default class EpubNews { - private _debug: boolean; + private _options: Options; - constructor(debug: boolean = false) { - this._debug = debug; + constructor(options: Options) { + // Default values + if (options.debug === undefined) options.debug = false; + if (options.details === undefined) options.details = false; + this._options = options; } /** @@ -52,7 +61,7 @@ export default class EpubNews { */ async listArticlesFromFeed(rssFeedUrl: string): Promise { // Try to retrieve a media source from the feed - const mediaSource = createMediaSource(rssFeedUrl, this._debug); + const mediaSource = createMediaSource(rssFeedUrl, this._options); if (!mediaSource) { throw new NoMediaSourceError( `No media source implemented for ${rssFeedUrl}> Are you sure it matches one of your MediaSource implementation ?`, @@ -77,7 +86,7 @@ export default class EpubNews { */ async getEpubDataFromArticles(rssFeedUrl: string, articles: Array): Promise { // Try to retrieve a media source from the feed - const mediaSource = createMediaSource(rssFeedUrl, this._debug); + const mediaSource = createMediaSource(rssFeedUrl, this._options); if (!mediaSource) { throw new NoMediaSourceError( `No media source implemented for ${rssFeedUrl}> Are you sure it matches one of your MediaSource implementation ?`, From b970ad5c60f098fa193033fa3f8ac693cf1066ca Mon Sep 17 00:00:00 2001 From: Benjamin Vigie Date: Tue, 26 Nov 2024 16:38:27 -0500 Subject: [PATCH 3/4] Passiong `Options` to `MediaSource` --- src/media_sources/media_source.ts | 33 +++++++++++++--------- src/media_sources/media_source_factory.ts | 9 +++--- src/media_sources/media_source_gamekult.ts | 8 ++++-- src/media_sources/media_source_le_monde.ts | 8 ++++-- 4 files changed, 34 insertions(+), 24 deletions(-) diff --git a/src/media_sources/media_source.ts b/src/media_sources/media_source.ts index 822c8d3..ef1c9a1 100644 --- a/src/media_sources/media_source.ts +++ b/src/media_sources/media_source.ts @@ -4,6 +4,7 @@ import Parser from "rss-parser"; import ora, { Ora } from "ora"; import terminalLink from "terminal-link"; import { EpubContentOptions } from "@lesjoursfr/html-to-epub"; +import { Options } from "../epub_news.js"; /** Default trim article error */ export class TrimArticleError extends Error {} @@ -15,15 +16,15 @@ export class TrimArticleError extends Error {} export abstract class MediaSource { protected _rss_sources: string; protected _rss_parser: Parser; - protected _debug: boolean; + protected _options: Options; protected _feedTitle: string | undefined; protected _spinner: Ora; protected _fetchOptions: https.RequestOptions; protected _cover: string | undefined; - constructor(source: string, debug: boolean = false) { + constructor(source: string, options: Options) { this._rss_sources = source; - this._debug = debug; + this._options = options; this._spinner = ora(); this._fetchOptions = {}; // You may need to create a new parser which will match your source's feed and items custom fields @@ -66,7 +67,7 @@ export abstract class MediaSource> { const feed = await this._rss_parser.parseURL(this._rss_sources); - if (this._debug) console.info(chalk.blue.magenta(feed.title)); + if (this._options.debug) console.info(chalk.blue.magenta(feed.title)); this._feedTitle = feed.title; feed.items.forEach((item) => { - if (this._debug) { + if (this._options.debug) { console.log(chalk.blue(` - ${item.title}`), chalk.gray(`(${item.pubDate})`)); } }); @@ -190,8 +193,10 @@ export abstract class MediaSource { - constructor(source: string, debug: boolean = false) { - super(source, debug); + constructor(source: string, options: Options) { + super(source, options); + // Create a new rss parser with custom Gamekult fields this._rss_parser = new Parser({ customFields: { feed: [], item: ["media:content"] }, @@ -55,7 +57,7 @@ export default class GamekultMediaSource extends MediaSource { - constructor(source: string, debug: boolean = false) { - super(source, debug); + constructor(source: string, options: Options) { + super(source, options); + // Create a new rss parser with custom "Le Monde" fields this._rss_parser = new Parser({ customFields: { feed: [], item: ["media:content"] }, @@ -75,7 +77,7 @@ export default class LeMondeMediaSource extends MediaSource Date: Tue, 26 Nov 2024 16:39:13 -0500 Subject: [PATCH 4/4] Adding _details_ option to show/hide app operations --- README.md | 1 + src/news_cli.ts | 5 +++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 5f79fed..8d596ca 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,7 @@ Options: -t, --title Ebook title (default: "today's date") -e, --envPath <path> Path to the env file to load (default: "./.env") -d, --debug Debug options (default: false) + -v, --verbose Print all app operations in terminal (default: true) -h, --help Show tool help ``` diff --git a/src/news_cli.ts b/src/news_cli.ts index 0f7592b..303b37e 100644 --- a/src/news_cli.ts +++ b/src/news_cli.ts @@ -22,7 +22,8 @@ program .option("-p, --path <path>", "default path for epub export", "./ if no DEFAULT_EXPORT_PATH set in env") .option("-t, --title <title>", "Ebook title", dt.toLocaleString(DateTime.DATE_FULL)) .option("-e, --envPath <path>", "Path to the env file to load (if any)", ".env") - .option("-d, --debug", "Debug options", false); + .option("-d, --debug", "Debug options", false) + .option("-v, --verbose", "Print all app operations in terminal", true); program.parse(); prg_options = program.opts(); @@ -30,7 +31,7 @@ prg_options = program.opts(); dotenv.config({ path: prg_options.envPath }); async function main() { - const epub = new EpubNews(prg_options.debug); + const epub = new EpubNews({ debug: prg_options.debug, details: prg_options.verbose }); let epubContent: EpubArticle[] = []; let customCss: string = ""; let epubCover: string | undefined;