UNPKG

zapier-platform-cli

Version:

The CLI for apps in the Zapier Developer Platform.

127 lines (108 loc) 5.67 kB
'use strict'; var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }(); /*eslint no-unused-vars: 0 */ var path = require('path'); var colors = require('colors'); var utils = require('../utils'); var defaultPort = 7545; // TODO: would be nice to have a /tmp/zapier-watch.pid var watch = function watch(context) { context.line('Watching and running your app locally. Zapier will tunnel JS calls here.\n'); var options = { log: context.line, port: context.argOpts.port || defaultPort }; var localAppId = void 0, localProxyUrl = void 0, localDefinition = void 0; var orgVersion = require(path.join(process.cwd(), 'package.json')).version; var resetHandler = function resetHandler() { return new Promise(function (resolve) { options.handler = utils.getLocalAppHandler({ reload: true, baseEvent: { logToStdout: true } }); resolve(); }); }; resetHandler(); var pingZapierForTunnel = function pingZapierForTunnel() { if (!localProxyUrl) { return Promise.resolve(); } var url = '/apps/' + localAppId + '/versions/' + orgVersion + '/tunnel'; return utils.callAPI(url, { method: 'PUT', body: { url: localProxyUrl, definition: localDefinition || {} } }); }; var reloadDefinition = function reloadDefinition() { return new Promise(function (resolve, reject) { options.handler({ command: 'definition' }, {}, function (err, resp) { if (err) { return reject(err); } var currentDefinition = resp.results; if (currentDefinition.version !== orgVersion) { context.line(colors.yellow(' Warning! Version changed from ' + orgVersion + ' to ' + currentDefinition.version + '! You need to restart watch to do that.')); } else { localDefinition = currentDefinition; } return resolve(currentDefinition); }); }); }; reloadDefinition(); utils.nodeWatch(process.cwd(), {}, function (filePath) { var fileName = filePath.replace(process.cwd() + path.sep, ''); if (fileName.startsWith('.git')) { return; } utils.printStarting('Reloading for ' + fileName); resetHandler().then(reloadDefinition).then(pingZapierForTunnel).then(function () { return utils.printDone(); }).catch(function (err) { utils.printDone(false); context.line(err); // don't print err.stack until we use require('syntax-error') or similar }); }); // TODO: check we've pushed the current versions. return utils.checkCredentials().then(function () { return utils.getLinkedApp(); }).then(function (app) { utils.printStarting('Starting local server on port ' + options.port); return Promise.all([app, utils.localAppTunnelServer(options)]); }).then(function (_ref) { var _ref2 = _slicedToArray(_ref, 2), app = _ref2[0], server = _ref2[1]; utils.printDone(); utils.printStarting('Starting local tunnel for port ' + options.port); return Promise.all([app, server, utils.makeTunnelUrl(options.port)]); }).then(function (_ref3) { var _ref4 = _slicedToArray(_ref3, 3), app = _ref4[0], server = _ref4[1], _proxyUrl = _ref4[2]; utils.printDone(); localAppId = app.id; localProxyUrl = _proxyUrl; context.line(); context.line('Running! Make changes local and you should see them reflect almost instantly in the Zapier editor.'); context.line(); // We must ping Zapier with the new deets a minimum of every 15 seconds. return utils.promiseForever(pingZapierForTunnel, 15000); }); }; watch.hide = true; watch.argsSpec = []; watch.argOptsSpec = { port: { help: 'what port should we host/listen for tunneling', default: defaultPort } }; watch.help = 'Watch the current directory and send changes live to Zapier.'; watch.example = 'zapier watch'; watch.docs = '\nThis command watches the current directory and, on changes, does two things:\n\n* Sends any new changes to Zapier, instantly updating the UI in your Zapier editor.\n* Tunnels all Javascript calls through your local environment with logs to stdout.\n\nThis makes for a great development experience, letting you make and observe changes much faster than a `zapier push`\n\n> Note: this is only temporary and has no effect on other users at Zapier! You\'ll want to do `zapier push` to make your changes permanent and universal.\n\n**Arguments**\n\n' + utils.argsFragment(watch.argsSpec) + '\n' + utils.argOptsFragment(watch.argOptsSpec) + '\n\n' + '```' + 'bash\n$ zapier watch --port=9090\n# Watching and running your app locally. Zapier will tunnel JS calls here.\n#\n# Starting local server on port 9090 - done!\n# Starting local tunnel for port 9090 - done!\n#\n# Running! Make changes local and you should see them reflect almost instantly in the Zapier editor.\n#\n# Reloading for index.js - done!\n# Reloading for resources/form.js - done!\n# Reloading for index.js - done!\n' + '```' + '\n'; module.exports = watch;