Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
external-editor
Advanced tools
Edit a string with the users preferred text editor using $VISUAL or $ENVIRONMENT
The external-editor npm package allows you to easily invoke and manage external text editors from within your Node.js applications. It is particularly useful when you need to allow users to edit large multi-line texts, which can be cumbersome to handle within a command-line interface. The package takes care of launching the editor, capturing the text once the editor is closed, and handling temporary files.
Launch an external editor and get the edited text
This feature launches the default system editor, waits for the user to make changes and save, and then returns the edited text.
const { edit } = require('external-editor');
const text = edit();
console.log(text);
Launch an external editor with predefined text
This feature allows you to launch an external editor with predefined text, which the user can then edit.
const { edit } = require('external-editor');
const initialText = 'Initial content of the file.';
const text = edit(initialText);
console.log(text);
Create an instance of ExternalEditor
This feature provides more control by creating an instance of ExternalEditor, which can be used to run the editor and clean up temporary files manually.
const { ExternalEditor } = require('external-editor');
const editor = new ExternalEditor();
const text = editor.run();
editor.cleanup();
console.log(text);
Inquirer is a comprehensive library for creating interactive command-line interfaces. While it does not directly provide functionality to launch external editors, it offers a wide range of input types, including an 'editor' input type that allows users to input multi-line text using an embedded editor.
The 'open' package is used to open files or URLs in the user's preferred application. While it is not specifically designed for text editing, it can be used to open text files in the default editor. However, it does not manage the editing process or handle temporary files like external-editor does.
Enquirer is another library for creating stylish command-line interfaces with various prompts. Similar to Inquirer, it includes a prompt type for editing text, but it does not specifically focus on launching external editors or managing temporary files.
A node module to edit a string with a users preferred text editor using $VISUAL or $ENVIRONMENT.
Version: 1.1.0
##Install
npm install external-editor --save
##Usage
A simple example using the .edit
convenience method
var ExternalEditor = require('external-editor')
var data = ExternalEditor.edit('\n\n# Please write your text above');
console.log(data);
A full featured example
var ExternalEditor = require('external-editor');
try {
var editor = new ExternalEditor();
var text = editor.run()
// the text is also available in editor.text
} catch (err) {
if (err instanceOf ExternalEditor.CreateFileError) {
console.log('Failed to create the temporary file');
} else if (err instanceOf ExternalEditor.ReadFileError) {
console.log('Failed to read the temporary file');
} else if (err instanceOf ExternalEditor.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 ExternalEditor.RemoveFileError) {
console.log('Failed to remove the temporary file');
} else {
throw err
}
}
####API Static Methods
edit(text)
text
(string) Optional Defaults to empty stringCreateFileError
, ReadFileError
, or LaunchEditorError
, or RemoveFileError
editAsync(text, callback)
text
(string) Optional Defaults to empty stringcallback
(function (error, text))
error
could be of type CreateFileError
, ReadFileError
, or LaunchEditorError
, or RemoveFileError
text
(string) The contents of the fileStatic Properties
CreateFileError
Error thrown if the temporary file could not be created.ReadFileError
Error thrown if the temporary file could not be read.RemoveFileError
Error thrown if the temporary file could not be removed during cleanup.LaunchEditorError
Error thrown if the editor could not be launched.Public Methods
new ExternalEditor(text)
text
(string) Optional Defaults to empty stringCreateFileError
run()
Launches the editor.
LaunchEditorError
or ReadFileError
runAsync(callback)
Launches the editor in an async way
callback
(function (error, text))
error
could be of type ReadFileError
or LaunchEditorError
text
(string) The contents of the filecleanup()
Removes the temporary file.
RemoveFileError
Public Properties
text
(string) readonly The text in the temporary file.editor.bin
(string) The editor determined from the environment.editor.args
(array) Default arguments for the bintemp_file
(string) Path to temporary file. Can be changed, but be careful as the temporary file probably already
exists and would need be removed manually.##Errors
All errors have a simple message explaining what went wrong. They all also have an original_error
property containing
the original error thrown for debugging purposes.
##Why Synchronous?
Everything is synchronous to make sure the editor has complete control of the stdin and stdout. Testing has shown async launching of the editor can lead to issues when using readline or other packages which try to read from stdin or write to stdout. Seeing as this will be used in an interactive CLI environment, I made the decision to force the package to be synchronous. If you know a reliable way to force all stdin and stdout to be limited only to the child_process, please submit a PR.
If async is really needed, you can use editAsync
or runAsync
. If you are using readline or have anything else
listening to the stdin or you write to stdout, you will most likely have problem, so make sure to remove any other
listeners on stdin, stdout, or stdin.
##Demo
##License
The MIT License (MIT)
Copyright (c) 2016 Kevin Gravier
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
FAQs
Edit a string with the users preferred text editor using $VISUAL or $ENVIRONMENT
The npm package external-editor receives a total of 20,239,613 weekly downloads. As such, external-editor popularity was classified as popular.
We found that external-editor demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.