zapier-platform-cli
Version:
The CLI for apps in the Zapier Developer Platform.
160 lines (133 loc) • 10.3 kB
JavaScript
;
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"); } }; }();
var colors = require('colors/safe');
var _ = require('lodash');
var utils = require('../utils');
var authenticationPaths = ['authentication.test', 'authentication.oauth2Config.getAccessToken', 'authentication.oauth2Config.refreshAccessToken', 'authentication.sessionConfig.perform'];
// {type:triggers}.{key:lead}.operation.perform
var actionTemplates = ['<%= type %>.<%= key %>.operation.perform', '<%= type %>.<%= key %>.operation.performSubscribe', '<%= type %>.<%= key %>.operation.performUnsubscribe', '<%= type %>.<%= key %>.operation.inputFields', '<%= type %>.<%= key %>.operation.outputFields'].map(function (template) {
return _.template(template);
});
var hydrateTemplate = _.template('hydrators.<%= key %>');
var inlineResourceMethods = ['get', 'hook', 'list', 'search', 'create', 'searchOrCreate'];
// resources.{key:lead}.get.operation.perform
var makeResourceTemplates = function makeResourceTemplates(methods) {
return methods.reduce(function (acc, method) {
return acc.concat(['resources.<%= key %>.' + method + '.operation.perform', 'resources.<%= key %>.' + method + '.operation.performSubscribe', 'resources.<%= key %>.' + method + '.operation.performUnsubscribe', 'resources.<%= key %>.' + method + '.operation.inputFields', 'resources.<%= key %>.' + method + '.operation.outputFields']);
}, []).map(function (template) {
return _.template(template);
});
};
var allResourceTemplates = makeResourceTemplates(inlineResourceMethods);
var typeMap = {
triggers: ['list', 'hook'],
searches: ['search'],
creates: ['create']
};
var describe = function describe(context) {
return Promise.resolve().then(function () {
return Promise.all([utils.getLinkedApp().catch(function () {
return null;
}), utils.getLinkedAppConfig().catch(function () {
return null;
}), utils.getVersionInfo().catch(function () {
return null;
}), utils.localAppCommand({ command: 'definition' })]);
}).then(function (_ref) {
var _ref2 = _slicedToArray(_ref, 4),
app = _ref2[0],
appConfig = _ref2[1],
version = _ref2[2],
definition = _ref2[3];
context.line('A description of your app listed below.\n');
if (app) {
context.line(colors.bold('Title') + '\n');
context.line(app.title);
context.line();
if (app.description) {
context.line(colors.bold('Description') + '\n');
context.line(app.description);
context.line();
}
}
context.line(colors.bold('Authentication') + '\n');
var authRows = [];
if (definition.authentication) {
var authentication = _.assign({}, definition.authentication);
authentication.paths = authenticationPaths.filter(function (path) {
return _.has(definition, path);
}).join('\n');
if (authentication.type === 'oauth2') {
if (appConfig && version) {
authentication.redirect_uri = version.oauth_redirect_uri;
} else {
authentication.redirect_uri = colors.grey('do zapier push to see redirect_uri!');
}
}
authRows = [authentication];
}
var authHeaders = [['Type', 'type'], ['Redirect URI', 'redirect_uri', colors.grey('n/a')], ['Available Methods', 'paths', colors.grey('n/a')]];
var authIfEmpty = colors.grey('Nothing found for authentication.');
utils.printData(authRows, authHeaders, authIfEmpty);
context.line();
context.line(colors.bold('Hydrators') + '\n');
var hydratorRows = _.map(definition.hydrators, function (val, key) {
return {
key: key,
paths: hydrateTemplate({ key: key })
};
});
var hydratorHeaders = [['Key', 'key'], ['Method', 'paths', colors.grey('n/a')]];
var hydratorIfEmpty = colors.grey('Nothing found for hydrators.');
utils.printData(hydratorRows, hydratorHeaders, hydratorIfEmpty);
context.line();
var resourceRows = _.values(definition.resources || {}).map(function (resource) {
resource = _.assign({}, resource);
resource.paths = allResourceTemplates.map(function (method) {
return method({ key: resource.key });
}).filter(function (path) {
return _.has(definition, path);
}).join('\n');
return resource;
});
context.line(colors.bold('Resources') + '\n');
var resourceHeaders = [['Noun', 'noun'], ['Ref', 'key'], ['Available Methods', 'paths', colors.grey('n/a')]];
var resourceIfEmpty = colors.grey('Nothing found for resources, maybe try the `zapier scaffold` command?');
utils.printData(resourceRows, resourceHeaders, resourceIfEmpty);
context.line();
Object.keys(typeMap).forEach(function (type) {
context.line(colors.bold(_.capitalize(type)) + '\n');
var rows = _.values(definition[type]).map(function (row) {
row = _.assign({}, row);
row.paths = [];
// add possible action paths
row.paths = row.paths.concat(actionTemplates.map(function (method) {
return method({ type: type, key: row.key });
}));
// add possible resource paths
if (row.operation.resource) {
var key = row.operation.resource.split('.')[0];
var resourceTemplates = makeResourceTemplates(typeMap[type]);
row.paths = row.paths.concat(resourceTemplates.map(function (method) {
return method({ key: key });
}));
}
row.paths = row.paths.filter(function (path) {
return _.has(definition, path);
}).join('\n');
return row;
});
var headers = [['Noun', 'noun'], ['Label', 'display.label'], ['Resource Ref', 'operation.resource', colors.grey('n/a')], ['Available Methods', 'paths', colors.grey('n/a')]];
var ifEmpty = colors.grey('Nothing found for ' + type + ', maybe try the `zapier scaffold` command?');
utils.printData(rows, headers, ifEmpty);
context.line();
});
context.line('If you\'d like to add more, try the `zapier scaffold` command to kickstart!');
});
};
describe.argsSpec = [];
describe.argOptsSpec = {};
describe.help = 'Describes the current app.';
describe.example = 'zapier describe';
describe.docs = '\nPrints a human readable enumeration of your app\'s triggers, searches, and actions as seen by Zapier. Useful to understand how your resources convert and relate to different actions.\n\n> These are the same actions we\'d display in our editor!\n\n* `Noun` -- your action\'s noun\n* `Label` -- your action\'s label\n* `Resource` -- the resource (if any) this action is tied to\n* `Available Methods` -- testable methods for this action\n\n**Arguments**\n\n' + utils.argsFragment(describe.argsSpec) + '\n' + utils.argOptsFragment(describe.argOptsSpec) + '\n' + utils.defaultArgOptsFragment() + '\n\n' + '```' + 'bash\n$ zapier describe\n# A description of your app "Example" listed below.\n#\n# Triggers\n#\n# \u250C\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\u252C\u2500\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\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\n# \u2502 Noun \u2502 Label \u2502 Resource Ref \u2502 Available Methods \u2502\n# \u251C\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\u253C\u2500\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\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524\n# \u2502 Member \u2502 Updated Subscriber \u2502 member \u2502 triggers.updated_member.operation.perform \u2502\n# \u2502 \u2502 \u2502 \u2502 triggers.updated_member.operation.inputFields \u2502\n# \u2502 \u2502 \u2502 \u2502 resources.member.list.operation.perform \u2502\n# \u2502 \u2502 \u2502 \u2502 resources.member.list.operation.inputFields \u2502\n# \u2514\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\u2534\u2500\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\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\n#\n# Searches\n#\n# Nothing found for searches, maybe try the `zapier scaffold` command?\n#\n# Creates\n#\n# Nothing found for creates, maybe try the `zapier scaffold` command?\n#\n# If you\'d like to add more, try the `zapier scaffold` command to kickstart!\n' + '```' + '\n';
module.exports = describe;