From d1514139f6c10d57ef7f600ef3e107e73becd811 Mon Sep 17 00:00:00 2001 From: Unitech Date: Sat, 29 Aug 2015 19:52:58 +0200 Subject: [PATCH] module:cmd prefixes - add pm2 module:generate to generate a sample module --- CHANGELOG.md | 6 ++--- bin/pm2 | 14 ++++++----- lib/CLI.js | 11 +++++++++ lib/Modularizer.js | 61 +++++++++++++++++++++++++++++++++++++++------- 4 files changed, 74 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 471d2d84..fceecc4c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,13 +2,13 @@ # 1.0.0-beta - beta pmx // rm -- pm2 module:update +- New command: pm2 module:update -> Update a module +- New command: pm2 module:publish -> Publish module in current folder + Git push +- New command: pm2 module:generate -> Generate a sample module - alias pm2 install with pm2 i -- pm2 publish publish module on NPM + commit on Git # 0.14.7 -- - New flag `--no-pmx` : starts an app without injecting pmx - New feature : cron restart now works in fork mode as well - Disabled auto-gc on interactor diff --git a/bin/pm2 b/bin/pm2 index 1354c855..3d12675e 100755 --- a/bin/pm2 +++ b/bin/pm2 @@ -342,26 +342,28 @@ commander.command('update') * Module specifics */ commander.command('install ') - .alias('i') + .alias('module:install') .description('install or update a module and run it forever') .action(function(plugin_name) { CLI.install(plugin_name); }); -commander.command('module:update ') - .description('install or update a module and run it forever') - .action(function(plugin_name) { - CLI.install(plugin_name); +commander.command('module:generate') + .description('Generate a sample module in current folder') + .action(function() { + CLI.generateModuleSample(); }); commander.command('uninstall ') - .alias('ui') + .alias('module:uninstall') .description('stop and uninstall a module') .action(function(plugin_name) { CLI.uninstall(plugin_name); }); + commander.command('publish') + .alias('module:publish') .description('Publish the module you are currently on') .action(CLI.publish); diff --git a/lib/CLI.js b/lib/CLI.js index 369fd4bd..6b2a4209 100644 --- a/lib/CLI.js +++ b/lib/CLI.js @@ -1846,6 +1846,17 @@ CLI.publish = function(module_name, cb) { }); }; +/** + * Publish module on NPM + Git push + */ +CLI.generateModuleSample = function(cb) { + Modularizer.generateSample(function(err, data) { + if (err) + return cb ? cb(err) : exitCli(cst.ERROR_EXIT); + return cb ? cb(null, data) : exitCli(cst.SUCCESS_EXIT); + }); +}; + CLI.killAllModules = function(cb) { Common.getAllModulesId(function(err, modules_id) { async.forEachLimit(modules_id, 1, function(id, next) { diff --git a/lib/Modularizer.js b/lib/Modularizer.js index aa1367f3..04a390ac 100644 --- a/lib/Modularizer.js +++ b/lib/Modularizer.js @@ -1,11 +1,12 @@ var Modularizer = module.exports = {}; -var shelljs = require('shelljs'); -var path = require('path'); -var fs = require('fs'); -var async = require('async'); -var p = path; +var shelljs = require('shelljs'); +var path = require('path'); +var fs = require('fs'); +var async = require('async'); +var p = path; +var readline = require('readline'); var Configuration = require('./Configuration.js'); var cst = require('../constants.js'); @@ -23,7 +24,7 @@ var UX = require('./CliUx.js'); // [X] Block all tentatives to stop a module // [X] NO NEED pm2 generate module = Provide a module skeleton // [X] pm2 update=install = not deleting the conf file -// pm2 publish module (increment version, git push, npm publish) +// [X] pm2 publish module (increment version, git push, npm publish) // [X] API normalization = dont block adoption, find common way to transform current software into propack function startModule(opts, cb) { @@ -278,9 +279,6 @@ Modularizer.uninstall = function(module_name, cb) { * Publish a module */ Modularizer.publish = function(cb) { - var readline = require('readline'); - - var rl = readline.createInterface({ input: process.stdin, output: process.stdout @@ -327,6 +325,51 @@ Modularizer.publish = function(cb) { }); }; +Modularizer.generateSample = function(cb) { + var rl = readline.createInterface({ + input: process.stdin, + output: process.stdout + }); + + rl.question(cst.PREFIX_MSG_MOD + "Module name: ", function(module_name) { + + var cmd1 = 'git clone https://github.com/pm2-hive/sample-module.git ' + module_name + '; cd ' + module_name + '; rm -rf .git'; + var cmd2 = 'cd ' + module_name + ' ; sed -i "s:sample-module:'+ module_name +':g" package.json'; + var cmd3 = 'cd ' + module_name + ' ; npm install'; + + Common.printOut(cst.PREFIX_MSG_MOD + 'Getting sample app'); + shelljs.exec(cmd1, function(err) { + if (err) Common.printError(cst.PREFIX_MSG_MOD_ERR + err.message); + shelljs.exec(cmd2, function(err) { + console.log(''); + shelljs.exec(cmd3, function(err) { + console.log(''); + Common.printOut(cst.PREFIX_MSG_MOD + 'Module sample created in folder: ', path.join(process.cwd(), module_name)); + console.log(''); + Common.printOut('Start module in development mode:'); + Common.printOut('$ cd ' + module_name + '/'); + Common.printOut('$ pm2 install . '); + console.log(''); + + Common.printOut('Probe values: '); + Common.printOut('$ pm2 iprobe'); + console.log(''); + Common.printOut('Module Log: '); + Common.printOut('$ pm2 logs ' + module_name); + console.log(''); + Common.printOut('Uninstall module: '); + Common.printOut('$ pm2 uninstall ' + module_name); + console.log(''); + Common.printOut('Force restart: '); + Common.printOut('$ pm2 restart ' + module_name); + return cb ? cb() : false; + }); + }); + }); + + }); + +}; function isValidModule(conf) { var valid = true;