\href{https://travis-ci.org/mrkmg/node-external-editor/branches}{\texttt{ }} \href{https://www.npmjs.com/package/external-editor}{\texttt{ }} \href{https://opensource.org/licenses/MIT}{\texttt{ }}

A node module to edit a string with a users preferred text editor using \$\+VISUAL or \$\+ENVIRONMENT.

Version\+: 3.\+1.\+0

As of version 3.\+0.\+0, the minimum version of node supported is 4.\hypertarget{md_sfcode_node_modules_external_editor_README_autotoc_md10087}{}\doxysection{Install}\label{md_sfcode_node_modules_external_editor_README_autotoc_md10087}
{\ttfamily npm install external-\/editor -\/-\/save}\hypertarget{md_sfcode_node_modules_external_editor_README_autotoc_md10088}{}\doxysection{Usage}\label{md_sfcode_node_modules_external_editor_README_autotoc_md10088}
A simple example using the {\ttfamily .edit} convenience method \begin{DoxyVerb}import {edit} from "external-editor";
const data = edit('\n\n# Please write your text above');
console.log(data);
\end{DoxyVerb}
 A full featured example \begin{DoxyVerb}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
    }
}
\end{DoxyVerb}
\hypertarget{md_sfcode_node_modules_external_editor_README_autotoc_md10089}{}\doxysubsubsection{API}\label{md_sfcode_node_modules_external_editor_README_autotoc_md10089}
{\bfseries{Convenience Methods}}


\begin{DoxyItemize}
\item {\ttfamily edit(text, config)}
\begin{DoxyItemize}
\item {\ttfamily text} (string) {\itshape Optional} Defaults to empty string
\item {\ttfamily config} (Config) {\itshape Optional} Options for temporary file creation
\item {\bfseries{Returns}} (string) The contents of the file
\item Could throw {\ttfamily Create\+File\+Error}, {\ttfamily Read\+File\+Error}, or {\ttfamily Launch\+Editor\+Error}, or {\ttfamily Remove\+File\+Error}
\end{DoxyItemize}
\item {\ttfamily edit\+Async(text, callback, config)}
\begin{DoxyItemize}
\item {\ttfamily text} (string) {\itshape Optional} Defaults to empty string
\item {\ttfamily callback} (function (error, text))
\begin{DoxyItemize}
\item {\ttfamily error} could be of type {\ttfamily Create\+File\+Error}, {\ttfamily Read\+File\+Error}, or {\ttfamily Launch\+Editor\+Error}, or {\ttfamily Remove\+File\+Error}
\item {\ttfamily text}(string) The contents of the file
\end{DoxyItemize}
\item {\ttfamily config} (Config) {\itshape Optional} Options for temporary file creation
\end{DoxyItemize}
\end{DoxyItemize}

{\bfseries{Errors}}


\begin{DoxyItemize}
\item {\ttfamily Create\+File\+Error} Error thrown if the temporary file could not be created.
\item {\ttfamily Read\+File\+Error} Error thrown if the temporary file could not be read.
\item {\ttfamily Remove\+File\+Error} Error thrown if the temporary file could not be removed during cleanup.
\item {\ttfamily Launch\+Editor\+Error} Error thrown if the editor could not be launched.
\end{DoxyItemize}

{\bfseries{External Editor Public Methods}}


\begin{DoxyItemize}
\item {\ttfamily new External\+Editor(text, config)}
\begin{DoxyItemize}
\item {\ttfamily text} (string) {\itshape Optional} Defaults to empty string
\item {\ttfamily config} (Config) {\itshape Optional} Options for temporary file creation
\item Could throw {\ttfamily Create\+File\+Error}
\end{DoxyItemize}
\item {\ttfamily run()} Launches the editor.
\begin{DoxyItemize}
\item {\bfseries{Returns}} (string) The contents of the file
\item Could throw {\ttfamily Launch\+Editor\+Error} or {\ttfamily Read\+File\+Error}
\end{DoxyItemize}
\item {\ttfamily run\+Async(callback)} Launches the editor in an async way
\begin{DoxyItemize}
\item {\ttfamily callback} (function (error, text))
\begin{DoxyItemize}
\item {\ttfamily error} could be of type {\ttfamily Read\+File\+Error} or {\ttfamily Launch\+Editor\+Error}
\item {\ttfamily text}(string) The contents of the file
\end{DoxyItemize}
\end{DoxyItemize}
\item {\ttfamily cleanup()} Removes the temporary file.
\begin{DoxyItemize}
\item Could throw {\ttfamily Remove\+File\+Error}
\end{DoxyItemize}
\end{DoxyItemize}

