Skip to content

Commit 6943fe7

Browse files
committed
refactor(packages): share ensureParentFolders helper
1 parent 24fc01f commit 6943fe7

File tree

3 files changed

+25
-36
lines changed

3 files changed

+25
-36
lines changed

src/services/packageExportService.ts

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
import { log } from "../logger/logManager";
1818
import { encodeToBase64 } from "../utils/base64";
1919
import { deepClone } from "../utils/deepClone";
20+
import { ensureParentFolders } from "../utils/ensureParentFolders";
2021

2122
export interface BuildPackageOptions {
2223
choices: IChoice[];
@@ -209,22 +210,3 @@ export async function writePackageToVault(
209210
const serialized = JSON.stringify(pkg, null, 2);
210211
await app.vault.adapter.write(normalizedPath, serialized);
211212
}
212-
213-
async function ensureParentFolders(app: App, filePath: string): Promise<void> {
214-
const lastSlash = filePath.lastIndexOf("/");
215-
if (lastSlash < 0) return;
216-
217-
const folderPath = filePath.slice(0, lastSlash);
218-
if (!folderPath) return;
219-
220-
const segments = folderPath.split("/").filter(Boolean);
221-
let current = "";
222-
223-
for (const segment of segments) {
224-
current = current ? `${current}/${segment}` : segment;
225-
const exists = await app.vault.adapter.exists(current);
226-
if (!exists) {
227-
await app.vault.createFolder(current);
228-
}
229-
}
230-
}

src/services/packageImportService.ts

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { CommandType } from "../types/macros/CommandType";
2121
import { log } from "../logger/logManager";
2222
import { decodeFromBase64 } from "../utils/base64";
2323
import { deepClone } from "../utils/deepClone";
24+
import { ensureParentFolders } from "../utils/ensureParentFolders";
2425

2526
export interface LoadedQuickAddPackage {
2627
pkg: QuickAddPackage;
@@ -562,23 +563,6 @@ function findMultiByPath(
562563
return currentMulti;
563564
}
564565

565-
async function ensureParentFolders(app: App, filePath: string): Promise<void> {
566-
const lastSlash = filePath.lastIndexOf("/");
567-
if (lastSlash < 0) return;
568-
const folderPath = filePath.slice(0, lastSlash);
569-
if (!folderPath) return;
570-
571-
const segments = folderPath.split("/").filter(Boolean);
572-
let current = "";
573-
for (const segment of segments) {
574-
current = current ? `${current}/${segment}` : segment;
575-
const exists = await app.vault.adapter.exists(current);
576-
if (!exists) {
577-
await app.vault.createFolder(current);
578-
}
579-
}
580-
}
581-
582566
function applyAssetPathOverrides(
583567
choice: IChoice,
584568
pathOverrides: Map<string, string>,

src/utils/ensureParentFolders.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import type { App } from "obsidian";
2+
3+
export async function ensureParentFolders(
4+
app: App,
5+
filePath: string,
6+
): Promise<void> {
7+
const lastSlash = filePath.lastIndexOf("/");
8+
if (lastSlash < 0) return;
9+
10+
const folderPath = filePath.slice(0, lastSlash);
11+
if (!folderPath) return;
12+
13+
const segments = folderPath.split("/").filter(Boolean);
14+
let current = "";
15+
16+
for (const segment of segments) {
17+
current = current ? `${current}/${segment}` : segment;
18+
const exists = await app.vault.adapter.exists(current);
19+
if (!exists) {
20+
await app.vault.createFolder(current);
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)