Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: write to tempdir #14

Merged
merged 1 commit into from
May 22, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {Notice, Plugin} from 'obsidian';
import {normalizePath, Notice, Plugin} from 'obsidian';
import * as fs from "fs";
import * as os from "os"
import * as child_process from "child_process";
import {Outputter} from "./Outputter";
import {SettingsTab, ExecutorSettings} from "./SettingsTab";
Expand All @@ -25,6 +26,8 @@ const DEFAULT_SETTINGS: ExecutorSettings = {
maxPrologAnswers: 15,
}



export default class ExecuteCodePlugin extends Plugin {
settings: ExecutorSettings;

Expand Down Expand Up @@ -145,16 +148,22 @@ export default class ExecuteCodePlugin extends Plugin {
console.log("Unloaded plugin: Execute Code");
}

private getTempFile(ext: string) {
return `${os.tmpdir()}/temp_${Date.now()}.${ext}`
}

private runJavaScript(codeBlockContent: string, outputter: Outputter, button: HTMLButtonElement) {
new Notice("Running...");
const tempFileName = `temp_${Date.now()}.js`;
console.log(tempFileName);
const tempFileName = this.getTempFile('js')
console.log(`${tempFileName}`);

fs.promises.writeFile(tempFileName, codeBlockContent)
.then(() => {
console.log(`Execute ${this.settings.nodePath} ${tempFileName}`);
const args = this.settings.nodeArgs ? this.settings.nodeArgs.split(" ") : [];
args.push(tempFileName);
console.log(this.settings.nodePath)
console.log(args)
const child = child_process.spawn(this.settings.nodePath, args);

this.handleChildOutput(child, outputter, button, tempFileName);
Expand All @@ -181,7 +190,7 @@ export default class ExecuteCodePlugin extends Plugin {

private runPython(codeBlockContent: string, outputter: Outputter, button: HTMLButtonElement) {
new Notice("Running...");
const tempFileName = `temp_${Date.now()}.py`;
const tempFileName = this.getTempFile('js')

fs.promises.writeFile(tempFileName, codeBlockContent)
.then(() => {
Expand Down