Completed
Pull Request — master (#2395)
by Mischa
01:50
created

NpmRequirement   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 42
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __init__() 0 17 1
A install_command() 0 10 1
A is_installed() 0 7 1
1
from coalib.bears.requirements.PackageRequirement import PackageRequirement
2
from coalib.misc.Shell import call_without_output
3
4
5
class NpmRequirement(PackageRequirement):
6
    """
7
    This class is a subclass of ``PackageRequirement``, and helps specifying
8
    requirements from ``npm``, without using the manager name.
9
    """
10
11
    def __init__(self, package, version=""):
12
        """
13
        Constructs a new ``NpmRequirement``, using the ``PackageRequirement``
14
        constructor.
15
16
        >>> pr = NpmRequirement('ramllint', '6.2')
17
        >>> pr.manager
18
        'npm'
19
        >>> pr.package
20
        'ramllint'
21
        >>> pr.version
22
        '6.2'
23
24
        :param package: A string with the name of the package to be installed.
25
        :param version: A version string. Leave empty to specify latest version.
26
        """
27
        PackageRequirement.__init__(self, 'npm', package, version)
28
29
    def install_command(self):
30
        """
31
        Creates the installation command for the instance of the class.
32
33
        >>> NpmRequirement('alex', '2').install_command()
34
        'npm install alex@2'
35
36
        :param return: A string with the installation command.
37
        """
38
        return "npm install {}@{}".format(self.package, self.version)
39
40
    def is_installed(self):
41
        """
42
        Checks if the dependency is installed.
43
44
        :param return: True if dependency is installed, false otherwise.
45
        """
46
        return not call_without_output(['npm', 'show', self.package])
47