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');
1618var chalk = require ( 'chalk' ) ;
1719var async = require ( 'async' ) ;
1820var pkg = require ( '../package' ) ;
21+
1922/*
2023 * Application variables
2124 * */
@@ -37,6 +40,7 @@ var success = chalk.green;
3740var error = chalk . red ;
3841var warning = chalk . yellow ;
3942var 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 */
332343Publish . 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+
443450module . exports = Publish ;
0 commit comments