Add support for '--test' CLI option

This commit is contained in:
2025-01-17 17:03:50 +01:00
parent 8490e073f6
commit cc7deb9d6b
4 changed files with 41 additions and 12 deletions

View File

@@ -2,13 +2,14 @@ import { program } from "commander";
import { CLIOptions } from "../types/cli";
import { loadConfig } from "../config";
import { notify, scan } from "../runner";
import { notify, scan, test } from "../runner";
const getOptions = () => program
.name("obsidian-tasks-reminder")
.version("0.0.1")
.requiredOption("-c, --config <file>", "sets the path to the YAML file with configuration")
.option("-x, --set <arg>", "overrides the config option for this specific run (arg: <key>=<name>, i.e. backend.ntfy.enable=false", (v: string, prev: string[]) => prev.concat([v]), [])
.requiredOption("-c, --config <file>", "sets the path to the YAML file with configuration")
.option("-x, --set <arg>", "overrides the config option for this specific run (arg: <key>=<name>, i.e. backend.ntfy.enable=false", (v: string, prev: string[]) => prev.concat([v]), [])
.option("-t, --test", "evaluates the query, applies the mapper and prints to stdout the notifications about to be trigger, without actual triggering them")
.option("-s, --scan", "scans new tasks for future notifications and generates the database")
.option("-n, --notify", "reads the generated database and triggers notifications if any")
.parse()
@@ -36,12 +37,19 @@ const getOptions = () => program
current = current[segment];
});
}
if (options.test) {
await test(config);
return;
}
if (options.scan) {
scan(config);
await scan(config);
return;
}
if (options.notify) {
notify(config);
await notify(config);
return;
}
}

View File

@@ -16,19 +16,24 @@ export function dumpDatabase(file: string, tasks: Task[], mapper?: string) {
* Applies the mapper for each task from list, groups them by time and serializes into JSON format.
*/
export function serializeDatabase(tasks: Task[], mapper?: string): string {
const transformer = mapper
? jsMapper<Task, Notification>(mapper, {})
: defaultMapper;
return JSON.stringify(createDatabase(tasks, mapper));
}
const output = tasks.map(wrapWithTimeFiller(transformer)).reduce((acc, n) => {
/**
* Applies the mapper for each task from list and groups them by time.
*/
export function createDatabase(tasks: Task[], mapper?: string): NotificationDatabase {
const transformer = mapper
? jsMapper<Task, Notification>(mapper, {})
: defaultMapper;
return tasks.map(wrapWithTimeFiller(transformer)).reduce((acc, n) => {
if (n.time) {
(acc[n.time] = (acc[n.time] || [])).push(n);
};
return acc;
}, {} as NotificationDatabase);
return JSON.stringify(output);
}
function wrapWithTimeFiller(mapper: (task: Task) => Notification): (task: Task) => Notification {

View File

@@ -1,9 +1,24 @@
import { loadTasks } from "../loader";
import { dumpDatabase } from "../database/serializer";
import { createDatabase, dumpDatabase } from "../database/serializer";
import { loadDatabase } from "../database/deserializer";
import { remind } from "../backend";
import { Config } from "../types/config";
export async function test(config: Config) {
const tasks = await loadTasks(config.sources, config.query);
const db = createDatabase(tasks, config.mapper);
for (const time of Object.keys(db)) {
console.log(time);
for (const notification of db[time]) {
console.log(` - title: ${notification.title}\n text: ${notification.text}\n priority: ${notification.priority}\n tags: ${notification.tags?.join(",")}`)
}
console.log();
}
}
export async function scan(config: Config) {
const tasks = await loadTasks(config.sources, config.query);
dumpDatabase(config.databaseFile, tasks, config.mapper);

View File

@@ -1,5 +1,6 @@
export type CLIOptions = {
config: string;
test: boolean;
scan: boolean;
notify: boolean;
set: string[];