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