Add support for profiles + excluding directories from lookup for tasks
This commit is contained in:
@@ -10,7 +10,7 @@ import { jsMapper } from "../util/code";
|
||||
/**
|
||||
* Returns all tasks from specified directory and filters them with optional query.
|
||||
*/
|
||||
export async function loadTasks(directories: string[], query?: string): Promise<Task[]> {
|
||||
export async function loadTasks(directories: string[], query?: string, exclude?: string): Promise<Task[]> {
|
||||
const ctx = {
|
||||
now: dayjs(),
|
||||
LOWEST: TaskPriority.LOWEST,
|
||||
@@ -21,8 +21,9 @@ export async function loadTasks(directories: string[], query?: string): Promise<
|
||||
HIGHEST: TaskPriority.HIGHEST
|
||||
};
|
||||
|
||||
const filter = query && jsMapper<Task, boolean>(query, ctx);
|
||||
const tasks = await Promise.all(directories.map(readTasksFromDirectory));
|
||||
const excludeFn = exclude ? jsMapper<string, boolean>(exclude, {}) : () => false;
|
||||
const filter = query && jsMapper<Task, boolean>(query, ctx);
|
||||
const tasks = await Promise.all(directories.map(readTasksFromDirectory(excludeFn)));
|
||||
|
||||
return tasks.flat().filter(t => filter ? filter(t) : true);
|
||||
}
|
||||
@@ -30,26 +31,28 @@ export async function loadTasks(directories: string[], query?: string): Promise<
|
||||
/**
|
||||
* Read all files in specific directory and returns all tasks from those files.
|
||||
*/
|
||||
async function readTasksFromDirectory(directory: string): Promise<Task[]> {
|
||||
return walk(directory, readTasksFromFile);
|
||||
function readTasksFromDirectory(excludeFn: (path: string) => boolean) {
|
||||
return async (directory: string) => walk(directory, readTasksFromFile, excludeFn);
|
||||
}
|
||||
|
||||
/**
|
||||
* Walks through a specific directory recursively and invokes visitor on each file.
|
||||
* Returns a flat list of items returned by visitors.
|
||||
*/
|
||||
async function walk<T>(directory: string, visitor: (path: string) => Promise<T[]>): Promise<T[]> {
|
||||
async function walk<T>(directory: string, visitor: (path: string) => Promise<T[]>, excludeFn: (path: string) => boolean): Promise<T[]> {
|
||||
const list = [];
|
||||
|
||||
for(const file of fs.readdirSync(directory)) {
|
||||
const path = `${directory}/${file}`;
|
||||
|
||||
if (fs.statSync(path).isDirectory()) {
|
||||
const items = await walk(path, visitor);
|
||||
const items = await walk(path, visitor, excludeFn);
|
||||
list.push(...items);
|
||||
} else if (path.endsWith("md") || (path.endsWith("MD"))) {
|
||||
const items = await visitor(path);
|
||||
list.push(...items);
|
||||
if (!excludeFn(path)) {
|
||||
const items = await visitor(path);
|
||||
list.push(...items);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user