"""
Node.js
=======

This module provides tools for installing `Node.js`_ and managing
packages using `npm`_.

.. _Node.js: http://nodejs.org/
.. _npm: http://npmjs.org/

"""
from fabric.api import run, sudo, cd
from fabtools import require


DEFAULT_VERSION = '0.8.11'


def install_nodejs(version=DEFAULT_VERSION):
    """
    Install Node JS from source.

    ::

        import fabtools

        # Install Node.js
        fabtools.nodejs.install_nodejs()

    .. note:: This function may not work for old versions of Node.js.

    """
    require.deb.packages([
        'build-essential',
        'python',
        'libssl-dev',
    ])

    filename = 'node-v%s.tar.gz' % version
    foldername = filename[0:-7]

    run('wget http://nodejs.org/dist/v%(version)s/%(filename)s' % locals())
    run('tar -xzf %s' % filename)
    with cd(foldername):
        run('./configure')
        run('make')
        sudo('make install')
    run('rm -rf %(filename)s %(foldername)s' % locals())


def install(package=None, version=None, local=False):
    """
    Install Node.js package using ``npm``.

    If *local* is ``True``, the package will be installed locally.

    ::

        import fabtools

        # Install package globally
        fabtools.nodejs.install('express')

        # Install package locally
        fabtools.nodejs.install('underscore', local=False)

    If no package name is given, then ``npm install`` will be run,
    which will locally install all packages specified in the
    ``package.json`` file in the current directory.
    """
    if package:
        if version:
            package += '@%s' % version

        if local:
            run('npm install -l %s' % package)
        else:
            sudo('HOME=/root npm install -g %s' % package)
    else:
        run('npm install')


def update(package, local=False):
    """
    Update Node.js package.

    If *local* is ``True``, the package will be updated locally.
    """
    if local:
        run('npm update -l %s' % package)
    else:
        sudo('HOME=/root npm update -g %s' % package)


def uninstall(package, version=None, local=False):
    """
    Uninstall Node.js package.

    If *local* is ``True``, the package will be uninstalled locally.

    ::

        import fabtools

        # Uninstall package globally
        fabtools.nodejs.uninstall('express')

        # Uninstall package locally
        fabtools.nodejs.uninstall('underscore', local=False)

    """
    if version:
        package += '@%s' % version

    if local:
        run('npm uninstall -l %s' % package)
    else:
        sudo('HOME=/root npm uninstall -g %s' % package)

@@ -277,22 +277,27 @@
 install_
-nodejs
+from_source
 (version
@@ -992,24 +992,32 @@
 %0Adef install
+_package
 (package=Non
@@ -1063,24 +1063,26 @@
     Install 
+a 
 Node.js pack
@@ -1088,22 +1088,8 @@
 kage
- using %60%60npm%60%60
 .%0A%0A 
@@ -1250,24 +1250,32 @@
 dejs.install
+_package
 ('express')%0A
@@ -1336,24 +1336,32 @@
 dejs.install
+_package
 ('underscore
@@ -1828,16 +1828,24 @@
 f update
+_package
 (package
@@ -1879,16 +1879,18 @@
  Update 
+a 
 Node.js 
@@ -2107,16 +2107,24 @@
 ninstall
+_package
 (package
@@ -2171,24 +2171,26 @@
   Uninstall 
+a 
 Node.js pack
@@ -2368,16 +2368,24 @@
 ninstall
+_package
 ('expres
@@ -2458,16 +2458,24 @@
 ninstall
+_package
 ('unders

Rename functions in Node.js module