Skip to content

Instantly share code, notes, and snippets.

@UltraInstinct05
Last active May 6, 2020 08:57
Show Gist options
  • Save UltraInstinct05/e5df44c6eb6b942aac0d659cfc0efdeb to your computer and use it in GitHub Desktop.
Save UltraInstinct05/e5df44c6eb6b942aac0d659cfc0efdeb to your computer and use it in GitHub Desktop.
import os
import sublime
import sublime_plugin
def get_npm_scripts(chosen_directory):
""" Gets the list of npm scripts defined in the package.json
Args:
chosen_directory (str) : The absolute path of the chosen directory.
Returns:
npm_scripts (list) : The list of npm scripts defined in package.json
"""
package_json_path = os.path.join(chosen_directory, "package.json")
with open(package_json_path) as package_json:
json_data = sublime.decode_value(package_json.read())
npm_scripts = [key for key in json_data["scripts"]]
return npm_scripts
class RunNpmScriptsCommand(sublime_plugin.WindowCommand):
""" Lists all npm projects & then lists all npm scripts. Selecting one of them executes the said npm script.
"""
def run(self, use_terminus=False):
"""Invoked when the command is run and shows a list of folders.
Args:
None
Returns:
None
"""
is_terminus = use_terminus
global is_terminus
npm_projects = [x for x in self.window.folders() if os.path.isfile(os.path.join(x, "package.json"))]
if len(npm_projects) == 0:
self.window.status_message("No folder in the side bar is an npm project")
return
elif len(npm_projects) == 1:
self.show_npm_scripts(npm_projects, 0)
return
self.window.show_quick_panel(npm_projects, lambda id: self.show_npm_scripts(npm_projects, id))
def show_npm_scripts(self, npm_projects, id):
"""Shows the npm scripts defined in the package.json of the chosen directory.
Args:
npm_projects (list) : List of all folders in side bar that are npm projects.
id (int) : The id of the selected folder.
Returns:
None
"""
chosen_directory = npm_projects[id]
if id >= 0:
npm_scripts = get_npm_scripts(chosen_directory)
if len(npm_scripts) == 0:
self.window.status_message("No npm scripts in package.json")
else:
self.window.show_quick_panel(npm_scripts, lambda id: self.run_build_system(npm_scripts, chosen_directory, id))
else:
self.window.status_message("No directory chosen")
def run_build_system(self, npm_scripts, chosen_directory, id):
"""Runs the exec command & passes it on some parameters for running the chosen npm script.
Args:
npm_scripts (list) : A list of npm scripts defined in the package.json of the chosen directory.
chosen_directory (str) : The absolute path of the selected directory
id (int) : The id of the npm script selected.
Returns:
None
"""
if id >= 0:
is_terminus_installed = "Terminus" in sublime.load_settings("Package Control.sublime-settings").get("installed_packages")
target = "terminus_exec" if (is_terminus and is_terminus_installed) else "exec"
self.window.run_command(target, {
"shell_cmd": "npm run {}".format(npm_scripts[id]),
"working_dir": chosen_directory
})
else:
self.window.status_message("No npm script selected")
@predragnikolic
Copy link

Just a question,
If I have a script serve which starts a server at localhost:3000, for example.
How will I be able to stop the server?

@UltraInstinct05
Copy link
Author

@predragnikolic Right now I am not sure if there is a way I can handle that from the plugin itself (I'll look into it). As a workaround, you can define a keybinding as follows :-

{
	"keys": ["ctrl+alt+6"],
	"command": "exec",
	"args": {
		"kill": true,
	},
}

which should kill the running server when the exec command is running. If you are using terminus however, this is not required as you can press ctrl + c in the terminus build panel and kill the server.

@predragnikolic
Copy link

Thanks :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment