forked from albertlauncher/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
80 lines (66 loc) · 2.52 KB
/
Copy path__init__.py
File metadata and controls
80 lines (66 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# -*- coding: utf-8 -*-
"""Provides a way to install, remove and search for packages in the npmjs.com database. To trigger \
the extension you just need to type `npm ` in albert. If no search query is supplied you have the \
option to update all globally installed packages."""
from albertv0 import *
from shutil import which
import os
import json
import subprocess
__iid__ = "PythonInterface/v0.1"
__prettyname__ = "npm"
__version__ = "1.0"
__trigger__ = "npm "
__author__ = "Benedict Dudel"
__dependencies__ = ["npm"]
if which("npm") is None:
raise Exception("'npm' is not in $PATH.")
iconPath = iconLookup("npm")
if not iconPath:
iconPath = os.path.dirname(__file__)+"/logo.svg"
def handleQuery(query):
if query.isTriggered:
if not query.string.strip():
return Item(
id = __prettyname__,
icon = iconPath,
text = "Update",
subtext = "Update all globally installed packages",
actions = [
TermAction("", ["npm", "update", "--global"])
]
)
items = getSearchResults(query.string.strip())
if not items:
return Item(
id = __prettyname__,
icon = iconPath,
text = "Search on npmjs.com",
subtext = "No modules found in local database. Try to search on npmjs.com",
actions = [
UrlAction(
"Search on npmjs.com",
"https://www.npmjs.com/search?q=%s" % query.string.strip()
)
]
)
return items
def getSearchResults(query):
proc = subprocess.run(["npm", "search", "--json", query], stdout=subprocess.PIPE)
items = []
for module in json.loads(proc.stdout.decode()):
items.append(
Item(
id = __prettyname__,
icon = iconPath,
text = "%s (%s)" % (module["name"], module["version"]),
subtext = module.get("description", ""),
actions = [
UrlAction("Open module on npmjs.com", "https://www.npmjs.com/package/%s" % module["name"]),
TermAction("Install", ["npm", "install", "--global", module["name"]]),
TermAction("Update", ["npm", "update", "--global", module["name"]]),
TermAction("Remove", ["npm", "uninstall", "--global", module["name"]]),
]
)
)
return items