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 pathutils.js
More file actions
384 lines (364 loc) · 10.5 KB
/
utils.js
File metadata and controls
384 lines (364 loc) · 10.5 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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*!
* contentstack-cli
* copyright (c) Contentstack
* MIT Licensed
*/
'use strict';
/**
* Module Dependencies.
*/
var _ = require('lodash');
var prompt = require('prompt');
var debug = require('debug')('util');
var pkg = require('../package');
var request = require('./request');
var limit = 100;
var config, api;
/**
* Utility/helper methods
* @param {Boolean} skip : Flag to load config/api details
*/
function Utility(skip) {
if (!skip) {
config = require('./config').config();
api = config.get('contentstack');
if (typeof api.cdn === 'string' && typeof api.host === 'string') {
debug(`Querying content from origin server: ${api.host}.`);
debug('Add \'contentstack.cdn\' in express config to fetch data from Contentstack\'s delivery networks!');
}
// setting the headers
this.headers = {
api_key: api.api_key,
access_token: api.access_token,
'X-User-Agent': 'contentstack-cli/' + pkg.version
};
}
}
/**
* Iterative function that fetches entries from Contentstack
* @param {Object} req : Request object
* @param {Object} bucket : Entries collection, queried so far
* @param {Function} fn : Callback function
* @return {Object} : Return entries collection
*/
Utility.prototype.request = function(req, bucket, key, fn) {
var self = this;
return request(req, function(error, body) {
if (error) {
return fn(error);
} else if (typeof body.count === 'undefined') {
// if there are no objects
return fn(null, bucket);
}
bucket = bucket.concat(body[key]);
req.qs.skip += limit;
if (req.qs.skip > body.count) {
return fn(null, bucket);
}
return self.request(req, bucket, key, fn);
});
};
/**
* Get entries from servers
* @param {String} content_type : Content type who's entries are to be fetched
* @param {String} locale : Locale from where the entries are to be fetched
* @param {Object} _query : Query filter
* @param {Object} fields : Fields expected in result
* @param {String} environment : Environment name
* @param {Function} fn : Callback function
* @return {Object} : Return collection of entries, filtered on 'fields'
*/
Utility.prototype.getEntries = function(content_type, locale, query, fields, environment, fn) {
var uri = `${api.cdn || api.host}/${api.version}${api.urls.content_types}${content_type}${api.urls.entries}?locale=${locale}`;
if (typeof environment === 'string') {
uri += `&environment=${environment}`;
}
for (var i = 0; i < fields.length; i++) {
uri += `&only[BASE]=${fields[i]}`;
}
var req = {
uri: uri,
method: 'GET',
headers: this.headers,
qs: {
skip: 0,
limit: limit,
desc: 'created_at',
include_count: true
},
json: true
};
if (typeof query === 'object' && Object.keys(query).length) {
req.qs.query = query;
}
return this.request(req, [], 'entries', fn);
};
/**
* Get all content types present in the stack
* @param {Function} fn : Callback fn
* @return {Object} : Collection of content types fetched in the stack
*/
Utility.prototype.getContentTypes = function(fn) {
var uri = `${api.cdn || api.host}/${api.version}${api.urls.content_types}?`;
var requiredKeys = ['title', 'uid'];
requiredKeys.forEach(function (key) {
uri += `only[BASE]=${key}&`;
});
var query = {
uri: uri,
method: 'GET',
headers: this.headers,
qs: {
skip: 0,
limit: limit || 100,
include_count: true
},
json: true
};
return this.request(query, [], 'content_types', fn);
};
/**
* Get all content types present in the stack
* @param {Function} fn : Callback fn
* @return {Object} : Collection of content types fetched in the stack
*/
Utility.prototype.getContentTypesExperimental = function(fn) {
var uri = `${api.cdn || api.host}/${api.version}${api.urls.content_types}`;
var query = {
uri: uri,
method: 'GET',
headers: this.headers,
qs: {
skip: 0,
limit: limit || 100,
include_count: true
},
json: true
};
return this.request(query, [], 'content_types', fn);
};
/**
* API to login into Contentstack
* @param {Object} user : User details such as email/pass
* @param {Function} fn : Error first callback fn
* @return {Object} : Stack login details
*/
Utility.prototype.login = function(user, fn) {
return request({
uri: `${api.host}/${api.version}${api.urls.session}`,
headers: this.headers,
method: 'POST',
json: user
}, fn);
};
/**
* Get asset json from selected stack
* @param {Object} req : Asset request object
* @param {Object} assets : Asset json, queried
* @param {Function} fn : Error first callback function
* @return {Object} : Asset collection
*/
Utility.prototype.getAssets = function(req, assets, fn) {
var self = this;
return request(req, function(error, body) {
if (error) {
return fn(error);
}
req.qs.skip += 100;
assets = assets.concat(body.assets);
if (typeof body.count === 'undefined' || req.qs.skip > body.count) {
return fn(null, assets);
}
return self.getAssets(req, assets, fn);
});
};
/**
* Get environment details
* @param {String} envName : Environment name
* @param {Function} fn : Error first callback
* @return {Object} : Environment details
*/
Utility.prototype.getEnvironment = function(envName, fn) {
if (typeof envName !== 'string' || typeof fn !== 'function') {
throw new Error(`Invalid params for 'getEnvironment'!\n${JSON.stringify(arguments)}`);
}
return request({
url: `${api.host}/${api.version}${api.urls.environments}${envName}`,
headers: this.headers,
method: 'GET',
json: true
}, fn);
};
/**
* Fetch stack details from Contentstack
* @param {String|Undefined} headers : Header details
* @param {Function} fn : Error first callback
* @return {Function} : Error first callback
*/
Utility.prototype.getStack = function(headers, fn) {
return request({
url: `${api.host}/${api.version}${api.urls.stacks}`,
headers: headers || this.headers,
method: 'GET',
qs: {
include_discrete_variables: true
},
json: true
}, fn);
};
/**
* Fetch environment details from Contentstack
* @param {String|Undefined} headers : Header details
* @param {Object} query : Query filter
* @param {Function} fn : Error first callback
* @return {Function} : Error first callback
*/
Utility.prototype.getEnvironments = function(headers, query) {
var fn;
if (arguments[0] === 'function') {
fn = arguments[0];
} else if (arguments[1] === 'function') {
fn = arguments[1];
} else {
fn = arguments[2];
}
return request({
url: `${api.host}/${api.version}${api.urls.environments}`,
qs: query || {},
headers: headers || this.headers,
method: 'GET',
json: true
}, fn);
};
/**
* Prompt input skeleton
* @type {Object}
*/
Utility.prototype.inputs = {
type: function() {
return {
type: 'string',
name: 'type',
required: false,
description: 'Input types that you would like to sync: content_types|assets|all\nPress ENTER to select \'all\' types:',
conform: function(value) {
return (value == 'content_types' || value == 'assets' || value == 'all');
},
before: function(value) {
return (value) ? value.toLowerCase() : value;
}
};
},
content_types: function(opts) {
return {
type: 'string',
name: (opts && opts.name) ? opts.name : 'content_types',
required: false,
description: (opts && opts.description) ? opts.description : 'Enter the content types that you would like to process\nPress ENTER to select to select \'all\' content types:',
ask: function() {
return (prompt && prompt.history('type').value !== 'assets');
},
before: function(input) {
if (input) {
return input.split(',');
}
return [];
}
};
},
environment: function() {
return {
type: 'string',
name: 'environment',
required: true,
description: 'Enter the environment name you would like to sync:',
before: function(input) {
return input.toLowerCase();
}
};
},
language: function(locale) {
var masterLocale = (locale) ? locale : 'en-us';
return {
type: 'string',
name: 'language',
default: masterLocale,
required: true,
description: 'Enter the locale that you would like to sync:',
before: function(input) {
return input.trim().toLowerCase();
},
conform: function(input) {
var languages = config.get('languages'),
idx = _.findIndex(languages, {
'code': input.trim()
});
return (idx > -1);
}
};
},
custom: function(opts) {
opts = opts || {};
var _temp = {
type: (!opts.type) ? 'string' : opts.type,
format: opts.format || 'string',
name: opts.name || '',
required: opts.required || false,
default: opts.default || '',
description: opts.description || '',
message: opts.message || '',
before: opts.before || function(input) {
return input;
},
conform: opts.conform || function() {
return true;
}
};
return _temp;
}
};
/**
* Get environment from client
* @param {Object} option : Options to select from
* @return {Object} : Client's choice
*/
Utility.prototype.inputEnvironment = function(option) {
var self = this;
prompt.message = '';
prompt.delimiter = '';
prompt.start();
return function(callback) {
prompt.get([option], function(error, result) {
if (error) {
return callback(error);
}
return self.getEnvironment(result[option.name], callback);
});
};
};
/**
* Get custom input from the user
* @param {Object} option : Options to choose from
* @return {Object} : Client's choice
*/
Utility.prototype.inputCustom = function(option) {
prompt.message = '';
prompt.delimiter = '';
prompt.start();
return function(callback) {
prompt.get([option], function(err, result) {
if (err) throw err;
callback(null, result);
});
};
};
/**
* Match client's input
* @param {String} confirm : Client input
* @return {Boolean} : Return true, if match confirmed
*/
Utility.prototype.matchConfirm = function(confirm) {
var regExp = new RegExp('(yes|y)', 'i');
return (confirm) ? regExp.test(confirm) : undefined;
};
module.exports = Utility;