Skip to content

Commit

Permalink
Fixing node.js and npm PATH problem, loading automatically the PATH f…
Browse files Browse the repository at this point in the history
…rom the user's shell env.
  • Loading branch information
pichillilorenzo committed Jan 25, 2018
1 parent bf6b3fc commit 8a19fd7
Show file tree
Hide file tree
Showing 5 changed files with 237 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
import os, sys, imp, platform, json, traceback, threading, urllib, shutil, re, time
from shutil import copyfile
from threading import Timer
from os import environ
from subprocess import Popen, PIPE

PLUGIN_VERSION = "0.13.15"
PLUGIN_VERSION = "0.13.16"

PACKAGE_PATH = os.path.abspath(os.path.dirname(__file__))
PACKAGE_NAME = os.path.basename(PACKAGE_PATH)
Expand Down Expand Up @@ -5984,10 +5986,79 @@ def start():

mainPlugin.init()

##
## start - Fix Mac Path plugin code with some fixes
##

fixPathSettings = None
fixPathOriginalEnv = {}

def getSysPath():
command = ""
if platform.system() == "Darwin":
command = "TERM=ansi CLICOLOR=\"\" SUBLIME=1 /usr/bin/login -fqpl $USER $SHELL -l -c 'TERM=ansi CLICOLOR=\"\" SUBLIME=1 printf \"%s\" \"$PATH\"'"
elif platform.system() == "Linux":
command = "TERM=ansi CLICOLOR=\"\" SUBLIME=1 $SHELL --login -c 'TERM=ansi CLICOLOR=\"\" printf \"%s\" $PATH'"
else:
return ""

# Execute command with original environ. Otherwise, our changes to the PATH propogate down to
# the shell we spawn, which re-adds the system path & returns it, leading to duplicate values.
sysPath = Popen(command, stdout=PIPE, shell=True, env=fixPathOriginalEnv).stdout.read()

sysPathString = sysPath.decode("utf-8")
# Remove ANSI control characters (see: http://www.commandlinefu.com/commands/view/3584/remove-color-codes-special-characters-with-sed )
sysPathString = re.sub(r'\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]', '', sysPathString)
sysPathString = sysPathString.strip().rstrip(':')

# Decode the byte array into a string, remove trailing whitespace, remove trailing ':'
return sysPathString

def fixPath():
currSysPath = getSysPath()
# Basic sanity check to make sure our new path is not empty
if len(currSysPath) < 1:
return False

environ['PATH'] = currSysPath

for pathItem in fixPathSettings.get("additional_path_items", []):
environ['PATH'] = pathItem + ':' + environ['PATH']

return True


def plugin_unloaded():
# When we unload, reset PATH to original value. Otherwise, reloads of this plugin will cause
# the PATH to be duplicated.
environ['PATH'] = fixPathOriginalEnv['PATH']

global fixPathSettings
fixPathSettings.clear_on_change('fixpath-reload')

##
## end - Fix Mac Path plugin code
##

def plugin_loaded():

if int(sublime.version()) >= 3124 :

if platform.system() == "Darwin" or platform.system() == "Linux":
global fixPathSettings
fixPathSettings = sublime.load_settings("Preferences.sublime-settings")
fixPathSettings.clear_on_change('fixpath-reload')
fixPathSettings.add_on_change('fixpath-reload', fixPath)

# Save the original environ (particularly the original PATH) to restore later
global fixPathOriginalEnv
for key in environ:
fixPathOriginalEnv[key] = environ[key]

fixPath()

sublime.set_timeout_async(start, 1000)

else:
response = sublime.yes_no_cancel_dialog("JavaScript Enhancements plugin requires Sublime Text 3 (build 3124 or newer). Your build is: " + sublime.version() + ". Do you want open the download page?", "Yes, open it", "No")
if response == sublime.DIALOG_YES:
Expand Down
90 changes: 90 additions & 0 deletions changelog/0.13.16.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
v0.13.16

## Fixes

- Fixing node.js and npm PATH problem, loading automatically the PATH from the user's shell env.



=================================================================
** THIS PLUGIN IS IN BETA! Thanks for your support in advance! **
=================================================================

If you like it, remember to star it ⭐ on GitHub: https://github.com/pichillilorenzo/JavaScriptEnhancements

** USAGE **
===========

See how it works on the Wiki: 👉👉 https://github.com/pichillilorenzo/JavaScriptEnhancements/wiki 👈👈


** WHAT IS THIS? **
===================

This plugin uses Flow (javascript static type checker from Facebook) under the hood.

It offers better javascript autocomplete and a lot of features about creating,
developing and managing javascript projects, such as:

- Cordova projects (run cordova emulate, build, compile, serve, etc. directly from Sublime Text!)
- Ionic v1 and v2 projects (same as Cordova projects!)
- Angular v1 and v2 projects
- React projects (only about the creation at this moment)
- React Native projects (only about the creation at this moment. I will add also NativeScript support)
- Express projects (only about the creation at this moment)
- Yeoman generators
- Local bookmarks project
- JavaScript real-time errors
- etc.

