generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
services.ts
25 lines (22 loc) · 1.1 KB
/
services.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
import { DataAdapter } from "obsidian";
import * as path from "path";
import { ServerFile } from "types";
async function createFileAndFolders(sFile: ServerFile, adapter: DataAdapter) {
const foldersToBeCreated = sFile.path.split("/");
foldersToBeCreated.splice(foldersToBeCreated.length - 1, 1);
await adapter.mkdir(path.join(...foldersToBeCreated))
await adapter.writeBinary(sFile.path, Buffer.from(sFile.content, 'base64'))
}
async function fetchAllSubFoldersAndContents(startPath: string, adapter: DataAdapter): Promise<string[]> {
const thisFolderFiles: string[] = []
const thisFolder = await adapter.list(startPath)
thisFolderFiles.push(...thisFolder.files)
for (const folder of thisFolder.folders) {
if (!folder.contains(".git") && !folder.contains("node_modules") && !folder.contains("git-sync") && !folder.contains('obsidian-git-sync')) {
const contents = await fetchAllSubFoldersAndContents(folder, adapter)
thisFolderFiles.push(...contents)
}
}
return thisFolderFiles
}
export { createFileAndFolders, fetchAllSubFoldersAndContents }