zapier-platform-cli
Version:
The CLI for apps in the Zapier Developer Platform.
56 lines (47 loc) • 3.97 kB
JavaScript
;
var fs = require('fs');
var utils = require('../utils');
var env = function env(context, version, key, value) {
var isRemove = global.argOpts.remove;
if (value !== undefined || isRemove) {
key = key.toUpperCase();
return utils.checkCredentials().then(function () {
return utils.getLinkedApp();
}).then(function (app) {
var url = '/apps/' + app.id + '/versions/' + version + '/environment/' + key;
var verb = isRemove ? 'remove' : 'set';
context.line('Preparing to ' + verb + ' environment ' + key + ' for your ' + version + ' "' + app.title + '".\n');
var startMsg = isRemove ? 'Deleting ' + key : 'Setting ' + key + ' to "' + value + '"';
utils.printStarting(startMsg);
var method = isRemove ? 'DELETE' : 'PUT';
return utils.callAPI(url, {
method: method,
body: value
});
}).then(function () {
utils.printDone();
context.line();
context.line('Environment updated! Try viewing it with `zapier env ' + version + '`.');
// touch index.js to force watch to pick up env changes
fs.utimesSync(utils.entryPoint(), NaN, NaN);
return;
});
}
if (key) {
context.line('Try viewing your env with `zapier env` or setting with `' + env.example + '`.');
return Promise.resolve();
}
return utils.listEnv(version).then(function (data) {
context.line('The env of your "' + data.app.title + '" listed below.\n');
utils.printData(data.environment, [['Version', 'app_version'], ['Key', 'key'], ['Value', 'value']]);
context.line('\nTry setting an env with the `' + env.example + '` command.');
});
};
env.argsSpec = [{ name: 'version', example: '1.0.0', required: true, help: 'the app version\'s environment to work on' }, { name: 'key', example: 'CLIENT_SECRET', help: 'the uppercase key of the environment variable to set' }, { name: 'value', example: '12345', help: 'the raw value to set to the key' }];
env.argOptsSpec = {
remove: { flag: true, help: 'optionally remove environment variable with this key' }
};
env.help = 'Read and write environment variables.';
env.example = 'zapier env 1.0.0 CLIENT_SECRET 12345';
env.docs = '\nManage the environment of your app so that `process.env` has the necessary variables, making it easy to match a local environment with a production environment via `CLIENT_SECRET=12345 zapier test`.\n\n**Arguments**\n\n' + utils.argsFragment(env.argsSpec) + '\n' + utils.argOptsFragment(env.argOptsSpec) + '\n' + utils.defaultArgOptsFragment() + '\n\n' + '```' + 'bash\n$ zapier env 1.0.0\n# The env of your "Example" listed below.\n#\n# \u250C\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\u252C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\n# \u2502 Version \u2502 Key \u2502 Value \u2502\n# \u251C\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\u253C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524\n# \u2502 1.0.0 \u2502 CLIENT_SECRET \u2502 12345 \u2502\n# \u2514\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\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\n#\n# Try setting an env with the `zapier env 1.0.0 CLIENT_SECRET 12345` command.\n\n$ zapier env 1.0.0 CLIENT_SECRET 12345\n# Preparing to set environment CLIENT_SECRET for your 1.0.0 "Example".\n#\n# Setting CLIENT_SECRET to "12345" - done!\n#\n# Environment updated! Try viewing it with `zapier env 1.0.0`.\n\n$ zapier env 1.0.0 CLIENT_SECRET --remove\n# Preparing to remove environment CLIENT_SECRET for your 1.0.0 "Example".\n#\n# Deleting CLIENT_SECRET - done!\n#\n# Environment updated! Try viewing it with `zapier env 1.0.0`.\n' + '```' + '\n';
module.exports = env;