-
Notifications
You must be signed in to change notification settings - Fork 0
/
NodeManager.py
126 lines (89 loc) · 3.76 KB
/
NodeManager.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# -*- coding:ISO-8859-1 -*-
import subprocess
import re
'''
This file contains the implementations of the functions that are
responsable to change the node version.
'''
# 0.11.16 -> 2015-01-14
# 1.8.2 -> 2015-05-04
# 2.5.0 -> 2015-08-04
# 3.3.1 -> 2015-09-08
# 4.2.2 -> 2015-10-29
# 5.11.1 -> 2016-04-26
# 6.9.2 -> 2016-10-25
# 7.10.1 -> 2017-05-30
# 8.9.0 -> 2017-10-31
# 9.11.2 -> 2018-04-24
# 10.12.0 -> 2018-10-10
# 12.18.3 -> 2020-04-21
nodeDates = ['2015-01-14', '2015-05-04', '2015-08-04', '2015-09-08', '2015-10-29',
'2016-04-26', '2016-10-25', '2017-05-30', '2017-10-31', '2018-04-24', '2018-10-10', '2020-04-21']
nodeVersions = {'2015-01-14':'0.11.16', '2015-05-04':'1.8.2', '2015-08-04':'2.5.0', '2015-09-08':'3.3.1', '2015-10-29':'4.2.2',
'2016-04-26':'5.11.1', '2016-10-25':'6.9.2', '2017-05-30':'7.10.1', '2017-10-31':'8.9.0', '2018-04-24':'9.11.2', '2018-10-10': '10.12.0', '2020-04-21': '12.18.3'}
# get the latest version based in current version
def getVersionOnVersion(version):
versions = list(nodeVersions.values()) # get all versions
versions.sort() # sort the version
for i, lVersion in enumerate(versions): # in each version
if lVersion > version: # if latest version is minor than version
return versions[i] # return the previous version
return versions[-1] # return the last version
# check if version is installed
def isInstalled(version):
if subprocess.getstatusoutput('bash nvm.sh version {0}'.format(version))[1].__eq__('N/A'):
return False
else:
return True
# install in local machine the specify version of node
def installVersion(version):
subprocess.getstatusoutput('bash nvm.sh install {0}'.format(version))
# format the output
def printLine(num, version):
if num < 10:
line = str(num) + ' - '
else:
line = str(num) + ' - '
line += version
i = 8 - len(version)
line += (' ' * i)
line += ': '
print(line, end='', flush=True)
# install all required versions of node js
def installAllVersions():
print('\nInstall all - 11 - required versions of NodeJs')
for i, date in enumerate(nodeDates):
version = nodeVersions[date]
printLine(i+1, version)
if not isInstalled(version):
installVersion(version)
print("OK")
print('')
# based in date, get the latest version
# of Node before this date
def getVersionOnDate(date):
for dateNode in nodeDates:
if date < dateNode: # if date release is menor than date node
return nodeVersions[dateNode] # get the node version in this date
return nodeVersions[nodeDates[-1]] # latest version
# return the version if there is in package
# if the developer put the version in 'engines'->'node', get this
def getVersionOnPackage(package):
engines = package.get('engines') # get the map engines, if exists, or raise KeyError
version = engines['node'] # get the version of node
version = re.search('[\d]+', version).group(0)
version = getVersionOnVersion(version) # get last version of this version
return version
# return the version of NodeJs if there is in package.json based in date
# and install the current version of node if version isnt installed
def getVersion(package, date):
# first, try to get the version on the package.json
# after, get based in the date of release
version = ' '
try:
version = getVersionOnPackage(package)
if not isInstalled(version):
installVersion(version) # try install
except (KeyError, IndexError, AttributeError, TypeError):
version = getVersionOnDate(date)
return version