Coder Social home page Coder Social logo

launch-editor's Introduction

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

launch-editor's People

Contributors

anteriovieira avatar dependabot[bot] avatar dominikg avatar elmassimo avatar justmoon avatar kirisakow avatar nrayburn-tech avatar pi0 avatar samverschueren avatar sapphi-red avatar sodatea avatar trysound avatar yyx990803 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

launch-editor's Issues

feat: add filename validation for windows

The vue-inspector by @webfansplz contains code that prevents some filenames from being opened with a shell command

https://github.com/webfansplz/vite-plugin-vue-inspector/blob/main/src/launch-editor.ts#L336-L357

This is to prevent possible attacks where a crafted filename could be used to execute malicious commands.

Currently launch-editor does not have a check like this. It's hard to exploit because there is an fs.existsSync check but for defense in depth it would be better to not let these kinds of filenames through

The regex here https://github.com/webfansplz/vite-plugin-vue-inspector/blob/main/src/launch-editor.ts#L108-L112 looks longish, maybe a smaller list of forbidden chars does the trick too

Improve Jetbrains Toolbox App Detection (macOS)

Jetbrains toolbox doesn't install the application in /Applications for macOS. This breaks most (all?) of the assumptions that the current osx.js file has for JetBrains IDEs.
I don't know if there is any impact on Unix or Windows.

Example executable path for WebStorm.

/Users/nrayburn/Library/Application Support/JetBrains/Toolbox/apps/WebStorm/ch0/222.3345.108/WebStorm.app/Contents/MacOS/webstorm

I don't know what the best solution is, so far the best I have come up with is find the process that ends with /WebStorm.app/Contents/MacOS/webstorm and then use that as the executable to launch. However, I don't know if this works in all cases.
I can submit a PR for this, or another solution if there are any better ideas.

Open component with IntelliJ 2017.3

I am getting an error with IntelliJ whilst opening a component trough the Vue Dev Tools.

schermafbeelding 2018-01-14 om 15 25 39

I'm using a MacBook Pro with High Sierra 10.13.2 and the following setup:
Versions:

webpack: 3.10.0
webpack-dev-server: 2.10.1
yarn: 1.3.2
node: 8.5.0

While having HMR enabled I get the following output in my terminal:

webpack: Compiled successfully.
2018-01-14 15:43:32.577 idea[36772:3014555] allVms required 1.8*,1.8+
2018-01-14 15:43:32.580 idea[36772:3014563] Value of IDEA_VM_OPTIONS is (null)
2018-01-14 15:43:32.580 idea[36772:3014563] fullFileName is: /Applications/IntelliJ IDEA.app/Contents/bin/idea.vmoptions
2018-01-14 15:43:32.580 idea[36772:3014563] fullFileName exists: /Applications/IntelliJ IDEA.app/Contents/bin/idea.vmoptions
2018-01-14 15:43:32.581 idea[36772:3014563] Processing VMOptions file at /Applications/IntelliJ IDEA.app/Contents/bin/idea.vmoptions
2018-01-14 15:43:32.581 idea[36772:3014563] Done
Already running

This is my code to run the webpack-dev-server:

  devServer: {
      contentBase: commonPaths.publicPath,
      hot: true,
      overlay: true,
      headers: {
        'Access-Control-Allow-Origin': '*',
        'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, PATCH, OPTIONS',
        'Access-Control-Allow-Headers': 'X-Requested-With, content-type, Authorization'
      },
      allowedHosts: [
        'localhost'
      ],
      before(app){
        app.use('/__open-in-editor', openInEditor())
      }
    },

If you need more information, just let me know and I will try to provide you with it.

webpack-dev-server - wrong hostname and port

Our webpack-dev-server is configured to listen on port 8081 but the __open-in-editor route is initiated on 8888 which is the port used by nginx and Symfony framework

image

Hence there's a 404 Not Found response

image

image

Please fix so that the middleware will reuse the port specified for webpack-dev-server. Here we launch it with webpack-dev-server --hot --port 8081

Currently can't open webstorm from MacOS with m1

Currently output if i try launchEditor(filePath)

