UNPKG

zapier-platform-cli

Version:

The CLI for apps in the Zapier Developer Platform.

64 lines (54 loc) 4.72 kB
'use strict'; var constants = require('../constants'); var utils = require('../utils'); var hasCancelled = function hasCancelled(answer) { return answer.toLowerCase() === 'no' || answer.toLowerCase() === 'cancel'; }; var pickApp = function pickApp(context, apps, appMap) { if (!apps.length) { throw new Error('You don\'t seem to have any CLI apps. Make sure you\'re invited to one, or create one first.'); } utils.printData(apps, [['#', 'number'], ['Title', 'title'], ['Unique Key', 'key'], ['Timestamp', 'date'], ['Linked', 'linked']]); var action = function action() { return utils.getInput('Which app number do you want to link? (Ctrl-C to cancel)\n\n'); }; var stop = function stop(answer) { if (!hasCancelled(answer) && !appMap[answer]) { throw new Error('That app number does not match any CLI app that you have access to.'); } return appMap[answer] || hasCancelled(answer); }; return utils.promiseDoWhile(action, stop); }; var link = function link(context) { var appMap = {}; return utils.listApps().then(function (data) { var apps = data.apps.map(function (app, index, arr) { app.number = arr.length - index; appMap[app.number] = app; return app; }); return pickApp(context, apps, appMap); }).then(function (answer) { context.line(); if (hasCancelled(answer)) { throw new Error('Cancelled link operation.'); } else { utils.printStarting('Selecting existing app "' + appMap[answer].title + '"'); return appMap[answer]; } }).then(function (app) { utils.printDone(); utils.printStarting('Setting up `' + constants.CURRENT_APP_FILE + '` file'); return utils.writeLinkedAppConfig(app); }).then(function () { utils.printDone(); context.line('\nFinished! You can `zapier push` now to build & upload a version!'); }); }; link.argsSpec = []; link.argOptsSpec = {}; link.help = 'Link the current directory to an app you have access to.'; link.example = 'zapier link'; link.docs = '\nLink the current directory to an app you have access to. It is fairly uncommon to run this command - more often you\'d just `git clone git@github.com:example-inc/example.git` which would have a `' + constants.CURRENT_APP_FILE + '` file already included. If not, you\'d need to be an admin on the app and use this command to regenerate the `' + constants.CURRENT_APP_FILE + '` file.\n\nOr, if you are making an app from scratch - you should prefer `zapier init`.\n\n> This will change the `./' + constants.CURRENT_APP_FILE + '` (which identifies the app assosciated with the current directory).\n\n**Arguments**\n\n' + utils.argsFragment(link.argsSpec) + '\n' + utils.argOptsFragment(link.argOptsSpec) + '\n' + utils.defaultArgOptsFragment() + '\n\n' + '```' + 'bash\n$ zapier link\n# Which app would you like to link the current directory to?\n#\n# \u250C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\n# \u2502 Number \u2502 Title \u2502 Unique Key \u2502 Timestamp \u2502 Linked \u2502\n# \u251C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524\n# \u2502 1 \u2502 Example \u2502 Example \u2502 2016-01-01T22:19:28 \u2502 \u2714 \u2502\n# \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\n# ...or type any title to create new app!\n#\n# Which app number do you want to link? You also may type a new app title to create one. (Ctrl-C to cancel)\n#\n 1\n#\n# Selecting existing app "Example" - done!\n# Setting up `' + constants.CURRENT_APP_FILE + '` file - done!\n#\n# Finished! You can `zapier push` now to build & upload a version!\n' + '```' + '\n'; module.exports = link;