mikerourke
3/2/2017 - 5:07 PM

Open WebStorm files through the command line using Git Bash on Windows.

Open WebStorm files through the command line using Git Bash on Windows.

#!/bin/bash
#
# Opens WebStorm from the command line using Git Bash for Windows.  If you're 
# working within the integrated terminal in WebStorm, this will open the file
# passed in using just the relative path. If the file specified doesn't exist,
# it creates it and opens it.  Just copy wstorm() into wherever you keep your 
# Bash aliases and make sure you change your Terminal path to Git Bash (see 
# Notes section).
#
# ------
# Notes:
# ------
# If you want this to work in WebStorm, you have to change the terminal path in
# Tools > Terminal to the following (quotes included):
# "C:\Program Files\Git\bin\sh.exe" -login -i
# If you have 32-bit Git installed, add " (x86)" to "Program Files".
# FYI, I got this working with version 2016.3.2.
#
# -----------------
# From the website:
# -----------------
# In order to open a file from WebStorm through the command line, the format
# needs to be as follows:
# <WebStorm> <path1> --line <number> <path2>
# where:
# <WebStorm> is the platform-specific product launcher
# <path1> is the path to the project that contains the desired file
# <number> is the number of the line, where the caret should rest
# <path2> is the path to the file to be opened
#
#  www.jetbrains.com/help/webstorm/2016.3/opening-files-from-command-line.html
# -----------------------------------------------------------------------------
wstorm() {
  # Since the WebStorm binary is stored in a folder contingent on the version
  # installed, search for the folder matching "WebStorm" and inject that into
  # the path to the executable.
  ws_root=`ls -1d /c/Program\ Files\ \(x86\)/JetBrains/WebStorm\ * | tail -n1`
  ws_folder="$(basename "$ws_root")"
  ws_exe="C:\\Program Files (x86)\\JetBrains\\$ws_folder\\bin\\webstorm.exe"
  
  wd=$(pwd)
  path_arg="$1"

  if [ $path_arg ]; then
    # If the first three characters are "/c/", then it's a full
    # path.
    hasRoot=$([ ${path_arg:0:3} == "/c/" ] && echo "true" || echo "false")
    
    # If the last character was a slash or dot, it's a directory,
    # otherwise it's a file with a relative path.
    last_path_char=${path_arg: -1}
    if ([ $last_path_char != "/" ] && [ $last_path_char != "." ]); then
      full_path="$wd/$path_arg"
      base_path="$(dirname "$full_path")"

      # Creates the file if it doesn't exist, if you don't want to use this,
      # just comment it out.
      if [ ! -e "$full_path" ]; then
        # If you specify a file in a directory that doesn't exist, create it
        # create the file, and open it.
        if [ ! -d "$base_path" ]; then
          # If you want to create and open a file in a subdirectory of a
          # subdirectory of a subdirectory, go buck wild, this will make it
          # happen!
          mkdir -p $base_path
        fi
        touch $full_path
      fi
      
      "$ws_exe" $wd --line 1 $full_path
    fi

    # If it's a directory, the [line] and [file path] arguments aren't
    # required.
    if [ -d "$1" ]; then
      "$ws_exe" $wd
    fi
  else
    # If no path argument was specified, inform the user and exit:
    echo "Please specify a path or file name."
  fi
}