2022-04-18 18:02:27.126 webstorm[50917:1148523] allVms required 1.8*,1.8+
2022-04-18 18:02:27.127 webstorm[50917:1148527] Cannot load JVM bundle: Error Domain=NSCocoaErrorDomain Code=3585 "dlopen_preflight(/Applications/WebStorm.app/Contents/jbr/Contents/MacOS/libjli.dylib) => false, tried: '/Applications/WebStorm.app/Contents/jbr/Contents/MacOS/libjli.dylib' (mach-o file, but is an incompatible architecture (have 'arm64', need 'x86_64')), '/usr/lib/libjli.dylib' (no such file)" UserInfo={NSLocalizedFailureReason=The bundle doesn’t contain a version for the current architecture., NSLocalizedRecoverySuggestion=Try installing a universal version of the bundle., NSFilePath=/Applications/WebStorm.app/Contents/jbr/Contents/MacOS/libjli.dylib, NSDebugDescription=dlopen_preflight(/Applications/WebStorm.app/Contents/jbr/Contents/MacOS/libjli.dylib) => false, tried: '/Applications/WebStorm.app/Contents/jbr/Contents/MacOS/libjli.dylib' (mach-o file, but is an incompatible architecture (have 'arm64', need 'x86_64')), '/usr/lib/libjli.dylib' (no such file), NSBundlePath=/Applications/WebStorm.app/Contents/jbr, NSLocalizedDescription=The bundle “OpenJDK 11.0.13” couldn’t be loaded because it doesn’t contain a version for the current architecture.}

Could not open index.vue in the editor.
The editor process exited with an error: (code 255).

If i try launchEditor(filePath, 'webstorm'), all worked fine, but i think it's not normal

Remote support

When working from home, I connect to my office computer using vscode Remote Development feature. Currently the launch-editor uses -r -g arguments to open vscode but when working with remote development we need a different set of arguments. Otherwise it tries to find and open the file on the local machine (which does not exist)

https://github.com/yyx990803/launch-editor/blob/master/packages/launch-editor/get-args.js#L43

    case 'code':
    case 'Code':
    case 'code-insiders':
    case 'Code - Insiders':
    case 'codium':
    case 'vscodium':
    case 'VSCodium':
      return ['-r', '-g', `${fileName}:${lineNumber}:${columnNumber}`]

When working with remote development the arguments need to be these:

$ code -r --remote ssh-remote+192.168.0.17 -g /home/martin/nuxt/components/HelloWorld.vue:3

The 192.168.0.17 is the name of the host found in ~/.ssh/config. I guess this part would need to be configurable or passed as query arg like /__open-in-editor?file=src/main.js&remote=NAME_OF_REMOTE

The --remote option wasn't well documented, it does not show up when you type code --help but I found it on this issue and it does indeed work - microsoft/vscode-remote-release#5083

Missing license file in NPM packages

The NPM packages published to the registry do not contain the LICENSE (or the README.md) file. This results in tools like license-checker not being able to determine the license file for those packages. Other libraries with multi-package repositories handle this by copying the LICENSE and README.md file from the root directory into the build results directory during the build or release process. Alternatively you could also simply copy the files into the package directories.

MacOS VS Code

const openInEditor = require('launch-editor', 'code')
openInEditor('./shells/dev/target/Target.vue')

capture d ecran 2018-01-19 a 11 10 28

Linux IntelliJ

linux is missing the entries for IntelliJ if it was installed as snap package.

Commands are:

intellij-idea-ultimate
intellij-idea-community

The problem is that they both run as idea.sh

My proposal would be to change the PS command to: ps x --no-heading -o comm -o cmd --sort=comm

Then use regular expressions for the programs, e.g.:

idea.sh.*intellij-idea-ultimate.*

Fails when editor is installed in directory containing spaces on windows

My vscode is installed in C:\Users\Isabella Skořepová\AppData\Local\Programs\Microsoft VS Code\Code.exe, which is the default installation location. The first space is mostly my fault created when I was installing the system, but the other two are added by vscode installer.

It fails with error

'C:\Users\Isabella' is not recognized as an internal or external command, operable program or batch file.

Sidenote: I am using vite, but I see the same issue when trying to import the middleware directly in a vite.config.ts file.

WSL Support

It doesn't appear to work when serving from Windows Subsystem for Linux.

Is this a possibility?

Cannot open components with webstorm: spawn webstorm ENOENT.

I'm using launch-editor with webpack.

When I try to open a component in Webstorm, the following message is displayed in the dev-server console:

Could not open App.vue in the editor.
The editor process exited with an error: spawn webstorm ENOENT.

The browser console says File src/vue/App.vue opened in editor, and it works well with other editors so I guess it's well configured.

Any idea how to fix this ?
Thanks

seems to assume a particular install location on mac

My setup on macOS has Sublime in the user's Applications folder, not the system folder:

  • Sublime installed in
    ~/Applications/Sublime Text.app
  • Path includes ~/bin
  • Symlink
    ~/bin/subl -> ~/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl

error when using in vue dev tools:

Could not open ReportsLessonListPage.vue in the editor.
The editor process exited with an error: spawn /Applications/Sublime Text.app/Contents/SharedSupport/bin/subl ENOENT.

Support `file:` protocol

Currently file protocol (e.g. /__open-in-editor?file=file%3A%2F%2F%2FC%3A%2Ffoo.js:13:24) is not supported.

Because using fileURLToPath in browser requires a polyfill, I think it would be nice if file protocol is supported.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.