Skip to content

Commit ac5ba89

Browse files
committed
cli ratelimit and code maintainence push 4
1 parent 9d29493 commit ac5ba89

File tree

9 files changed

+724
-778
lines changed

9 files changed

+724
-778
lines changed

bin/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ function optionConversion(options) {
7373
return _options;
7474
}
7575
// printing the Contentstack Animation
76-
console.log('\n'+messages+'\x1b[0m\n');
76+
console.log('\n' + messages + '\x1b[0m\n');
7777
console.log('\x1b[31m Note: This version of Contentstack cli can be used only for v3 stacks. Use cli v1.x, for v2 stacks.\x1b[0m\n');
7878

7979
program

lib/config/default.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ module.exports = exports = {
3434
lastModified: true,
3535
maxAge: 0,
3636
redirect: true,
37-
setHeaders: function(res, path, stat) {
37+
setHeaders: function(res) {
3838
res.set('x-timestamp', Date.now());
3939
}
4040
}
4141
},
42-
'static': {
42+
static: {
4343
url: '/static',
4444
path: 'public',
4545
options: {
@@ -51,7 +51,7 @@ module.exports = exports = {
5151
lastModified: true,
5252
maxAge: 0,
5353
redirect: true,
54-
setHeaders: function(res, path, stat) {
54+
setHeaders: function(res) {
5555
res.set('x-timestamp', Date.now());
5656
}
5757
}
@@ -81,4 +81,4 @@ module.exports = exports = {
8181
form: 'form'
8282
}
8383
}
84-
}
84+
};

lib/connect.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ var createApplicationAt = function() {
222222
log(chalk.magenta('Next steps:'));
223223
for (var i = 0, _i = nextSteps.length; i < _i; i++) {
224224
log(chalk.magenta(`${i + 1}. ${nextSteps[i]['info']}`));
225-
log(chalk.magenta(` \$ ${nextSteps[i]['cmd']}`));
225+
log(chalk.magenta(` $ ${nextSteps[i]['cmd']}`));
226226
}
227227
});
228228
}, 1000);

