launch-editor-middleware

express middleware for launching editor


Keywords
express, middleware, editor
License
MIT
Install
npm install launch-editor-middleware@2.6.1

Documentation

launch-editor

Open file with line numbers in editor from Node.js.

The main functionality is extracted from react-dev-utils with slight modifications so that it can be used as a standalone package. The original source code is licensed under MIT.

Also added column number support.

Why

There are also a few other existing packages with the same purpose:

However, both expects env variables like EDITOR to be set in order to open files. This package infers the editor to open by checking current running processes before falling back to env variables.

On the other hand,react-dev-utils includes many other utilities and dependencies and is thus not suitable for standalone usage.

Usage

const launch = require('launch-editor')

launch(
  // filename:line:column
  // both line and column are optional
  'foo.js:12:34',
  // try specific editor bin first (optional)
  'code',
  // callback if failed to launch (optional)
  (fileName, errorMsg) => {
    // log error if any
  }
)

Middleware

An express/connect/webpack-dev-server compatible middleware is also available:

const launchMiddleware = require('launch-editor-middleware')

app.use('/__open-in-editor', launchMiddleware())

The middleware factory function accepts the following arguments (all optional, the callback can be in any position as long as it's the last argument):

  1. A specific editor bin to try first. Defaults to inferring from running processes, then fallback to env variables like EDITOR and VISUAL.
  2. The root directory of source files, in case the file path is relative. Defaults to process.cwd().
  3. a callback when it fails to launch the editor.

To launch files, send requests to the server like the following:

/__open-in-editor?file=src/main.js:13:24

Supported editors

Value Editor Linux Windows OSX
appcode AppCode βœ“
atom Atom βœ“ βœ“ βœ“
atom-beta Atom Beta βœ“
brackets Brackets βœ“ βœ“ βœ“
clion Clion βœ“ βœ“
code Visual Studio Code βœ“ βœ“ βœ“
code-insiders Visual Studio Code Insiders βœ“ βœ“ βœ“
codium VSCodium βœ“ βœ“ βœ“
emacs Emacs βœ“
idea IDEA βœ“ βœ“ βœ“
notepad++ Notepad++ βœ“
pycharm PyCharm βœ“ βœ“ βœ“
phpstorm PhpStorm βœ“ βœ“ βœ“
rubymine RubyMine βœ“ βœ“ βœ“
sublime Sublime Text βœ“ βœ“ βœ“
vim Vim βœ“
visualstudio Visual Studio βœ“
webstorm WebStorm βœ“ βœ“ βœ“

Custom editor support

You can use the LAUNCH_EDITOR environment variable

to force a specific supported editor

LAUNCH_EDITOR=codium

to run a custom launch script

LAUNCH_EDITOR=my-editor-launcher.sh
# gets called with 3 args: filename, line, column
filename=$1
line=$2
column=$3

# call your editor with whatever args it expects
my-editor -l $line -c $column -f $filename