diff --git a/packages/cli/src/commands/index.js b/packages/cli/src/commands/index.js index 9d17d66d2..d0189f594 100644 --- a/packages/cli/src/commands/index.js +++ b/packages/cli/src/commands/index.js @@ -11,7 +11,6 @@ module.exports = { history: require('./history'), invite: require('./invite'), link: require('./link'), - logout: require('./logout'), logs: require('./logs'), migrate: require('./migrate'), promote: require('./promote'), diff --git a/packages/cli/src/commands/logout.js b/packages/cli/src/commands/logout.js deleted file mode 100644 index 80a04cc32..000000000 --- a/packages/cli/src/commands/logout.js +++ /dev/null @@ -1,52 +0,0 @@ -const constants = require('../constants'); -const utils = require('../utils'); - -const logout = async context => { - context.line( - 'Preparing to deactivate personal deploy keys and reset local configs.' - ); - context.line(); - utils.startSpinner('Deactivating personal deploy keys'); - try { - await utils.callAPI('/keys', { - method: 'DELETE' - }); - } catch (e) { - // no worries - } - - utils.endSpinner(); - utils.startSpinner(`Destroying \`${constants.AUTH_LOCATION_RAW}\``); - await utils.deleteFile(constants.AUTH_LOCATION); - - utils.endSpinner(); - context.line(); - context.line( - 'All personal deploy keys deactivated - now try `zapier login` to login again.' - ); -}; -logout.argsSpec = []; -logout.argOptsSpec = {}; -logout.help = `Deactivates all your personal deploy keys and resets \`${ - constants.AUTH_LOCATION_RAW -}\`.`; -logout.example = 'zapier logout'; -logout.docs = ` -Deactivates all your personal deploy keys and resets your local config. Does not delete any apps or versions. - -> This will delete the \`${ - constants.AUTH_LOCATION_RAW -}\` (home directory identifies the deploy key & user). - -${'```'}bash -$ zapier logout -Preparing to deactivate personal deploy keys and reset local configs. - - Deactivating personal deploy keys - done! - Destroying \`~/.zapierrc\` - done! - -All personal keys deactivated - now try \`zapier login\` to login again. -${'```'} -`; - -module.exports = logout; diff --git a/packages/cli/src/oclif/README.md b/packages/cli/src/oclif/README.md index 59ad8fa96..3b6126de6 100644 --- a/packages/cli/src/oclif/README.md +++ b/packages/cli/src/oclif/README.md @@ -26,8 +26,8 @@ Some notes to help an ongoing project - [ ] history - [ ] invitees - [ ] link +- [x] logout - [x] login -- [ ] logout - [ ] logs - [ ] migrate - [ ] migrate diff --git a/packages/cli/src/oclif/commands/logout.js b/packages/cli/src/oclif/commands/logout.js new file mode 100644 index 000000000..78cd52676 --- /dev/null +++ b/packages/cli/src/oclif/commands/logout.js @@ -0,0 +1,39 @@ +const BaseCommand = require('../ZapierBaseCommand'); +const { buildFlags } = require('../buildFlags'); + +const { callAPI } = require('../../utils/api'); +const { deleteFile } = require('../../utils/files'); +const { AUTH_LOCATION, AUTH_LOCATION_RAW } = require('../../constants'); + +class LogoutCommand extends BaseCommand { + async perform() { + let success = true; + this.log( + 'Preparing to deactivate local deploy key and reset local configs.' + ); + this.startSpinner('Deactivating local deploy key'); + try { + await callAPI('/keys', { method: 'DELETE', body: { single: true } }); + } catch (e) { + success = false; + this.error( + `Deletion API request failed. Is your ${AUTH_LOCATION} already empty or invalid? If so, feel free to ignore this error.` + ); + } finally { + this.stopSpinner({ success }); + } + + this.startSpinner(`Destroying \`${AUTH_LOCATION}\``); + const deletedFileResult = deleteFile(AUTH_LOCATION); + this.debug(`file deletion success?: ${deletedFileResult}`); + this.stopSpinner(); + + this.log('The active deploy key was deactivated'); + } +} + +LogoutCommand.flags = buildFlags(); +LogoutCommand.examples = ['zapier logout']; +LogoutCommand.description = `Deactivates your acive deploy key and resets \`${AUTH_LOCATION_RAW}\`.`; + +module.exports = LogoutCommand; diff --git a/packages/cli/src/oclif/oCommands.js b/packages/cli/src/oclif/oCommands.js index adadea516..1f9233f44 100644 --- a/packages/cli/src/oclif/oCommands.js +++ b/packages/cli/src/oclif/oCommands.js @@ -2,6 +2,7 @@ module.exports = { init: require('./commands/init'), + logout: require('./commands/logout'), login: require('./commands/login'), versions: require('./commands/versions') }; diff --git a/packages/cli/src/utils/api.js b/packages/cli/src/utils/api.js index a8a86a9dd..667792774 100644 --- a/packages/cli/src/utils/api.js +++ b/packages/cli/src/utils/api.js @@ -95,10 +95,15 @@ const callAPI = (route, options, rawError = false) => { debug(`>> ${requestOptions.method} ${requestOptions.url}`); if (requestOptions.body) { const replacementStr = 'raw zip removed in logs'; - const cleanedBody = _.assign({}, JSON.parse(requestOptions.body), { - zip_file: replacementStr, - source_zip_file: replacementStr - }); + const requestBody = JSON.parse(requestOptions.body); + const cleanedBody = {}; + for (const k in requestBody) { + if (k.includes('zip_file')) { + cleanedBody[k] = replacementStr; + } else { + cleanedBody[k] = requestBody[k]; + } + } debug(`>> ${JSON.stringify(cleanedBody)}`); } debug(`<< ${res.status}`); diff --git a/packages/cli/src/utils/files.js b/packages/cli/src/utils/files.js index d1b154353..c7345c974 100644 --- a/packages/cli/src/utils/files.js +++ b/packages/cli/src/utils/files.js @@ -45,16 +45,14 @@ const writeFile = (fileName, data) => { return fse.writeFile(fixHome(fileName), data); }; -// Returns a promise that deletes the file. -const deleteFile = fileName => { - return new Promise(resolve => { - try { - fse.unlinkSync(fileName); - } catch (err) { - resolve(); - } - resolve(); - }); +// deletes a file, eats the error +const deleteFile = path => { + try { + fse.unlinkSync(path); + return true; + } catch (err) { + return false; + } }; // Returns a promise that ensures a directory exists.