(
host: DefaultBasesFileHost,
options: DefaultBasesFileOptions = {}
)
| 54 | } |
| 55 | |
| 56 | export async function ensureDefaultBasesViewFiles( |
| 57 | host: DefaultBasesFileHost, |
| 58 | options: DefaultBasesFileOptions = {} |
| 59 | ): Promise<DefaultBasesFileResult> { |
| 60 | const created: string[] = []; |
| 61 | const updated: string[] = []; |
| 62 | const skipped: string[] = []; |
| 63 | const overwriteExisting = options.overwriteExisting === true; |
| 64 | |
| 65 | try { |
| 66 | const vault = host.app.vault; |
| 67 | const adapter = vault.adapter; |
| 68 | const commandFileMapping = { |
| 69 | ...DEFAULT_SETTINGS.commandFileMapping, |
| 70 | ...(host.settings.commandFileMapping ?? {}), |
| 71 | }; |
| 72 | host.settings.commandFileMapping = commandFileMapping; |
| 73 | |
| 74 | for (const [commandId, rawPath] of Object.entries(commandFileMapping)) { |
| 75 | if (!rawPath) { |
| 76 | continue; |
| 77 | } |
| 78 | |
| 79 | const normalizedPath = normalizePath(rawPath); |
| 80 | const template = host.generateTemplate(commandId); |
| 81 | if (!template) { |
| 82 | skipped.push(rawPath); |
| 83 | continue; |
| 84 | } |
| 85 | |
| 86 | if (await adapter.exists(normalizedPath)) { |
| 87 | if (!overwriteExisting) { |
| 88 | skipped.push(rawPath); |
| 89 | continue; |
| 90 | } |
| 91 | |
| 92 | const existing = vault.getAbstractFileByPath(normalizedPath); |
| 93 | if (!(existing instanceof TFile)) { |
| 94 | host.warn?.( |
| 95 | `[TaskNotes][Bases] Cannot update default Bases file because path is not a file: ${normalizedPath}` |
| 96 | ); |
| 97 | skipped.push(rawPath); |
| 98 | continue; |
| 99 | } |
| 100 | |
| 101 | await vault.modify(existing, template); |
| 102 | updated.push(rawPath); |
| 103 | continue; |
| 104 | } |
| 105 | |
| 106 | const lastSlashIndex = normalizedPath.lastIndexOf("/"); |
| 107 | const directory = |
| 108 | lastSlashIndex >= 0 ? normalizedPath.substring(0, lastSlashIndex) : ""; |
| 109 | |
| 110 | if (directory) { |
| 111 | await ensureFolderHierarchy(vault, directory); |
| 112 | } |
| 113 |
no test coverage detected