{\bfseries{External Editor Public Properties}}


\begin{DoxyItemize}
\item {\ttfamily text} (string) {\itshape readonly} The text in the temporary file.
\item {\ttfamily editor.\+bin} (string) The editor determined from the environment.
\item {\ttfamily editor.\+args} (array) Default arguments for the bin
\item {\ttfamily temp\+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.
\item {\ttfamily last\+Exit\+Status} (number) The last exit code emitted from the editor.
\end{DoxyItemize}

{\bfseries{Config Options}}


\begin{DoxyItemize}
\item {\ttfamily prefix} (string) {\itshape Optional} A prefix for the file name.
\item {\ttfamily postfix} (string; {\itshape Optional} A postfix for the file name. Useful if you want to provide an extension.
\item {\ttfamily mode} (number) {\itshape Optional} Which mode to create the file with. e.\+g. 644
\item {\ttfamily template} (string) {\itshape Optional} A template for the filename. See \href{https://www.npmjs.com/package/tmp}{\texttt{ tmp}}.
\item {\ttfamily dir} (string) {\itshape Optional} Which path to store the file.
\end{DoxyItemize}\hypertarget{md_sfcode_node_modules_external_editor_README_autotoc_md10090}{}\doxysection{Errors}\label{md_sfcode_node_modules_external_editor_README_autotoc_md10090}
All errors have a simple message explaining what went wrong. They all also have an {\ttfamily original\+Error} property containing the original error thrown for debugging purposes.\hypertarget{md_sfcode_node_modules_external_editor_README_autotoc_md10091}{}\doxysection{Why Synchronous?}\label{md_sfcode_node_modules_external_editor_README_autotoc_md10091}
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 {\ttfamily edit\+Async} or {\ttfamily run\+Async}. 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 stderr.\hypertarget{md_sfcode_node_modules_external_editor_README_autotoc_md10092}{}\doxysection{Demo}\label{md_sfcode_node_modules_external_editor_README_autotoc_md10092}
\href{https://asciinema.org/a/a1qh9lypbe65mj0ivfuoslz2s}{\texttt{ }}\hypertarget{md_sfcode_node_modules_external_editor_README_autotoc_md10093}{}\doxysection{Breaking Changes from v2 to v3}\label{md_sfcode_node_modules_external_editor_README_autotoc_md10093}

\begin{DoxyItemize}
\item Node\+JS 0.\+12 support dropped.
\item Switched to named imports.
\item All \char`\"{}snake\+\_\+cased\char`\"{} variables and properties are now \char`\"{}camel\+Cased\char`\"{}.
\begin{DoxyItemize}
\item {\ttfamily External\+Editor.\+temp\+\_\+file} is now {\ttfamily External\+Editor.\+temp\+File}.
\item {\ttfamily External\+Editor.\+last\+\_\+exit\+\_\+status} is now {\ttfamily External\+Editor.\+last\+Exit\+Status}.
\item {\ttfamily Error.\+original\+\_\+error} is now {\ttfamily Error.\+original\+Error}.
\end{DoxyItemize}
\end{DoxyItemize}\hypertarget{md_sfcode_node_modules_external_editor_README_autotoc_md10094}{}\doxysection{License}\label{md_sfcode_node_modules_external_editor_README_autotoc_md10094}
The MIT License (MIT)

Copyright (c) 2016-\/2018 Kevin Gravier

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \char`\"{}\+Software\char`\"{}), 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 \char`\"{}\+AS IS\char`\"{}, 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. 