-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
79 lines (69 loc) · 3.43 KB
/
main.py
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
import logging
import subprocess
from ulauncher.api.client.Extension import Extension
from ulauncher.api.client.EventListener import EventListener
from ulauncher.api.shared.event import KeywordQueryEvent, ItemEnterEvent
from ulauncher.api.shared.item.ExtensionResultItem import ExtensionResultItem
from ulauncher.api.shared.action.RenderResultListAction import RenderResultListAction
from ulauncher.api.shared.action.ExtensionCustomAction import ExtensionCustomAction
from ulauncher.api.shared.action.CopyToClipboardAction import CopyToClipboardAction
logger = logging.getLogger(__name__)
class NodeExtension(Extension):
def __init__(self):
super(NodeExtension, self).__init__()
self.subscribe(KeywordQueryEvent, KeywordQueryEventListener())
try:
self.nodePath = subprocess.check_output(['which', 'node'], universal_newlines=True, stderr=subprocess.STDOUT).strip()
except subprocess.CalledProcessError:
try:
command = """
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" && nvm which current
"""
self.nodePath = subprocess.check_output(command, universal_newlines=True, shell=True, text=True, executable="/bin/bash", stderr=subprocess.STDOUT).strip()
except subprocess.CalledProcessError:
try:
tryNodeVersion = subprocess.check_output(['/snap/bin/node', '-v'], universal_newlines=True, stderr=subprocess.STDOUT).strip()
self.nodePath = '/snap/bin/node'
except (FileNotFoundError, subprocess.CalledProcessError):
try:
tryNodeVersion = subprocess.check_output(['/usr/local/bin/node', '-v'], universal_newlines=True, stderr=subprocess.STDOUT).strip()
self.nodePath = '/usr/local/bin/node'
except (FileNotFoundError, subprocess.CalledProcessError):
self.nodePath = None
self.nodePathErrorMessage = '\'node\' command not found. please read \'Install Nodejs\' section. https://github.com/luasenvy/ulauncher-node'
logger.error(self.nodePathErrorMessage)
class KeywordQueryEventListener(EventListener):
def on_event(self, event, extension):
items = []
expression = event.get_argument()
try:
if None == extension.nodePath:
items.append(ExtensionResultItem(icon='images/icon.png',
name=extension.nodePathErrorMessage,
description='\'node\' command not found.',
on_enter=CopyToClipboardAction(extension.nodePathErrorMessage)
))
return RenderResultListAction(items)
if None == expression:
items.append(ExtensionResultItem(icon='images/icon.png',
name='Node: there is no code.',
description='please input script code.',
on_enter=None
))
return RenderResultListAction(items)
result = subprocess.check_output([extension.nodePath, '-p', expression], universal_newlines=True, stderr=subprocess.STDOUT).strip()
items.append(ExtensionResultItem(icon='images/icon.png',
name=result,
description='Press \'enter\' to copy to clipboard.',
on_enter=CopyToClipboardAction(result)
))
except subprocess.CalledProcessError as errorMessage:
items.append(ExtensionResultItem(icon='images/icon.png',
name='Node: %s' % errorMessage.output,
description='Expression has Error.',
on_enter=CopyToClipboardAction(errorMessage.output)
))
return RenderResultListAction(items)
if __name__ == '__main__':
NodeExtension().run()