Repository navigation
Expand file tree
/
Copy pathmain.js
More file actions
193 lines (192 loc) · 8.63 KB
/
Copy pathmain.js
File metadata and controls
193 lines (192 loc) · 8.63 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
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
/* THIS IS A GENERATED/BUNDLED FILE. DO NOT EDIT. */
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/main.ts
var main_exports = {};
__export(main_exports, {
default: () => OpenTermPlugin
});
module.exports = __toCommonJS(main_exports);
var import_obsidian = require("obsidian");
var import_child_process = require("child_process");
var path = __toESM(require("path"));
var SECTION_ID = "openterm";
var DEFAULT_SETTINGS = {
showDefaultTerminal: true,
showPowerShell: true,
showCmd: true,
windowsDefaultExe: "cmd.exe",
powershellExe: "pwsh",
cmdExe: "cmd.exe",
macTerminalApp: "Terminal",
linuxTerminalExe: "x-terminal-emulator"
};
function hasBasePath(adapter) {
return typeof adapter.getBasePath === "function";
}
function getDirectory(vaultBasePath, file) {
var _a, _b;
if (file instanceof import_obsidian.TFolder) {
return path.join(vaultBasePath, file.path);
}
return path.join(vaultBasePath, (_b = (_a = file.parent) == null ? void 0 : _a.path) != null ? _b : "");
}
function openInDefaultTerminal(directory, settings) {
if (import_obsidian.Platform.isWin) {
const exe = settings.windowsDefaultExe || "cmd.exe";
(0, import_child_process.execFile)("cmd.exe", ["/c", "start", "/D", directory, exe]);
} else if (import_obsidian.Platform.isMacOS) {
const app = settings.macTerminalApp || "Terminal";
(0, import_child_process.execFile)("open", ["-a", app, directory]);
} else {
const exe = settings.linuxTerminalExe || "x-terminal-emulator";
(0, import_child_process.execFile)(exe, [`--working-directory=${directory}`], (err) => {
if (err) {
(0, import_child_process.execFile)("xdg-open", [directory]);
}
});
}
}
function openInPowerShell(directory, settings) {
const exe = settings.powershellExe || "pwsh";
const psArgs = [
"-NoExit",
"-Command",
`Set-Location -LiteralPath '${directory.replace(/'/g, "''")}'`
];
if (import_obsidian.Platform.isWin) {
(0, import_child_process.execFile)("cmd.exe", ["/c", "start", exe, ...psArgs], (err) => {
const isNotFound = err && err.code === "ENOENT";
if (isNotFound && exe !== "powershell") {
(0, import_child_process.execFile)("cmd.exe", ["/c", "start", "powershell", ...psArgs]);
}
});
} else {
(0, import_child_process.execFile)(exe, psArgs);
}
}
function openInCmd(directory, settings) {
const exe = settings.cmdExe || "cmd.exe";
(0, import_child_process.execFile)("cmd.exe", ["/c", "start", "/D", directory, exe]);
}
var OpenTermPlugin = class extends import_obsidian.Plugin {
async onload() {
await this.loadSettings();
this.addSettingTab(new OpenTermSettingTab(this.app, this));
const adapter = this.app.vault.adapter;
this.registerEvent(
this.app.workspace.on("file-menu", (menu, file) => {
const basePath = hasBasePath(adapter) ? adapter.getBasePath() : "";
const directory = getDirectory(basePath, file);
if (this.settings.showDefaultTerminal) {
menu.addItem((item) => {
item.setTitle("Open in terminal").setIcon("terminal").setSection(SECTION_ID).onClick(() => openInDefaultTerminal(directory, this.settings));
});
}
if (this.settings.showPowerShell) {
menu.addItem((item) => {
item.setTitle("Open in PowerShell").setIcon("terminal").setSection(SECTION_ID).onClick(() => openInPowerShell(directory, this.settings));
});
}
if (import_obsidian.Platform.isWin && this.settings.showCmd) {
menu.addItem((item) => {
item.setTitle("Open in CMD").setIcon("terminal").setSection(SECTION_ID).onClick(() => openInCmd(directory, this.settings));
});
}
})
);
}
onunload() {
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
};
var OpenTermSettingTab = class extends import_obsidian.PluginSettingTab {
constructor(app, plugin) {
super(app, plugin);
this.plugin = plugin;
}
display() {
const { containerEl } = this;
containerEl.empty();
new import_obsidian.Setting(containerEl).setName("Context menu items").setHeading();
new import_obsidian.Setting(containerEl).setName("Show default terminal").setDesc("Show the 'Open in terminal' option in the context menu.").addToggle(
(toggle) => toggle.setValue(this.plugin.settings.showDefaultTerminal).onChange(async (value) => {
this.plugin.settings.showDefaultTerminal = value;
await this.plugin.saveSettings();
})
);
new import_obsidian.Setting(containerEl).setName("Show PowerShell").setDesc("Show the 'Open in PowerShell' option in the context menu.").addToggle(
(toggle) => toggle.setValue(this.plugin.settings.showPowerShell).onChange(async (value) => {
this.plugin.settings.showPowerShell = value;
await this.plugin.saveSettings();
})
);
new import_obsidian.Setting(containerEl).setName("Show CMD (Windows only)").setDesc("Show the 'Open in CMD' option in the context menu.").addToggle(
(toggle) => toggle.setValue(this.plugin.settings.showCmd).onChange(async (value) => {
this.plugin.settings.showCmd = value;
await this.plugin.saveSettings();
})
);
new import_obsidian.Setting(containerEl).setName("Executables").setHeading();
new import_obsidian.Setting(containerEl).setName("PowerShell executable").setDesc("Executable for PowerShell. Uses 'pwsh' (PowerShell 7+) by default, falling back to 'powershell' on Windows if not found.").addText(
(text) => text.setPlaceholder("pwsh").setValue(this.plugin.settings.powershellExe).onChange(async (value) => {
this.plugin.settings.powershellExe = value.trim();
await this.plugin.saveSettings();
})
);
new import_obsidian.Setting(containerEl).setName("CMD executable (Windows)").setDesc("Executable for Command Prompt on Windows.").addText(
(text) => text.setPlaceholder("cmd.exe").setValue(this.plugin.settings.cmdExe).onChange(async (value) => {
this.plugin.settings.cmdExe = value.trim();
await this.plugin.saveSettings();
})
);
new import_obsidian.Setting(containerEl).setName("Default terminal executable (Windows)").setDesc("Executable used for the default terminal option on Windows.").addText(
(text) => text.setPlaceholder("cmd.exe").setValue(this.plugin.settings.windowsDefaultExe).onChange(async (value) => {
this.plugin.settings.windowsDefaultExe = value.trim();
await this.plugin.saveSettings();
})
);
new import_obsidian.Setting(containerEl).setName("Terminal app (macOS)").setDesc("Application used for the default terminal option on macOS.").addText(
(text) => text.setPlaceholder("Terminal").setValue(this.plugin.settings.macTerminalApp).onChange(async (value) => {
this.plugin.settings.macTerminalApp = value.trim();
await this.plugin.saveSettings();
})
);
new import_obsidian.Setting(containerEl).setName("Terminal executable (Linux)").setDesc("Executable used for the default terminal option on Linux.").addText(
(text) => text.setPlaceholder("x-terminal-emulator").setValue(this.plugin.settings.linuxTerminalExe).onChange(async (value) => {
this.plugin.settings.linuxTerminalExe = value.trim();
await this.plugin.saveSettings();
})
);
}
};