|
| 1 | +#!/usr/bin/env node |
| 2 | +/*! |
| 3 | + * contentstack-cli |
| 4 | + * copyright (c) Built.io Contentstack |
| 5 | + * MIT Licensed |
| 6 | + */ |
| 7 | + |
| 8 | +'use strict'; |
| 9 | + |
| 10 | +/*! |
| 11 | + * Module dependencies |
| 12 | + */ |
| 13 | +var domain = require('domain'), |
| 14 | + program = require('commander'), |
| 15 | + path = require('path'), |
| 16 | + helper = require('./../lib/helper'); |
| 17 | + |
| 18 | +/* |
| 19 | +* Application defined variables |
| 20 | +* */ |
| 21 | +var messages = [ |
| 22 | + '\t \x1b[33m**' |
| 23 | + , ' \x1b[33m***' |
| 24 | + , ' \x1b[33m****' |
| 25 | + , '\x1b[33m***** \x1b[31m** \x1b[36m.----------------------------------.' |
| 26 | + , ' \x1b[33m**** \x1b[31m*** \x1b[36m| Built.io Contentstack! |' |
| 27 | + , ' \x1b[33m*** \x1b[31m**** \x1b[36m\'----------------------------------\'' |
| 28 | + , ' \x1b[33m** \x1b[31m***** ' |
| 29 | + , ' \x1b[31m****' |
| 30 | + , ' \x1b[31m***' |
| 31 | + , ' \x1b[31m**' |
| 32 | + ].join("\n\t"), |
| 33 | + pkg = require('./../package.json'); |
| 34 | + |
| 35 | +function list(val) { |
| 36 | + return ((val && val.length) ? val.split(',').map(String) : undefined); |
| 37 | +} |
| 38 | + |
| 39 | +function getDefaultOptions (opts, events) { |
| 40 | + var opt = {}; |
| 41 | + for(var key in events) { |
| 42 | + opt[key] = updateDefaultOption(opts, key); |
| 43 | + } |
| 44 | + return opt; |
| 45 | +} |
| 46 | + |
| 47 | +function updateDefaultOption (opts, key) { |
| 48 | + if(~['content_types', 'skip_content_types'].indexOf(key) && opts[key] === true) { |
| 49 | + opts[key] = []; |
| 50 | + } else if('backup' === key && opts[key] === true) { |
| 51 | + opts[key] = 'yes'; |
| 52 | + } else if('type' === key && opts[key] === true) { |
| 53 | + opts[key] = 'all'; |
| 54 | + } else if('language' === key && opts[key] === true) { |
| 55 | + opts[key] = 'en-us'; |
| 56 | + } else if('datetime' === key && opts[key] === true) { |
| 57 | + opts[key] = new Date('1970').toISOString(); |
| 58 | + } |
| 59 | + return opts[key]; |
| 60 | +} |
| 61 | + |
| 62 | +function optionConversion(options) { |
| 63 | + var _options = { |
| 64 | + environment: options.env, |
| 65 | + language: options.lang |
| 66 | + }; |
| 67 | + options.language = options._events.language = _options.language; |
| 68 | + options.environment = options._events.environment = _options.environment; |
| 69 | + delete options.env; |
| 70 | + delete options.lang; |
| 71 | + delete options._events.env; |
| 72 | + delete options._events.lang; |
| 73 | + return _options; |
| 74 | +} |
| 75 | +// printing the Built.io Contentstack Animation |
| 76 | +console.log('\n'+messages+'\x1b[0m\n'); |
| 77 | + |
| 78 | +program |
| 79 | + .version(pkg.version || "0.1.x"); |
| 80 | + |
| 81 | +program |
| 82 | + .command('connect [directory] [api_key] [access_token] [template]') |
| 83 | + .option('-d, --dir <directory>', 'Enter the name of "Directory"') |
| 84 | + .option('-a, --api_key <api_key>', 'Enter the stack "API KEY" to connect') |
| 85 | + .option('-c, --token <access_token>', 'Enter the "Access Token" relative to "API KEY"') |
| 86 | + .option('-t, --template [template]', 'Enter the template', 'basic') |
| 87 | + .description('Connect to an existing stack in Built.io Contentstack.') |
| 88 | + .action(function(directory, api_key, access_token, template, options) { |
| 89 | + setImmediate(function () { |
| 90 | + //creating the domain to execute in safe mode |
| 91 | + var context = domain.create(); |
| 92 | + |
| 93 | + // error handling in domain |
| 94 | + context.on('error', errorHandler); |
| 95 | + |
| 96 | + // running the connect in domain |
| 97 | + context.run(function() { |
| 98 | + var connect = require('./../lib/connect'), |
| 99 | + args = { |
| 100 | + directory: directory || options.dir, |
| 101 | + api_key: api_key || options.api_key, |
| 102 | + access_token: access_token || options.token, |
| 103 | + template: "basic" || options.template |
| 104 | + }; |
| 105 | + new connect(args); |
| 106 | + }); |
| 107 | + }); |
| 108 | + }); |
| 109 | + |
| 110 | +program |
| 111 | + .command('plugin create') |
| 112 | + .alias('plugin create') |
| 113 | + .description('Create a new plugin in the current application') |
| 114 | + .action(function (path, directory) { |
| 115 | + setImmediate(function() { |
| 116 | + var context = domain.create(); |
| 117 | + |
| 118 | + // error handling in domain |
| 119 | + context.on('error', errorHandler); |
| 120 | + |
| 121 | + // running the plugin in domain |
| 122 | + context.run(function() { |
| 123 | + var plugin = require('./../lib/plugin'); |
| 124 | + new plugin(directory); |
| 125 | + }); |
| 126 | + }); |
| 127 | + }); |
| 128 | + |
| 129 | +program |
| 130 | + .command('sync') |
| 131 | + .option('-e, --env <environment>', 'Enter the environment of which the content needs to be synchronized', undefined) |
| 132 | + .option('-l, --lang [language]', 'Enter the language of which the content needs to be synchronized', undefined) |
| 133 | + .option('-c, --content_types [content_types]', 'Enter the content types to be included in synchronization (comma(",") seperated)', list, undefined) |
| 134 | + .option('-s, --skip_content_types [skip_content_types]', 'Enter the content types to be excluded from synchronization (comma(",") seperated)', list, undefined) |
| 135 | + .option('-d, --datetime [datetime]', 'Enter start date in ISO String format. Content published after this date will be synchronized (skip for all content)', undefined) |
| 136 | + .option('-b, --backup [backup]', 'Enter backup option', /(yes|no|y|n)/i, undefined) |
| 137 | + .description('Synchronize the previously published entries in the current application') |
| 138 | + .action(function (options) { |
| 139 | + setImmediate(function() { |
| 140 | + var context = domain.create(); |
| 141 | + |
| 142 | + // error handling in domain |
| 143 | + context.on('error', errorHandler); |
| 144 | + |
| 145 | + // running the synchronization in domain |
| 146 | + context.run(function() { |
| 147 | + var _options = optionConversion(options); |
| 148 | + console.error(_options); |
| 149 | + _options = helper.merge(getDefaultOptions(options, options._events), _options); |
| 150 | + console.error(_options); |
| 151 | + var sync = require('./../lib/sync'); |
| 152 | + new sync(_options); |
| 153 | + }); |
| 154 | + }); |
| 155 | + }); |
| 156 | + |
| 157 | +/* |
| 158 | + * command for bulk publish |
| 159 | + * */ |
| 160 | + |
| 161 | +program |
| 162 | + .command('publish') |
| 163 | + .alias('bulk-publish') |
| 164 | + .option('-u, --username <username>', 'Email id registered on Built.io Contentstack', undefined) |
| 165 | + .option('-p, --password <password>', 'Password', undefined) |
| 166 | + .option('-e, --env <environment>', 'Environment/s where you want to publish (comma(",") seperated)', list, undefined) |
| 167 | + .option('-b, --backup [backup]', 'Enter backup option', /(yes|no|y|n)/i, undefined) |
| 168 | + .option('-t, --type [type]', 'Enter a type of content to include in publishing [content_types/assets/all]', /(content_types|assets|all)/, undefined) |
| 169 | + .option('-c, --content_types [content_types]', 'Enter the content types to be included (comma(",") seperated)', list, undefined) |
| 170 | + .option('-s, --skip_content_types [skip_content_types]', 'Enter the content types to be excluded (comma(",") seperated)', list, undefined) |
| 171 | + .option('-l, --lang [language]', 'Enter the language where content should be published', undefined) |
| 172 | + .description('Publish content-types/assets/both on specified environment/s') |
| 173 | + .action(function (options) { |
| 174 | + setImmediate(function() { |
| 175 | + var context = domain.create(); |
| 176 | + |
| 177 | + // error handling in domain |
| 178 | + context.on('error', errorHandler); |
| 179 | + // running the synchronization in domain |
| 180 | + context.run(function() { |
| 181 | + var _options = optionConversion(options); |
| 182 | + _options = helper.merge(getDefaultOptions(options, options._events), _options); |
| 183 | + var publish = require('./../lib/publish'); |
| 184 | + new publish('publish', _options); |
| 185 | + }); |
| 186 | + }); |
| 187 | + }); |
| 188 | + |
| 189 | +/* |
| 190 | + * command for bulk unpublish |
| 191 | + * */ |
| 192 | + |
| 193 | +program |
| 194 | + .command('unpublish') |
| 195 | + .alias('bulk-unpublish') |
| 196 | + .option('-u, --username <username>', 'Email id registered on Built.io Contentstack', undefined) |
| 197 | + .option('-p, --password <password>', 'Password', undefined) |
| 198 | + .option('-e, --env <environment>', 'Environment/s where you want to unpublish (comma(",") seperated)', list, undefined) |
| 199 | + .option('-b, --backup [backup]', 'Enter backup option', /(yes|no|y|n)/i, undefined) |
| 200 | + .option('-t, --type [type]', 'Enter a type of content to include in unpublishing [content_types/assets/all]', /(content_types|assets|all)/, undefined) |
| 201 | + .option('-c, --content_types [content_types]', 'Enter the content types to be included (comma(",") seperated)', list, undefined) |
| 202 | + .option('-s, --skip_content_types [skip_content_types]', 'Enter the content types to be excluded (comma(",") seperated)', list, undefined) |
| 203 | + .option('-l, --lang [language]', 'Enter the language where content should be unpublished', undefined) |
| 204 | + .description('Unpublish content-types/assets/both on specified environment/s') |
| 205 | + .action(function (options) { |
| 206 | + setImmediate(function() { |
| 207 | + var context = domain.create(); |
| 208 | + |
| 209 | + // error handling in domain |
| 210 | + context.on('error', errorHandler); |
| 211 | + |
| 212 | + // running the synchronization in domain |
| 213 | + context.run(function() { |
| 214 | + var _options = optionConversion(options); |
| 215 | + _options = helper.merge(getDefaultOptions(options, options._events), _options); |
| 216 | + var unpublish = require('./../lib/publish'); |
| 217 | + new unpublish('unpublish', _options); |
| 218 | + }); |
| 219 | + }); |
| 220 | + }); |
| 221 | + |
| 222 | +// parse the input arguments |
| 223 | +program.parse(process.argv); |
| 224 | + |
| 225 | +// show help by default if no args |
| 226 | +if (program.args.length == 0) { |
| 227 | + var message = [ |
| 228 | + 'Built.io Contentstack Command Line Interface '+pkg.version |
| 229 | + , '\nUsage: contentstack [command]' |
| 230 | + , '\nCommands:' |
| 231 | + , ' connect Connect to an existing stack in Built.io Contentstack' |
| 232 | + , ' sync Synchronize the previously published entries in the current application' |
| 233 | + , ' publish Publish content-types/assets/both on specified environment' |
| 234 | + , ' unpublish Unpublish content-types/assets/both on specified environment' |
| 235 | + , ' plugin create Create the new plugin in the current application' |
| 236 | + , '\nOptions:' |
| 237 | + , ' -h, --help output usage information' |
| 238 | + , ' -V, --version output the version number' |
| 239 | + , '\nDocumentation can be found at http://contentstackdocs.built.io/' |
| 240 | + ].join('\n'); |
| 241 | + console.log(message); |
| 242 | + process.exit(1); |
| 243 | +} |
| 244 | +/* |
| 245 | +* Error Handler to handle the domain level error |
| 246 | +* */ |
| 247 | +function errorHandler(err) { |
| 248 | + console.error(err.message); |
| 249 | +} |
| 250 | + |
| 251 | +process.on('uncaughtException', function(err) { |
| 252 | + console.error('Caught exception: ', err.message); |
| 253 | + process.exit(0); |
| 254 | +}); |
0 commit comments