You could use it also in existing projects (see the Wiki - https://github.com/pichillilorenzo/JavaScriptEnhancements/wiki/Using-it-with-an-existing-project)!

It turns Sublime Text into a JavaScript IDE like!

This project is based on my other Sublime Text plugin JavaScript Completions (https://github.com/pichillilorenzo/JavaScript-Completions)

** NOTE **
If you want use this plugin, you may want uninstall/disable the JavaScript Completions plugin, if installed.

** OS SUPPORTED NOW **
======================

👉 Linux (64-bit)
👉 Mac OS X
👉 Windows (64-bit): released without the use of TerminalView plugin. For each feature (like also creating a project) will be used the cmd.exe shell (so during the creation of a project don't close it until it finishes!). Unfortunately the TerminalView plugin supports only Linux-based OS 😞. Has someone any advice or idea about that? Is there something similar to the TerminalView plugin for Windows?? Thanks!

❗❗ Dependencies ❗❗
=======================

In order to work properly, this plugin has some dependencies:

👉 Sublime Text 3 (build 3124 or newer)
👉 Node.js and npm (https://nodejs.org or nvm (https://github.com/creationix/nvm))
👉 TerminalView (only for Linux and Mac OS X) sublime text plugin (https://github.com/Wramberg/TerminalView)

Not required, but useful for typescript files (Flow wont work on this type of files):

👉 TypeScript sublime text plugin (https://github.com/Microsoft/TypeScript-Sublime-Plugin)

** Flow Requirements **
=======================

It use [Flow](https://github.com/facebook/flow) for type checking and auto-completions.

👉 Mac OS X
👉 Linux (64-bit)
👉 Windows (64-bit)

Email me for any questions or doubts about this new project on: [email protected]

** Donation **
==============

If this project help you reduce time to develop and also you like it, please support it with a donation 😄👍. Thanks!

Open Collective: https://opencollective.com/javascriptenhancements/donate
PayPal: https://www.paypal.me/LorenzoPichilli

Thanks anyway for your support! 😄😄

MIT License
2 changes: 1 addition & 1 deletion helper/can_i_use/can_i_use_data.json

Large diffs are not rendered by default.

73 changes: 72 additions & 1 deletion make/_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
import os, sys, imp, platform, json, traceback, threading, urllib, shutil, re, time
from shutil import copyfile
from threading import Timer
from os import environ
from subprocess import Popen, PIPE

PLUGIN_VERSION = "0.13.15"
PLUGIN_VERSION = "0.13.16"

PACKAGE_PATH = os.path.abspath(os.path.dirname(__file__))
PACKAGE_NAME = os.path.basename(PACKAGE_PATH)
Expand Down Expand Up @@ -185,10 +187,79 @@ def start():

mainPlugin.init()

##
## start - Fix Mac Path plugin code with some fixes
##

fixPathSettings = None
fixPathOriginalEnv = {}

def getSysPath():
command = ""
if platform.system() == "Darwin":
command = "TERM=ansi CLICOLOR=\"\" SUBLIME=1 /usr/bin/login -fqpl $USER $SHELL -l -c 'TERM=ansi CLICOLOR=\"\" SUBLIME=1 printf \"%s\" \"$PATH\"'"
elif platform.system() == "Linux":
command = "TERM=ansi CLICOLOR=\"\" SUBLIME=1 $SHELL --login -c 'TERM=ansi CLICOLOR=\"\" printf \"%s\" $PATH'"
else:
return ""

# Execute command with original environ. Otherwise, our changes to the PATH propogate down to
# the shell we spawn, which re-adds the system path & returns it, leading to duplicate values.
sysPath = Popen(command, stdout=PIPE, shell=True, env=fixPathOriginalEnv).stdout.read()

sysPathString = sysPath.decode("utf-8")
# Remove ANSI control characters (see: http://www.commandlinefu.com/commands/view/3584/remove-color-codes-special-characters-with-sed )
sysPathString = re.sub(r'\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]', '', sysPathString)
sysPathString = sysPathString.strip().rstrip(':')

# Decode the byte array into a string, remove trailing whitespace, remove trailing ':'
return sysPathString

def fixPath():
currSysPath = getSysPath()
# Basic sanity check to make sure our new path is not empty
if len(currSysPath) < 1:
return False

environ['PATH'] = currSysPath

for pathItem in fixPathSettings.get("additional_path_items", []):
environ['PATH'] = pathItem + ':' + environ['PATH']

return True


def plugin_unloaded():
# When we unload, reset PATH to original value. Otherwise, reloads of this plugin will cause
# the PATH to be duplicated.
environ['PATH'] = fixPathOriginalEnv['PATH']

global fixPathSettings
fixPathSettings.clear_on_change('fixpath-reload')

##
## end - Fix Mac Path plugin code
##

def plugin_loaded():

if int(sublime.version()) >= 3124 :

if platform.system() == "Darwin" or platform.system() == "Linux":
global fixPathSettings
fixPathSettings = sublime.load_settings("Preferences.sublime-settings")
fixPathSettings.clear_on_change('fixpath-reload')
fixPathSettings.add_on_change('fixpath-reload', fixPath)

# Save the original environ (particularly the original PATH) to restore later
global fixPathOriginalEnv
for key in environ:
fixPathOriginalEnv[key] = environ[key]

fixPath()

sublime.set_timeout_async(start, 1000)

else:
response = sublime.yes_no_cancel_dialog("JavaScript Enhancements plugin requires Sublime Text 3 (build 3124 or newer). Your build is: " + sublime.version() + ". Do you want open the download page?", "Yes, open it", "No")
if response == sublime.DIALOG_YES:
Expand Down
3 changes: 2 additions & 1 deletion messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
"0.13.12": "changelog/0.13.12.txt",
"0.13.13": "changelog/0.13.13.txt",
"0.13.14": "changelog/0.13.14.txt",
"0.13.15": "changelog/0.13.15.txt"
"0.13.15": "changelog/0.13.15.txt",
"0.13.16": "changelog/0.13.16.txt"
}

0 comments on commit 8a19fd7

Please sign in to comment.