Repository navigation
Expand file tree
/
Copy pathmain.ts
More file actions
124 lines (103 loc) · 3.24 KB
/
Copy pathmain.ts
File metadata and controls
124 lines (103 loc) · 3.24 KB
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
import { App, FileSystemAdapter, Notice, Plugin, PluginSettingTab, Setting } from 'obsidian';
import * as fs from 'fs';
import * as path from 'path';
interface MyPluginSettings {
folderToCopy: string;
destination: string;
}
const DEFAULT_SETTINGS: MyPluginSettings = {
folderToCopy: '',
destination: ''
}
export default class MyPlugin extends Plugin {
settings: MyPluginSettings;
async onload() {
await this.loadSettings();
// This adds a simple command that can be triggered anywhere
this.addCommand({
id: 'push-folder-command',
name: 'Push folder',
callback: async () => {
await this.copyFolderContents(this.settings.folderToCopy, this.settings.destination);
}
});
// This adds a settings tab so the user can configure various aspects of the plugin
this.addSettingTab(new SettingTab(this.app, this));
}
onunload() {}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
getVaultPath() {
let adapter = this.app.vault.adapter;
if (adapter instanceof FileSystemAdapter) {
return adapter.getBasePath();
}
return null;
}
async copyFolderContents(sourceFolder: string, destinationFolder: string) {
if (!destinationFolder) {
new Notice('Must set destination folder');
return;
}
const copy = async (src: string, dest: string) => {
const entries = await fs.promises.readdir(src, { withFileTypes: true });
await fs.promises.mkdir(dest, { recursive: true });
for (const entry of entries) {
const srcPath = path.join(src, entry.name);
const destPath = path.join(dest, entry.name);
if (entry.isDirectory()) {
await copy(srcPath, destPath); // Recursively copy directories
} else {
await fs.promises.copyFile(srcPath, destPath); // Copy files
}
}
};
try {
const vaultPath = this.getVaultPath();
if (!vaultPath) {
new Notice('Failed to get vault path');
return;
}
await copy(path.join(vaultPath, sourceFolder), destinationFolder);
new Notice('Folder copied successfully!');
} catch (error) {
console.error('Error copying folder:', error);
new Notice('Failed to copy folder. Check console for details.');
}
}
}
class SettingTab extends PluginSettingTab {
plugin: MyPlugin;
constructor(app: App, plugin: MyPlugin) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const {containerEl} = this;
containerEl.empty();
new Setting(containerEl)
.setName('Folder to copy')
.setDesc('Select a folder inside your Obsidian vault')
.addText(text => text
.setValue(this.plugin.settings.folderToCopy)
.setPlaceholder('Enter folder path')
.onChange(async (value) => {
this.plugin.settings.folderToCopy = value;
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName('Destination')
.setDesc('Where to copy your folder to. Note this will override anything in the destination folder. Think of it like a one-way push.')
.addText(text => text
.setValue(this.plugin.settings.destination)
.setPlaceholder('/Users/emma/src/my-blog/content/')
.onChange(async (value) => {
this.plugin.settings.destination = value;
await this.plugin.saveSettings();
}));
}
}