5
5
This module provides tools for installing `Node.js`_ and managing
6
6
packages using `npm`_.
7
7
8
+ .. note: the ``simplejson`` module is required on Python 2.5
9
+
8
10
.. _Node.js: http://nodejs.org/
9
11
.. _npm: http://npmjs.org/
10
12
11
13
"""
12
- from fabric .api import run , sudo , cd
13
- from fabtools import require
14
+ from __future__ import with_statement
15
+
16
+ try :
17
+ import json
18
+ except ImportError :
19
+ import simplejson as json
20
+
21
+ from fabric .api import run , sudo , cd , settings , hide
14
22
15
23
16
24
DEFAULT_VERSION = '0.8.11'
@@ -30,6 +38,7 @@ def install_from_source(version=DEFAULT_VERSION):
30
38
.. note:: This function may not work for old versions of Node.js.
31
39
32
40
"""
41
+ from fabtools import require
33
42
require .deb .packages ([
34
43
'build-essential' ,
35
44
'python' ,
@@ -48,6 +57,20 @@ def install_from_source(version=DEFAULT_VERSION):
48
57
run ('rm -rf %(filename)s %(foldername)s' % locals ())
49
58
50
59
60
+ def version ():
61
+ """
62
+ Get the version of Node.js currently installed.
63
+
64
+ Returns ``None`` if it is not installed.
65
+ """
66
+ with settings (hide ('running' , 'stdout' ), warn_only = True ):
67
+ res = run ('/usr/local/bin/node --version' )
68
+ if res .failed :
69
+ return None
70
+ else :
71
+ return res [1 :]
72
+
73
+
51
74
def install_package (package , version = None , local = False ):
52
75
"""
53
76
Install a Node.js package.
@@ -94,6 +117,31 @@ def install_dependencies():
94
117
run ('npm install' )
95
118
96
119
120
+ def package_version (package , local = False ):
121
+ """
122
+ Get the installed version of a Node.js package.
123
+
124
+ Returns ``None``is the package is not installed. If *local* is
125
+ ``True``, returns the version of the locally installed package.
126
+ """
127
+ options = ['--json true' , '--silent' ]
128
+ if local :
129
+ options .append ('-l' )
130
+ else :
131
+ options .append ('-g' )
132
+ options = ' ' .join (options )
133
+
134
+ with hide ('running' , 'stdout' ):
135
+ res = run ('npm list %s' % options )
136
+
137
+ dependencies = json .loads (res )['dependencies' ]
138
+ pkg_data = dependencies .get (package )
139
+ if pkg_data :
140
+ return pkg_data ['version' ]
141
+ else :
142
+ return None
143
+
144
+
97
145
def update_package (package , local = False ):
98
146
"""
99
147
Update a Node.js package.
0 commit comments