-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfindpending.js
157 lines (137 loc) · 4.02 KB
/
findpending.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// a tool to make todos in my obsidian journal
//
//
const {
Command
} = require("commander");
const toml = require("toml");
const moment = require("moment");
const fs = require("fs");
const fsPromises = fs.promises;
const path = require("path");
const {
findAll,
findTags
} = require("./todosearch");
const {
basename
} = require("path");
const GENDIR = "_Generated";
const mtd = require("./mtd");
var baseLoc = `${process.env.HOME}/DropBox/Journal`;
var when = moment(Date.now()); // when is a moment object
console.log(when);
const prog = new Command();
prog.version('0.0.1');
prog.option('-d, --debug', 'output extra debugging')
.option('-D, --dir <directory>', 'search in directory')
.option('-W --watch');
prog.parse(process.argv);
if (prog.dir) {
console.log('-Dir ' + prog.dir);
baseLoc = prog.dir;
}
baseLoc = path.normalize(baseLoc);
console.log(baseLoc);
var configDir = process.env.MARKTODO_CONFIGDIR;
if (!configDir) {
configDir = `${process.env.HOME}/.config/marktask`;
if (!fs.existsSync(configDir)) {
fs.mkdirSync(configDir);
}
}
const configLoc = `${configDir}/config.toml`;
console.log(`configLoc ${configLoc}`);
const config = fs.existsSync(configLoc) ? toml.parse(fs.readFileSync(configLoc, 'utf-8')) : {};
if (config.baseDir) {
baseLoc = config.baseDir;
if (!baseLoc.startsWith("/")) {
baseLoc = path.join(process.env.HOME, baseLoc);
}
}
const datefmt = config.dateFmt ?? "YYYY-MM-DD";
genDir = config.genDir ?? GENDIR;
const genLoc = path.join(baseLoc, genDir);
const dotLoc = path.join(baseLoc, '.obsidian');
const taskapi = new mtd({
"baseLoc": baseLoc,
"genLoc": genLoc,
});
taskapi.writeHandler = async (text, fpath) => {
await fsPromises.writeFile(fpath, text);
};
const readPath = async (dirLoc, fileCache) => {
let dirInfo;
try {
const dirPath = path.join(baseLoc, dirLoc);
dirInfo = await fs.promises.opendir(dirPath);
} catch (err) {
console.log(`error opening directory ${ err } `);
throw (err);
}
for await (const dirent of dirInfo) {
const fPathRelative = path.join(dirLoc, dirent.name);
const fPath = path.join(baseLoc, fPathRelative);
const relPath = fPath.substring(baseLoc.length + 1, fPath.length - 3);
const fname = dirent.name.slice(0, -3);
if (dirent.isFile() && dirent.name.endsWith(".md")) {
var stats;
try {
const fd = fs.openSync(fPath);
stats = fs.fstatSync(fd);
fs.closeSync(fd);
} catch (e) {}
var lastTouch;
var isNew = true;
/* Only bother to read the file if its new (not in cache) or
if the modify timestamp is newer than cached
*/
var isOld = lastTouch && lastTouch >= stats.mtime.getTime();
if (isOld) {
todos = fileCache[fPathRelative].todos;
} else {
let text;
try {
if (prog.debug) console.log(`reading file ${fPath}`);
text = fs.readFileSync(fPath, "utf8");
} catch (err) {
console.log(`${err} reading file ${fPath}`);
throw (err);
}
todos = taskapi.findPending(text, relPath);
for(var t in todos){
console.log(todos[t].item);
}
//console.log(fname);
}
} else if (dirent.isDirectory() && dirent.name !== genDir) {
// don't bother with the generated directory
// otherwise recurse to subdirs
const dir = path.join(dirLoc, dirent.name);
let resolved = path.join(baseLoc, dir);
if (prog.debug) console.log(`cd to ${resolved}`);
if (watchedPath(resolved)) {
await readPath(dir, fileCache);
}
}
}
};
var ignoreDirs = [genLoc, dotLoc];
if (config.ignoreDirs) {
for (const dir in config.ignoreDirs)
ignoreDirs.push(path.join(baseLoc, config.ignoreDirs[dir]));
}
console.log(`ignoreDirs: ${ignoreDirs}`);
watchedPath = (fpath) => {
for (const id in this.ignoreDirs) {
if (fpath.startsWith(this.ignoreDirs[id])) {
return false;
}
}
return true;
};
const main = async () => {
console.log(`run against: ${baseLoc}`);
await readPath(".");
};
main();