Last active
May 6, 2020 08:57
-
-
Save UltraInstinct05/e5df44c6eb6b942aac0d659cfc0efdeb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
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?
@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.
Thanks :-)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have updated the plugin to now show only the npm scripts in a panel if there is only one project in the sidebar that contains a
package.json
so now there is no need of an intermediate panel that shows only one project in a panel.Please feel free to report any issues.