Skip to content
This repository was archived by the owner on Jan 24, 2025. It is now read-only.

Commit e2c35a1

Browse files
committed
added env file support
1 parent ca135ff commit e2c35a1

File tree

1 file changed

+56
-4
lines changed

1 file changed

+56
-4
lines changed

main.ts

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ interface PythonScripterSettings {
1111
passVaultPath: { [index: string]: boolean };
1212
passCurrentFile: { [index: string]: boolean };
1313
additionalArgs: { [index: string]: string[] };
14+
dotFiles: { [index: string]: string };
1415
// useLastFile: boolean;
1516
}
1617

@@ -21,6 +22,7 @@ const DEFAULT_SETTINGS: PythonScripterSettings = {
2122
passVaultPath: {},
2223
passCurrentFile: {},
2324
additionalArgs: {},
25+
dotFiles: {}
2426
// useLastFile: false
2527
}
2628

@@ -75,6 +77,29 @@ export default class PythonScripterPlugin extends Plugin {
7577
return;
7678
}
7779

80+
// Fixing relative paths
81+
let dot_files = this.settings.dotFiles[fileName];
82+
if (dot_files != undefined && !path.isAbsolute(dot_files)) {
83+
dot_files = path.join(basePath, dot_files);
84+
}
85+
86+
// Setting Environment Variables
87+
if (fileName in this.settings.dotFiles) {
88+
if (!fs.existsSync(dot_files)) {
89+
new Notice(`Error: ${dot_files} does not exist`)
90+
console.log(`Error: ${dot_files} does not exist`)
91+
return;
92+
}
93+
let dotFile = fs.readFileSync(dot_files, 'utf8');
94+
let lines = dotFile.split("\n");
95+
for (var i = 0; i < lines.length; i++) {
96+
let line = lines[i].split("=");
97+
if (line.length == 2) {
98+
process.env[line[0]] = line[1];
99+
}
100+
}
101+
}
102+
78103
// Getting Executable
79104
let python_exe = "";
80105
if (this.settings.pythonExe != "") {
@@ -109,28 +134,41 @@ export default class PythonScripterPlugin extends Plugin {
109134
}
110135

111136
// Getting Arguments
137+
let get_vault_path = true;
138+
let get_file_path = true;
139+
if (this.settings.passVaultPath[fileName] === undefined) {
140+
this.settings.passVaultPath[fileName] = true;
141+
get_vault_path = true;
142+
this.saveSettings();
143+
}
144+
if (this.settings.passCurrentFile[fileName] === undefined) {
145+
this.settings.passCurrentFile[fileName] = true;
146+
get_file_path = true;
147+
this.saveSettings();
148+
}
112149
var args = [];
113-
if (this.settings.passVaultPath[fileName]) {
150+
if (get_vault_path) {
114151
args.push(basePath);
115152
}
116-
if (this.settings.passCurrentFile[fileName]) {
153+
if (get_file_path) {
117154
var local_current_file_path = this.app.workspace.getActiveFile()?.path?.toString();
118155
if (!(local_current_file_path === undefined)) {
119156
args.push(local_current_file_path);
120157
} else {
121158
args.push("");
122159
}
123160
}
161+
if (this.settings.additionalArgs[fileName] === undefined) {
162+
this.settings.additionalArgs[fileName] = [];
163+
}
124164
for (var i = 0; i < this.settings.additionalArgs[fileName].length; i++) {
125165
args.push(this.settings.additionalArgs[fileName][i]);
126166
}
127-
128167
// Running the script
129168
let command = `${python_exe} \"${main_file}\"`;
130169
for (var i = 0; i < args.length; i++) {
131170
command += ` \"${args[i]}\"`;
132171
}
133-
134172
exec(command, { cwd: this.pythonDirectory }, (error: any, stdout: any, stderr: any) => {
135173
if (error) {
136174
new Notice(`Error executing script ${filePath}: ${error}`);
@@ -203,6 +241,9 @@ class PythonScripterSettingTab extends PluginSettingTab {
203241
await this.plugin.saveSettings();
204242
}));
205243
this.containerEl.createEl("h1", { text: `Scripts` });
244+
// Create an area with preable information
245+
this.containerEl.createEl("p", { text: `Use the following areas to set settings per script, paths provided may either be absolute or relative to the vault path.` });
246+
206247
for (var index = 0; index < this.files.length; index++) {
207248
let file = this.files[index];
208249
if (!(file in this.plugin.settings.passVaultPath)) {
@@ -223,6 +264,17 @@ class PythonScripterSettingTab extends PluginSettingTab {
223264
await this.plugin.saveSettings();
224265
});
225266
});
267+
new Setting(containerEl)
268+
.setName(`${file} .env File`)
269+
.setDesc(`Provides Runtime Environment Variables for ${file}`)
270+
.addTextArea((area) => {
271+
area
272+
.setValue(this.plugin.settings.dotFiles[file])
273+
.onChange(async (value) => {
274+
this.plugin.settings.dotFiles[file] = value;
275+
await this.plugin.saveSettings();
276+
});
277+
});
226278
new Setting(containerEl)
227279
.setName(`Pass Vault Path`)
228280
.setDesc(`Whether to pass the vault path to ${file}`)

0 commit comments

Comments
 (0)