external-editor
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|---|---|---|---|---|
chardet | 0.7.0 | 16.52 kB | MIT | prod | |
iconv-lite | 0.4.24 | 180.9 kB | MIT | prod | |
tmp | 0.0.33 | 8.42 kB | MIT | prod |
External-editor is a Node.js module that facilitates editing of a string using a user's preferred text editor. It operates by leveraging environment variables $VISUAL or $ENVIRONMENT. This handy tool is designed to streamline text modifications within an integrated development environment.
To use the external-editor package in your JavaScript projects, you need to first install it using npm by executing npm install external-editor --save
in your terminal.
To edit a string, you can utilize the provided .edit
convenience method as seen in this snippet:
import {edit} from "external-editor";
const data = edit('\n\n# Please write your text above');
console.log(data);
A more detailed usage example that provides a full feature set including error handling is as follows:
import {ExternalEditor, CreateFileError, ReadFileError, RemoveFileError} from "external-editor"
try {
const editor = new ExternalEditor();
const text = editor.run() // the text is also available in editor.text
if (editor.last_exit_status !== 0) {
console.log("The editor exited with a non-zero code");
}
} catch (err) {
if (err instanceOf CreateFileError) {
console.log('Failed to create the temporary file');
} else if (err instanceOf ReadFileError) {
console.log('Failed to read the temporary file');
} else if (err instanceOf LaunchEditorError) {
console.log('Failed to launch your editor');
} else {
throw err;
}
}
// Do things with the text
// Eventually call the cleanup to remove the temporary file
try {
editor.cleanup();
} catch (err) {
if (err instanceOf RemoveFileError) {
console.log('Failed to remove the temporary file');
} else {
throw err
}
}
This bit of code demonstrates the creation of a new instance of the ExternalEditor, running the editor, handling potential errors, working with the resultant text, and cleaning up the temporary file.
The comprehensive documentation for the external-editor package is available within the GitHub repository's README file, reachable at https://github.com/mrkmg/node-external-editor
. This source documentation includes comprehensive installation instructions, usage guidelines, API details, error handling guidance, and more, giving developers all the necessary information on how to integrate and use this package effectively.