Reputation: 267
I'm writing an application for Node.js. I want to execute an external text editor like VSCode and wait until a user inputs any texts and saves them. After that, the app receives the saved data from the text editor as stdin. I mean that it is action same as git commit[ENTER]
.
I tried to use open [path to app]
command but it didn't work well. The process exits with no wait.
Is there any way to do this?
Upvotes: 0
Views: 251
Reputation: 338
You shouldn't need an external library for this. You can launch the program using child_process.spawn, and then wait for changes to the file using fs.watch. When a change comes in (or when the editor is closed), you can read the file again.
Code (untested):
import {spawn} from "child_process";
import fs from "fs";
function getEditorInput(editorCmd, file, cb) {
const process = spawn(editorCmd, [file]);
process.on('close', (code) => {
// re-read file, and cb
});
// OR
const watcher = fs.watch(file, (ev, f) => {
// re-read file, call cb
})
}
Note you should close the watcher at some point (say, after editor closes) with watcher.close()
.
Upvotes: 1