@@ -13,6 +13,7 @@ import { get, init as initAPI } from '../api'
1313import { existsSync , readFileSync } from '../util/fs'
1414import { filterItems , formatItems , groupItems , markCheckpoint } from '../util/index'
1515import { logger } from '../util/logger'
16+ import { MESSAGES } from '../util/messages'
1617import { map } from '../util/promise.map'
1718import { netConnectivityIssues } from './inet'
1819import { Q as Queue } from './q'
@@ -76,15 +77,15 @@ export const init = (contentStore, assetStore) => {
7677 config = getConfig ( )
7778 Q = new Queue ( contentStore , assetStore , config )
7879 initAPI ( config . contentstack )
79- debug ( 'Sync core:start invoked' )
80+ debug ( MESSAGES . SYNC_CORE . START )
8081
8182 return new Promise ( ( resolve , reject ) => {
8283 try {
8384 Contentstack = config . contentstack
8485 const checkPointConfig : ICheckpoint = config . checkpoint
8586 const paths = config . paths
8687 const environment = Contentstack . environment || process . env . NODE_ENV || 'development'
87- debug ( `Environment: ${ environment } ` )
88+ debug ( MESSAGES . SYNC_CORE . ENVIRONMENT ( environment ) )
8889 const request : any = {
8990 qs : {
9091 environment,
@@ -136,23 +137,23 @@ const loadCheckpoint = (checkPointConfig: ICheckpoint, paths: any): void => {
136137
137138 // Set sync token if checkpoint is found
138139 if ( checkpoint ) {
139- debug ( "Found sync token in checkpoint file:" , checkpoint ) ;
140+ debug ( MESSAGES . SYNC_CORE . TOKEN_FOUND , checkpoint ) ;
140141 Contentstack . sync_token = checkpoint . token ;
141- debug ( "Using sync token:" , Contentstack . sync_token ) ;
142+ debug ( MESSAGES . SYNC_CORE . TOKEN_USING , Contentstack . sync_token ) ;
142143 }
143144} ;
144145
145146
146147function readHiddenFile ( filePath : string ) {
147148 try {
148149 if ( ! fs . existsSync ( filePath ) ) {
149- logger . error ( "File does not exist:" , filePath ) ;
150+ logger . error ( MESSAGES . SYNC_CORE . FILE_NOT_FOUND ( filePath ) ) ;
150151 return ;
151152 }
152153 const data = fs . readFileSync ( filePath , "utf8" ) ;
153154 return JSON . parse ( data ) ;
154155 } catch ( err ) {
155- logger . error ( "Error reading file:" , err ) ;
156+ logger . error ( MESSAGES . SYNC_CORE . FILE_READ_ERROR ( err ) ) ;
156157 return undefined ;
157158 }
158159}
@@ -174,15 +175,15 @@ export const pop = () => {
174175 */
175176export const poke = async ( ) => {
176177 try {
177- debug ( 'Invoked poke' ) ;
178- logger . info ( 'Received \'contentstack sync\' notification' )
178+ debug ( MESSAGES . SYNC_CORE . POKE_INVOKED ) ;
179+ logger . info ( MESSAGES . SYNC_CORE . POKE_NOTIFICATION )
179180 if ( ! flag . lockdown ) {
180181 flag . WQ = true
181182 return await check ( )
182183 }
183184 return null ;
184185 } catch ( error ) {
185- debug ( 'Error [poke]' , error ) ;
186+ debug ( MESSAGES . SYNC_CORE . POKE_ERROR , error ) ;
186187 throw error ;
187188 }
188189}
@@ -193,24 +194,24 @@ export const poke = async () => {
193194 */
194195const check = async ( ) => {
195196 try {
196- debug ( `Check called. SQ status is ${ flag . SQ } and WQ status is ${ flag . WQ } ` )
197+ debug ( MESSAGES . SYNC_CORE . CHECK_CALLED ( flag . SQ , flag . WQ ) )
197198 if ( ! flag . SQ && flag . WQ ) {
198199 flag . WQ = false
199200 flag . SQ = true
200201 await sync ( ) ;
201- debug ( `Sync completed and SQ flag updated. Cooloff duration is ${ config . syncManager . cooloff } ` )
202+ debug ( MESSAGES . SYNC_CORE . CHECK_COMPLETE ( config . syncManager . cooloff ) )
202203 setTimeout ( ( ) => {
203204 flag . SQ = false
204205 emitter . emit ( 'check' )
205206 } , config . syncManager . cooloff )
206207 }
207208 } catch ( error ) {
208209 logger . error ( error )
209- debug ( 'Error [check]' , error ) ;
210+ debug ( MESSAGES . SYNC_CORE . CHECK_ERROR , error ) ;
210211 check ( ) . then ( ( ) => {
211- debug ( 'passed [check] error' ) ;
212+ debug ( MESSAGES . SYNC_CORE . CHECK_RECOVERED ) ;
212213 } ) . catch ( ( error ) => {
213- debug ( 'failed [check] error' , error ) ;
214+ debug ( MESSAGES . SYNC_CORE . CHECK_FAILED , error ) ;
214215 } ) ;
215216 throw error ;
216217 }
@@ -221,9 +222,9 @@ const check = async () => {
221222 */
222223const sync = async ( ) => {
223224 try {
224- debug ( 'started [sync]' ) ;
225+ debug ( MESSAGES . SYNC_CORE . SYNC_STARTED ) ;
225226 const tokenObject = await getToken ( ) ;
226- debug ( 'tokenObject [sync]' , tokenObject ) ;
227+ debug ( MESSAGES . SYNC_CORE . SYNC_TOKEN_OBJECT , tokenObject ) ;
227228 const token : IToken = ( tokenObject as IToken )
228229 const request : any = {
229230 qs : {
@@ -234,7 +235,7 @@ const sync = async () => {
234235 }
235236 return await fire ( request )
236237 } catch ( error ) {
237- debug ( 'Error [sync]' , error ) ;
238+ debug ( MESSAGES . SYNC_CORE . SYNC_ERROR , error ) ;
238239 throw error
239240 }
240241}
@@ -243,15 +244,15 @@ const sync = async () => {
243244 * @description Used to lockdown the 'sync' process in case of exceptions
244245 */
245246export const lock = ( ) => {
246- debug ( 'Contentstack sync locked..' )
247+ debug ( MESSAGES . SYNC_CORE . SYNC_LOCKED )
247248 flag . lockdown = true
248249}
249250
250251/**
251252 * @description Used to unlock the 'sync' process in case of errors/exceptions
252253 */
253254export const unlock = ( refire ?: boolean ) => {
254- debug ( 'Contentstack sync unlocked..' , refire )
255+ debug ( MESSAGES . SYNC_CORE . SYNC_UNLOCKED , refire )
255256 flag . lockdown = false
256257 if ( typeof refire === 'boolean' && refire ) {
257258 flag . WQ = true
@@ -269,7 +270,7 @@ export const unlock = (refire?: boolean) => {
269270 * @param {Object } req - Contentstack sync API request object
270271 */
271272const fire = ( req : IApiRequest ) => {
272- debug ( `Fire called with: ${ JSON . stringify ( req ) } ` )
273+ debug ( MESSAGES . SYNC_CORE . FIRE_CALLED ( req ) )
273274 flag . SQ = true
274275
275276 return new Promise ( ( resolve , reject ) => {
@@ -279,7 +280,7 @@ const fire = (req: IApiRequest) => {
279280 delete req . qs . sync_token
280281 delete req . path
281282 const syncResponse : ISyncResponse = response
282- debug ( 'Response [fire]' , syncResponse . items . length ) ;
283+ debug ( MESSAGES . SYNC_CORE . FIRE_COMPLETE ( syncResponse . items . length ) ) ;
283284 if ( syncResponse . items . length ) {
284285 return filterItems ( syncResponse , config ) . then ( ( ) => {
285286 if ( syncResponse . items . length === 0 ) {
@@ -319,7 +320,7 @@ const fire = (req: IApiRequest) => {
319320 return map ( contentTypeUids , ( uid ) => {
320321
321322 return new Promise ( ( mapResolve , mapReject ) => {
322- debug ( `API called with for content type: ${ uid } ` )
323+ debug ( MESSAGES . SYNC_CORE . API_CALL_CT ( uid ) )
323324 return get ( {
324325 path : `${ Contentstack . apis . content_types } ${ uid } ` ,
325326 qs : {
@@ -344,7 +345,7 @@ const fire = (req: IApiRequest) => {
344345
345346 return mapReject ( err )
346347 } ) . catch ( ( error ) => {
347- debug ( 'Error [map] fetching content type schema:' , error )
348+ debug ( MESSAGES . SYNC_CORE . ERROR_MAP , error )
348349 if ( netConnectivityIssues ( error ) ) {
349350 flag . SQ = false
350351 }
@@ -361,11 +362,11 @@ const fire = (req: IApiRequest) => {
361362 flag . SQ = false
362363 }
363364 // Errorred while fetching content type schema
364- debug ( 'Error [mapResolve]:' , error )
365+ debug ( MESSAGES . SYNC_CORE . ERROR_MAP_RESOLVE , error )
365366 return reject ( error )
366367 } )
367368 } ) . catch ( ( processError ) => {
368- debug ( 'Error [filterItems]:' , processError )
369+ debug ( MESSAGES . SYNC_CORE . ERROR_FILTER_ITEMS , processError )
369370 return reject ( processError )
370371 } )
371372 }
@@ -374,7 +375,7 @@ const fire = (req: IApiRequest) => {
374375 . then ( resolve )
375376 . catch ( reject )
376377 } ) . catch ( ( error ) => {
377- debug ( 'Error [fire]' , error ) ;
378+ debug ( MESSAGES . SYNC_CORE . ERROR_FIRE , error ) ;
378379 if ( netConnectivityIssues ( error ) ) {
379380 flag . SQ = false
380381 }
@@ -406,7 +407,7 @@ const postProcess = (req, resp) => {
406407 req . qs [ name ] = resp [ name ]
407408
408409 if ( flag . lockdown ) {
409- logger . log ( 'Checkpoint: lockdown has been invoked' )
410+ logger . log ( MESSAGES . SYNC_CORE . CHECKPOINT_LOCKDOWN )
410411 flag . requestCache = {
411412 params : req ,
412413 reject,
@@ -419,7 +420,7 @@ const postProcess = (req, resp) => {
419420 return resolve ( '' )
420421 }
421422
422- debug ( `Re-Fire called with: ${ JSON . stringify ( req ) } ` )
423+ debug ( MESSAGES . SYNC_CORE . REFIRE_CALLED ( req ) )
423424 return fire ( req )
424425 . then ( resolve )
425426 . catch ( reject ) ;
0 commit comments