From 53e596fa6708aa1c3a4359c5679a898cfbd406ec Mon Sep 17 00:00:00 2001 From: Mariusz Nowak Date: Thu, 14 Nov 2019 15:46:08 +0100 Subject: [PATCH] feat: `uninstall` command for installed binaries --- lib/Serverless.js | 2 +- lib/plugins/executable/index.js | 12 ++++++++++++ scripts/postinstall.js | 22 ++++++++++++++-------- 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/lib/Serverless.js b/lib/Serverless.js index 99f166250..4c18fbcf2 100644 --- a/lib/Serverless.js +++ b/lib/Serverless.js @@ -18,7 +18,7 @@ const ServerlessError = require('./classes/Error').ServerlessError; const Version = require('./../package.json').version; const isStandaloneExecutable = require('./utils/isStandaloneExecutable'); -const installationMaintananceCommands = new Set(['upgrade']); +const installationMaintananceCommands = new Set(['uninstall', 'upgrade']); class Serverless { constructor(config) { diff --git a/lib/plugins/executable/index.js b/lib/plugins/executable/index.js index cc080b622..5bbdb76d3 100644 --- a/lib/plugins/executable/index.js +++ b/lib/plugins/executable/index.js @@ -24,12 +24,20 @@ module.exports = class Executable { usage: 'Upgrade Serverless', lifecycleEvents: ['upgrade'], }, + uninstall: { + isHidden: !isStandaloneExecutable, + usage: 'Uninstall Serverless', + lifecycleEvents: ['uninstall'], + }, }; this.hooks = { 'upgrade:upgrade': () => { return isStandaloneExecutable ? this.upgrade() : this.rejectCommand('upgrade'); }, + 'uninstall:uninstall': () => { + return isStandaloneExecutable ? this.uninstall() : this.rejectCommand('uninstall'); + }, }; } @@ -89,6 +97,10 @@ module.exports = class Executable { }); } + uninstall() { + return fse.removeAsync(BINARIES_DIR_PATH).then(() => this.serverless.cli.log('Uninstalled')); + } + rejectCommand(command) { throw new this.serverless.classes.Error( `\`${command}\` command is supported only in context of a standalone exacutable instance ` + diff --git a/scripts/postinstall.js b/scripts/postinstall.js index 17f6ea7d9..3221304e0 100644 --- a/scripts/postinstall.js +++ b/scripts/postinstall.js @@ -8,15 +8,21 @@ const isPathDependent = const truthyStr = val => val && !['0', 'false', 'f', 'n', 'no'].includes(val.toLowerCase()); const { CI, ADBLOCK, SILENT } = process.env; if (!truthyStr(CI) && !truthyStr(ADBLOCK) && !truthyStr(SILENT)) { + const messageTokens = ['Serverless Framework successfully installed!']; + if (isPathDependent) { + messageTokens.push( + 'To start your first project, please open another terminal and run “serverless”.', + 'You can uninstall at anytime by running “serverless uninstall”.' + ); + } else { + messageTokens.push('To start your first project run “serverless”.'); + } process.stdout.write( - `${boxen( - chalk.yellow( - `Serverless Framework successfully installed!\nTo start your first project, ${ - isPathDependent ? 'please open another terminal and ' : '' - }run “serverless”.` - ), - { padding: 1, margin: 1, borderColor: 'yellow' } - )}\n` + `${boxen(chalk.yellow(messageTokens.join('\n\n')), { + padding: 1, + margin: 1, + borderColor: 'yellow', + })}\n` ); }