-
-
Notifications
You must be signed in to change notification settings - Fork 247
/
service.ts
417 lines (399 loc) · 13.2 KB
/
service.ts
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
import {
assertInvalidState,
DendronError,
DEngineClient,
Disposable,
DLink,
DVault,
genUUID,
NoteChangeEntry,
NoteProps,
NoteUtils,
VaultUtils,
} from "@dendronhq/common-all";
import { createDisposableLogger, vault2Path } from "@dendronhq/common-server";
import throttle from "@jcoreio/async-throttle";
import _ from "lodash";
import path from "path";
import { Git, WorkspaceService } from "..";
import { LinkUtils, RemarkUtils } from "../markdown/remark/utils";
import { DendronASTDest } from "../markdown/types";
import { MDUtilsV4 } from "../markdown/utils";
import fs from "fs-extra";
export enum DoctorActionsEnum {
FIX_FRONTMATTER = "fixFrontmatter",
H1_TO_TITLE = "h1ToTitle",
HI_TO_H2 = "h1ToH2",
REMOVE_STUBS = "removeStubs",
CREATE_MISSING_LINKED_NOTES = "createMissingLinkedNotes",
REGENERATE_NOTE_ID = "regenerateNoteId",
FIND_BROKEN_LINKS = "findBrokenLinks",
FIX_REMOTE_VAULTS = "fixRemoteVaults",
FIX_AIRTABLE_METADATA = "fixAirtableMetadata",
}
export type DoctorServiceOpts = {
action: DoctorActionsEnum;
query?: string;
candidates?: NoteProps[];
limit?: number;
dryRun?: boolean;
exit?: boolean;
quiet?: boolean;
engine: DEngineClient;
podId?: string;
hierarchy?: string;
vault?: DVault | string;
};
/** DoctorService is a disposable, you **must** dispose instances you create
* otherwise you risk leaking file descriptors which may lead to crashes. */
export class DoctorService implements Disposable {
public L: ReturnType<typeof createDisposableLogger>["logger"];
private loggerDispose: ReturnType<typeof createDisposableLogger>["dispose"];
constructor() {
const { logger, dispose } = createDisposableLogger("DoctorService");
this.L = logger;
this.loggerDispose = dispose;
}
dispose() {
this.loggerDispose();
}
findBrokenLinks(note: NoteProps, notes: NoteProps[], engine: DEngineClient) {
const { wsRoot, vaults } = engine;
const links = note.links;
if (_.isEmpty(links)) {
return [];
}
const out = _.filter(links, (link) => {
if (link.type !== "wiki") {
return false;
}
const hasVaultPrefix = LinkUtils.hasVaultPrefix(link);
let vault: DVault | undefined;
if (hasVaultPrefix) {
vault = VaultUtils.getVaultByName({
vaults,
vname: link.to!.vaultName!,
});
if (!vault) return false;
}
const isMultiVault = vaults.length > 1;
const noteExists = NoteUtils.getNoteByFnameV5({
fname: link.to!.fname as string,
vault: hasVaultPrefix ? vault! : note.vault,
notes,
wsRoot,
});
if (hasVaultPrefix) {
// true: link w/ vault prefix that points to nothing. (candidate for sure)
// false: link w/ vault prefix that points to a note. (valid link)
return !noteExists;
}
if (!noteExists) {
// true: no vault prefix and single vault. (candidate for sure)
// false: no vault prefix and multi vault. (ambiguous)
return !isMultiVault;
}
// (valid link)
return false;
});
return out;
}
getBrokenLinkDestinations(notes: NoteProps[], engine: DEngineClient) {
const { vaults } = engine;
let brokenWikiLinks: DLink[] = [];
_.forEach(notes, (note) => {
const links = note.links;
if (_.isEmpty(links)) {
return;
}
const brokenLinks = this.findBrokenLinks(note, notes, engine);
brokenWikiLinks = brokenWikiLinks.concat(brokenLinks);
return true;
});
const uniqueCandidates: NoteProps[] = _.map(
_.uniqBy(brokenWikiLinks, "to.fname"),
(link) => {
const destVault = link.to?.vaultName
? VaultUtils.getVaultByName({ vaults, vname: link.to.vaultName })!
: VaultUtils.getVaultByName({ vaults, vname: link.from.vaultName! })!;
return NoteUtils.create({
fname: link.to!.fname!,
vault: destVault!,
});
}
);
return uniqueCandidates;
}
async executeDoctorActions(opts: DoctorServiceOpts) {
const {
action,
engine,
query,
candidates,
limit,
dryRun,
exit,
podId,
hierarchy,
vault,
} = _.defaults(opts, {
limit: 99999,
exit: true,
});
let notes: NoteProps[];
if (_.isUndefined(candidates)) {
notes = query
? engine.queryNotesSync({ qs: query, originalQS: query }).data
: _.values(engine.notes);
} else {
notes = candidates;
}
notes = notes.filter((n) => !n.stub);
// this.L.info({ msg: "prep doctor", numResults: notes.length });
let numChanges = 0;
let resp: any;
const engineWrite = dryRun
? () => {}
: throttle(_.bind(engine.writeNote, engine), 300, {
// @ts-ignore
leading: true,
});
const engineDelete = dryRun
? () => {}
: throttle(_.bind(engine.deleteNote, engine), 300, {
// @ts-ignore
leading: true,
});
const engineGetNoteByPath = dryRun
? () => {}
: throttle(_.bind(engine.getNoteByPath, engine), 300, {
// @ts-ignore
leading: true,
});
let doctorAction: ((note: NoteProps) => Promise<any>) | undefined;
switch (action) {
case DoctorActionsEnum.FIX_FRONTMATTER: {
// eslint-disable-next-line no-console
console.log(
"the CLI currently doesn't support this action. please run this using the plugin"
);
return { exit };
}
// eslint-disable-next-line no-fallthrough
case DoctorActionsEnum.H1_TO_TITLE: {
doctorAction = async (note: NoteProps) => {
const changes: NoteChangeEntry[] = [];
const proc = MDUtilsV4.procFull({
dest: DendronASTDest.MD_DENDRON,
engine,
fname: note.fname,
vault: note.vault,
});
const newBody = await proc()
.use(RemarkUtils.h1ToTitle(note, changes))
.process(note.body);
note.body = newBody.toString();
if (!_.isEmpty(changes)) {
await engineWrite(note, { updateExisting: true });
this.L.info({ msg: `changes ${note.fname}`, changes });
numChanges += 1;
return;
} else {
return;
}
};
break;
}
case DoctorActionsEnum.HI_TO_H2: {
doctorAction = async (note: NoteProps) => {
const changes: NoteChangeEntry[] = [];
const proc = MDUtilsV4.procFull({
dest: DendronASTDest.MD_DENDRON,
engine,
fname: note.fname,
vault: note.vault,
});
const newBody = await proc()
.use(RemarkUtils.h1ToH2(note, changes))
.process(note.body);
note.body = newBody.toString();
if (!_.isEmpty(changes)) {
await engineWrite(note, { updateExisting: true });
this.L.info({ msg: `changes ${note.fname}`, changes });
numChanges += 1;
return;
} else {
return;
}
};
break;
}
case DoctorActionsEnum.REMOVE_STUBS: {
doctorAction = async (note: NoteProps) => {
const changes: NoteChangeEntry[] = [];
if (_.trim(note.body) === "") {
changes.push({
status: "delete",
note,
});
}
if (!_.isEmpty(changes)) {
await engineDelete(note);
const vname = VaultUtils.getName(note.vault);
this.L.info(
`doctor ${DoctorActionsEnum.REMOVE_STUBS} ${note.fname} ${vname}`
);
numChanges += 1;
return;
} else {
return;
}
};
break;
}
case DoctorActionsEnum.CREATE_MISSING_LINKED_NOTES: {
notes = this.getBrokenLinkDestinations(notes, engine);
doctorAction = async (note: NoteProps) => {
await engineGetNoteByPath({
npath: note.fname,
createIfNew: true,
vault: note.vault,
});
numChanges += 1;
};
break;
}
case DoctorActionsEnum.REGENERATE_NOTE_ID: {
doctorAction = async (note: NoteProps) => {
if (note.id === "root") return; // Root notes are special, preserve them
note.id = genUUID();
await engine.writeNote(note, {
runHooks: false,
updateExisting: true,
});
numChanges += 1;
};
break;
}
case DoctorActionsEnum.FIND_BROKEN_LINKS: {
resp = [];
doctorAction = async (note: NoteProps) => {
const brokenLinks = this.findBrokenLinks(note, notes, engine);
if (brokenLinks.length > 0) {
resp.push({
file: note.fname,
vault: VaultUtils.getName(note.vault),
links: brokenLinks.map((link) => {
return {
value: link.value,
line: link.position?.start.line,
column: link.position?.start.column,
};
}),
});
return brokenLinks;
} else {
return;
}
};
break;
}
case DoctorActionsEnum.FIX_REMOTE_VAULTS: {
/** Convert a local vault to a remote vault if it is in a git repository and has a remote set. */
// This action deliberately doesn't set `doctorAction` since it doesn't run per note
const { wsRoot, vaults } = engine;
const ctx = "ReloadIndex.convertToRemoteVaultIfPossible";
await Promise.all(
vaults.map(async (vault) => {
const vaultDir = vault2Path({ wsRoot, vault });
const gitPath = path.join(vaultDir, ".git");
// Already a remote vault
if (vault.remote !== undefined) return;
// Not a git repository, nothing to convert
if (!(await fs.pathExists(gitPath))) return;
const git = new Git({ localUrl: vaultDir });
const remoteUrl = await git.getRemoteUrl();
// We can't convert if there is no remote
if (!remoteUrl) return;
// Need the workspace service to function
const workspaceService = new WorkspaceService({ wsRoot });
this.L.info({
ctx,
vaultDir,
remoteUrl,
msg: "converting local vault to a remote vault",
});
await workspaceService.markVaultAsRemoteInConfig(vault, remoteUrl);
})
);
return { exit: true };
}
case DoctorActionsEnum.FIX_AIRTABLE_METADATA: {
// Converts the airtable id in note frontmatter from a single scalar value to a hashmap
if (!podId) {
assertInvalidState(
"Please provide the pod Id that was used to export the note(s)."
);
}
// we get vault name(string) as parameter from cli and vault(DVault) from plugin
const selectedVault = _.isString(vault)
? VaultUtils.getVaultByName({ vaults: engine.vaults, vname: vault })
: vault;
const selectedHierarchy = _.isUndefined(query) ? hierarchy : query;
// Plugin already checks for selected hierarchy. This check is useful when fixAirtableMetadata action is ran from cli
if (!selectedHierarchy || !selectedVault) {
assertInvalidState(
"Please provide the hierarchy(with --query arg) and vault(--vault) of notes you would like to update with new Airtable Metadata"
);
}
//finding candidate notes
notes = Object.values(notes).filter(
(value) =>
value.fname.startsWith(selectedHierarchy) &&
value.stub !== true &&
VaultUtils.isEqualV2(value.vault, selectedVault) &&
value.custom.airtableId
);
this.L.info({
msg: `${DoctorActionsEnum.FIX_FRONTMATTER} ${notes.length} Notes will be Affected`,
});
doctorAction = async (note: NoteProps) => {
//get airtable id from note
const airtableId = _.get(note.custom, "airtableId") as string;
const pods = {
airtable: {
[podId]: airtableId,
},
};
delete note.custom["airtableId"];
const updatedNote = {
...note,
custom: { ...note.custom, pods },
};
// update note
await engine.writeNote(updatedNote, { updateExisting: true });
};
break;
}
default:
throw new DendronError({
message:
"Unexpected Doctor action. If this is something Dendron should support, please create an issue on our Github repository.",
});
}
if (doctorAction !== undefined) {
for (const note of notes) {
if (numChanges >= limit) break;
this.L.debug({ msg: `processing ${note.fname}` });
// eslint-disable-next-line no-await-in-loop
await doctorAction(note);
}
}
this.L.info({ msg: "doctor done", numChanges });
if (action === DoctorActionsEnum.FIND_BROKEN_LINKS && !opts.quiet) {
// eslint-disable-next-line no-console
console.log(JSON.stringify({ brokenLinks: resp }, null, " "));
}
return { exit, resp };
}
}