Add support for '--test' CLI option
This commit is contained in:
@@ -2,13 +2,14 @@ import { program } from "commander";
|
|||||||
import { CLIOptions } from "../types/cli";
|
import { CLIOptions } from "../types/cli";
|
||||||
|
|
||||||
import { loadConfig } from "../config";
|
import { loadConfig } from "../config";
|
||||||
import { notify, scan } from "../runner";
|
import { notify, scan, test } from "../runner";
|
||||||
|
|
||||||
const getOptions = () => program
|
const getOptions = () => program
|
||||||
.name("obsidian-tasks-reminder")
|
.name("obsidian-tasks-reminder")
|
||||||
.version("0.0.1")
|
.version("0.0.1")
|
||||||
.requiredOption("-c, --config <file>", "sets the path to the YAML file with configuration")
|
.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("-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("-s, --scan", "scans new tasks for future notifications and generates the database")
|
||||||
.option("-n, --notify", "reads the generated database and triggers notifications if any")
|
.option("-n, --notify", "reads the generated database and triggers notifications if any")
|
||||||
.parse()
|
.parse()
|
||||||
@@ -36,12 +37,19 @@ const getOptions = () => program
|
|||||||
current = current[segment];
|
current = current[segment];
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (options.test) {
|
||||||
|
await test(config);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (options.scan) {
|
if (options.scan) {
|
||||||
scan(config);
|
await scan(config);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options.notify) {
|
if (options.notify) {
|
||||||
notify(config);
|
await notify(config);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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.
|
* 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 {
|
export function serializeDatabase(tasks: Task[], mapper?: string): string {
|
||||||
const transformer = mapper
|
return JSON.stringify(createDatabase(tasks, mapper));
|
||||||
? jsMapper<Task, Notification>(mapper, {})
|
}
|
||||||
: defaultMapper;
|
|
||||||
|
|
||||||
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) {
|
if (n.time) {
|
||||||
(acc[n.time] = (acc[n.time] || [])).push(n);
|
(acc[n.time] = (acc[n.time] || [])).push(n);
|
||||||
};
|
};
|
||||||
|
|
||||||
return acc;
|
return acc;
|
||||||
}, {} as NotificationDatabase);
|
}, {} as NotificationDatabase);
|
||||||
|
|
||||||
return JSON.stringify(output);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function wrapWithTimeFiller(mapper: (task: Task) => Notification): (task: Task) => Notification {
|
function wrapWithTimeFiller(mapper: (task: Task) => Notification): (task: Task) => Notification {
|
||||||
|
|||||||
@@ -1,9 +1,24 @@
|
|||||||
import { loadTasks } from "../loader";
|
import { loadTasks } from "../loader";
|
||||||
import { dumpDatabase } from "../database/serializer";
|
import { createDatabase, dumpDatabase } from "../database/serializer";
|
||||||
import { loadDatabase } from "../database/deserializer";
|
import { loadDatabase } from "../database/deserializer";
|
||||||
import { remind } from "../backend";
|
import { remind } from "../backend";
|
||||||
import { Config } from "../types/config";
|
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) {
|
export async function scan(config: Config) {
|
||||||
const tasks = await loadTasks(config.sources, config.query);
|
const tasks = await loadTasks(config.sources, config.query);
|
||||||
dumpDatabase(config.databaseFile, tasks, config.mapper);
|
dumpDatabase(config.databaseFile, tasks, config.mapper);
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
export type CLIOptions = {
|
export type CLIOptions = {
|
||||||
config: string;
|
config: string;
|
||||||
|
test: boolean;
|
||||||
scan: boolean;
|
scan: boolean;
|
||||||
notify: boolean;
|
notify: boolean;
|
||||||
set: string[];
|
set: string[];
|
||||||
|
|||||||
Reference in New Issue
Block a user