lib/helper.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ exports.confirm = function(config, lang, backup, callback) {
7474
required: true,
7575
conform: function(value) {
7676
value = value.toLowerCase();
77-
return (value == 'yes' || value == 'no');
77+
return (value === 'yes' || value === 'no');
7878
},
7979
before: function(value) {
8080
return value.toLowerCase();
@@ -83,18 +83,18 @@ exports.confirm = function(config, lang, backup, callback) {
8383
if (error) {
8484
throw error;
8585
}
86-
var ok = (result.confirm == 'yes') ? true : false;
86+
var ok = (result.confirm === 'yes') ? true : false;
8787
process.stdin.destroy();
8888
if (ok) {
89-
return helper.createBackupDir(storagePath, lang,
90-
callback);
89+
return helper.createBackupDir(storagePath, lang, callback);
9190
}
9291
return callback(null);
9392
});
9493
} else if (backup === false) {
9594
return callback(null);
95+
} else {
96+
return helper.createBackupDir(storagePath, lang, callback);
9697
}
97-
return helper.createBackupDir(storagePath, lang, callback);
9898
}
9999
});
100100
} else {

lib/plugin.js

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,9 @@ var chalk = require('chalk');
1818
var log = console.log;
1919
var success = chalk.green;
2020
var error = chalk.red;
21-
var warning = chalk.yellow;
2221
var info = chalk.cyan;
2322

2423
var helper = require('./helper');
25-
// var utility = require('./utils');
26-
// var utility = new utility();
2724

2825
/**
2926
* Create contentstack-express framework plugin
@@ -74,20 +71,15 @@ var Plugin = function(name) {
7471
});
7572
})(_path, name);
7673
} else {
77-
helper.abort(
78-
`This command should be run from your application folder, where 'plugins' folder exists.`
79-
);
74+
helper.abort('This command should be run from your application folder, where \'plugins\' folder exists.');
8075
}
8176
} else {
82-
helper.abort(
83-
`Plugin name is not valid. Only 'alphabets', 'numbers', 'hyphens' and 'underscores' are allowed.`
84-
);
77+
helper.abort('Plugin name is not valid. Only \'alphabets\', \'numbers\', \'hyphens\' and \'underscores\' are allowed.');
8578
}
86-
} catch (error) {
87-
console.error(
88-
`Errorred while creating plugin!\n${error.message || error}`);
79+
} catch (err) {
80+
log(error(`Errorred while creating plugin!\n${err.message || err}`));
8981
}
90-
}
82+
};
9183

9284
var eol = 'win32' === os.platform() ? '\r\n' : '\n';
9385

@@ -227,15 +219,15 @@ var createPluginAt = function(path, name) {
227219
* @return {String} : Sanitized plugin name
228220
*/
229221
function pluginName(_plugin) {
230-
var subNames = _plugin.replace(/\-/g, '_').split('_');
222+
var subNames = _plugin.replace(/-/g, '_').split('_');
231223
var name = '';
232-
name = subNames.reduce(function(previous, current, index) {
224+
name = subNames.reduce(function(previous, current) {
233225
if (current) {
234226
previous += current.charAt(0).toUpperCase() + current.slice(1);
235227
}
236228
return previous;
237229
}, '');
238230
return name;
239-
};
231+
}
240232

241233
module.exports = Plugin;

lib/publish.js

Lines changed: 64 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
* copyright (c) Contentstack
44
* MIT Licensed
55
*/
6+
67
'use strict';
8+
79
/**
810
* Module dependencies.
911
*/
@@ -16,6 +18,7 @@ var debug_entries = require('debug')('publish:entry');
1618
var chalk = require('chalk');
1719
var async = require('async');
1820
var pkg = require('../package');
21+
1922
/*
2023
* Application variables
2124
* */
@@ -37,6 +40,7 @@ var success = chalk.green;
3740
var error = chalk.red;
3841
var warning = chalk.yellow;
3942
var info = chalk.cyan;
43+
4044
/**
4145
* Get stack's master locale
4246
* @param {Function} fn : Error first callback
@@ -55,6 +59,7 @@ function getStackMasterLocale(fn) {
5559
return fn('Stack \'api_key\' or \'access_token\' is invalid');
5660
});
5761
}
62+
5863
/**
5964
* Prompt & verify publish details
6065
* @param {String} _event : Action to be performed on the content types/assets
@@ -94,7 +99,7 @@ function Publish(_event, options) {
9499
},
95100
type: {
96101
name: 'type',
97-
description: `Send only content_types, only assets, or all content_types and assets ${_event}ing for ? [content_types/assets/all] (default:all):`,
102+
description: '\nInput types that you would like to publish: content_types|assets|all\nPress ENTER to select \'all\' content types:',
98103
required: false,
99104
conform: function(value) {
100105
return (value === 'content_types' || value === 'assets' || value === 'all');
@@ -105,7 +110,7 @@ function Publish(_event, options) {
105110
},
106111
content_types: {
107112
name: 'content_types',
108-
description: `Enter the specific content types ${_event} (default: ''):`,
113+
description: `\nEnter specific content types to ${_event}.\nPress ENTER to select \'all\' content types:`,
109114
required: false,
110115
ask: function() {
111116
return (prompt && prompt.history('type').value !== 'assets');
@@ -116,7 +121,7 @@ function Publish(_event, options) {
116121
},
117122
skip_content_types: {
118123
name: 'skip_content_types',
119-
description: `Enter the specific content types to be skipped in ${_event} (default: ''):`,
124+
description: `\nEnter the specific content types to be skipped for ${_event}\nPress ENTER for not skipping any content types:`,
120125
required: false,
121126
ask: function() {
122127
return (prompt && prompt.history('type').value !== 'assets');
@@ -127,7 +132,7 @@ function Publish(_event, options) {
127132
},
128133
language: {
129134
name: 'language',
130-
description: `Enter the code of the language that you want to ${_event} from (default: '${masterLocale}'):`,
135+
description: `\nEnter the code of the language that you want to ${_event} from (default: '${masterLocale}'):`,
131136
required: false
132137
}
133138
};
@@ -174,6 +179,7 @@ function Publish(_event, options) {
174179
}
175180
});
176181
}
182+
177183
/**
178184
* Initialize the content 'publishing' process
179185
*/
@@ -246,9 +252,10 @@ Publish.prototype.init = function() {
246252
], function(err) {
247253
if (err) {
248254
log(error(`Errorred in bulk ${inputs.event}ing on ${inputs.environment} environment.`));
249-
return;
255+
process.exit(1);
250256
}
251257
log(success(`Bulk ${inputs.event}ing finished on ${inputs.environment} environment.`));
258+
process.exit(0);
252259
return;
253260
});
254261
} catch (err) {
@@ -257,31 +264,34 @@ Publish.prototype.init = function() {
257264
return;
258265
}
259266
};
267+
260268
/**
261269
* Fetch content type's entries, and send them for publishing
262270
* @param {Object} content_types : Content type object
263271
* @param {Function} callback : Error first callback
264272
* @return {Function} : Error first callback
265273
*/
266-
Publish.prototype.loadEntries = function(content_types, callback) {
274+
Publish.prototype.loadEntries = function(contentTypes, callback) {
267275
var self = this;
268-
var bucket = [];
269-
for (var i = 0, total = content_types.length; i < total; i++) {
270-
bucket.push(function(content_type, fn) {
271-
return utility.getEntries(content_type.uid, inputs.language, {}, [], null, function(error, entries) {
272-
if (error) {
273-
return fn(error);
274-
}
275-
entries = entries || [];
276-
log(success(`A total of ${entries.length} were found in '${content_type.title}' content type`));
277-
log(info(`Sending ${content_type.uid} entries for publish`));
278-
// TODO: Important
279-
return self.publishEntries(entries, content_type, inputs.language, fn);
280-
});
281-
}(content_types[i]));
282-
}
283-
async.series(bucket, callback);
276+
return async.eachLimit(contentTypes, 1, function (contentType, cb) {
277+
return utility.getEntries(contentType.uid, inputs.language, {}, [], null, function(error, entries) {
278+
if (error) {
279+
return cb(error);
280+
}
281+
entries = entries || [];
282+
log(success(`A total of ${entries.length} were found in '${contentType.title}' content type`));
283+
log(info(`Sending ${contentType.title} content type entries for publish`));
284+
// TODO: Important
285+
return self.publishEntries(entries, contentType, inputs.language, cb);
286+
});
287+
}, function (error) {
288+
if (error) {
289+
return callback(error);
290+
}
291+
return callback(null, null);
292+
});
284293
};
294+
285295
/**
286296
* Get content types
287297
* @param {Function} callback : Error fist callback
@@ -321,6 +331,7 @@ Publish.prototype.content_types = function(callback) {
321331
return callback(null, null);
322332
});
323333
};
334+
324335
/**
325336
* Send entries to publish
326337
* @param {Object} entries : Entries to be sent for publishing
@@ -331,44 +342,38 @@ Publish.prototype.content_types = function(callback) {
331342
*/
332343
Publish.prototype.publishEntries = function(entries, content_type, language, finalCallback) {
333344
var self = this;
334-
var publishRequests = [];
335-
for (var i = 0; i < entries.length; i++) {
336-
publishRequests.push(function(entry) {
337-
return (function(fn) {
338-
var entry_id = (entry._metadata) ? entry._metadata.uid : entry.uid;
339-
var _options = {
340-
url: `${api.host}/${api.version}${api.urls.content_types}${content_type.uid}${api.urls.entries}${entry_id}/${inputs.event}`,
341-
headers: headers,
342-
method: 'POST',
343-
json: {
344-
entry: {
345-
title: entry[content_type.entry_title_field] || entry_id,
346-
entry_uid: entry_id,
347-
environments: inputs.environment,
348-
locale: entry.locale
349-
},
350-
content_type: {
351-
title: content_type.title,
352-
uid: content_type.uid
353-
},
354-
locale: inputs.languages
355-
}
356-
};
357-
return request(_options, function(error, body) {
358-
if (error) {
359-
return fn(error);
360-
}
361-
self.totalEntryCount++;
362-
return fn(null, body);
363-
});
364-
});
365-
}(entries[i]));
366-
}
367-
return async.eachLimit(publishRequests, ORG_RATE_LIMIT, function(error) {
345+
return async.eachLimit(entries, ORG_RATE_LIMIT,function(entry, cb) {
346+
var entry_id = (entry._metadata) ? entry._metadata.uid : entry.uid;
347+
var _options = {
348+
url: `${api.host}/${api.version}${api.urls.content_types}${content_type.uid}${api.urls.entries}${entry_id}/${inputs.event}`,
349+
headers: headers,
350+
method: 'POST',
351+
json: {
352+
entry: {
353+
title: entry[content_type.entry_title_field] || entry_id,
354+
entry_uid: entry_id,
355+
environments: inputs.environment,
356+
locale: entry.locale
357+
},
358+
content_type: {
359+
title: content_type.title,
360+
uid: content_type.uid
361+
},
362+
locale: inputs.languages
363+
}
364+
};
365+
return request(_options, function(error, body) {
366+
if (error) {
367+
return cb(error);
368+
}
369+
self.totalEntryCount++;
370+
return cb(null, body);
371+
});
372+
}, function (error) {
368373
if (error) {
369374
return finalCallback(error);
370375
}
371-
log(success(`Content type ${content_type.title} with ${publishRequests.length} entries was published successfully!`));
376+
log(success(`Content type ${content_type.title} with ${self.totalEntryCount} entries was published successfully!`));
372377
return finalCallback(null, null);
373378
});
374379
};
@@ -399,6 +404,7 @@ Publish.prototype.assets = function(callback) {
399404
return callback(null, 'No assets were found in the stack!');
400405
});
401406
};
407+
402408
/**
403409
* Send assets for publishing
404410
* @param {Object} assets : Assets to be sent for publishing
@@ -440,4 +446,5 @@ Publish.prototype.publishAssets = function(assets, finalCallback) {
440446
return finalCallback(null);
441447
});
442448
};
449+
443450
module.exports = Publish;

0 commit comments

Comments
 (0)