Skip to content

Commit 04dc738

Browse files
ma-zalvmarchaud
authored andcommitted
Fix Unitech#2809. Fixed the broken installation of modules specified as Github repository with non-master branch. Fixed the broken upgrade of modules specified by Github source (old version was not been removed, therefore two instances existed in memory after upgrade). (Unitech#2840)
1 parent fae1adf commit 04dc738

File tree

2 files changed

+44
-22
lines changed

2 files changed

+44
-22
lines changed

lib/API/Modules/Modularizer.js

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -140,21 +140,13 @@ function installModule(CLI, module_name, cb) {
140140

141141
Common.printOut(cst.PREFIX_MSG_MOD + 'Module downloaded');
142142

143-
if (module_name.match(/\.tgz($|\?)/)) {
144-
module_name = Utility.packageNameToModuleName(module_name);
145-
}
146-
else if (module_name.indexOf('/') != -1)
147-
module_name = module_name.split('/')[1];
148-
149-
//pm2 install [email protected]
150-
if(module_name.indexOf('@') != -1)
151-
module_name = module_name.split('@')[0]
143+
var canonic_module_name = Utility.getCanonicModuleName(module_name);
152144

153-
proc_path = p.join(cst.PM2_ROOT_PATH, 'node_modules', module_name);
145+
proc_path = p.join(cst.PM2_ROOT_PATH, 'node_modules', canonic_module_name);
154146

155147
cmd = p.join(proc_path, cst.DEFAULT_MODULE_JSON);
156148

157-
Configuration.set(MODULE_CONF_PREFIX + ':' + module_name, 'true', function(err, data) {
149+
Configuration.set(MODULE_CONF_PREFIX + ':' + canonic_module_name, 'true', function(err, data) {
158150
startModule(CLI, {
159151
cmd : cmd,
160152
development_mode : development_mode,
@@ -300,7 +292,7 @@ Modularizer.launchAll = function(CLI, cb) {
300292
Modularizer.install = function(CLI, module_name, cb) {
301293
Common.printOut(cst.PREFIX_MSG_MOD + 'Installing module ' + module_name);
302294

303-
var canonic_module_name = Utility.packageNameToModuleName(module_name);
295+
var canonic_module_name = Utility.getCanonicModuleName(module_name);
304296

305297
if (module_name == 'v8-profiler' || module_name == 'profiler') {
306298
installLangModule('v8-profiler', function(e) {
@@ -375,7 +367,8 @@ Modularizer.uninstall = function(CLI, module_name, cb) {
375367
return false;
376368
}
377369

378-
uninstallModule(CLI, module_name, cb);
370+
var canonic_module_name = Utility.getCanonicModuleName(module_name);
371+
uninstallModule(CLI, canonic_module_name, cb);
379372
};
380373

381374
/**

lib/Utility.js

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -153,19 +153,48 @@ var Utility = module.exports = {
153153

154154
async.waterfall(flows, callback);
155155
},
156+
156157
/**
157-
* Returns the module name from a .tgz package name (or the original name if it is not a valid pkg).
158-
* @param {string} package_name The package name (e.g. "foo.tgz", "foo-1.0.0.tgz", "folder/foo.tgz")
159-
* @return {string} the name
158+
* Function parse the module name and returns it as canonic:
159+
* - Makes the name based on installation filename.
160+
* - Removes the Github author, module version and git branch from original name.
161+
*
162+
* @param {string} module_name
163+
* @returns {string} Canonic module name (without trimed parts).
164+
* @example Always returns 'pm2-slack' for inputs 'ma-zal/pm2-slack', 'ma-zal/pm2-slack#own-branch',
165+
* 'pm2-slack-1.0.0.tgz' or '[email protected]'.
160166
*/
161-
packageNameToModuleName: function(package_name) {
162-
if (package_name.match(/^(.+\/)?([^\/]+)\.tgz($|\?)/)) {
163-
package_name = package_name.match(/^(.+\/)?([^\/]+)\.tgz($|\?)/)[2];
164-
if (package_name.match(/^(.+)-[0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z0-9_]+\.[0-9]+)?$/)) {
165-
package_name = package_name.match(/^(.+)-[0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z0-9_]+\.[0-9]+)?$/)[1];
167+
getCanonicModuleName: function(module_name) {
168+
var canonic_module_name = module_name;
169+
170+
// Returns the module name from a .tgz package name (or the original name if it is not a valid pkg).
171+
// Input: The package name (e.g. "foo.tgz", "foo-1.0.0.tgz", "folder/foo.tgz")
172+
// Output: The module name
173+
if (canonic_module_name.match(/\.tgz($|\?)/)) {
174+
if (canonic_module_name.match(/^(.+\/)?([^\/]+)\.tgz($|\?)/)) {
175+
canonic_module_name = canonic_module_name.match(/^(.+\/)?([^\/]+)\.tgz($|\?)/)[2];
176+
if (canonic_module_name.match(/^(.+)-[0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z0-9_]+\.[0-9]+)?$/)) {
177+
canonic_module_name = canonic_module_name.match(/^(.+)-[0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z0-9_]+\.[0-9]+)?$/)[1];
178+
}
166179
}
167180
}
168-
return package_name;
181+
182+
//pm2 install username/module
183+
else if(canonic_module_name.indexOf('/') !== -1) {
184+
canonic_module_name = canonic_module_name.split('/')[1];
185+
}
186+
187+
//pm2 install [email protected]
188+
if(canonic_module_name.indexOf('@') !== -1) {
189+
canonic_module_name = canonic_module_name.split('@')[0];
190+
}
191+
192+
//pm2 install module#some-branch
193+
if(canonic_module_name.indexOf('#') !== -1) {
194+
canonic_module_name = canonic_module_name.split('#')[0];
195+
}
196+
197+
return canonic_module_name;
169198
}
170199

171200
};

0 commit comments

Comments
 (0)