@@ -5,7 +5,17 @@ import { log, handleAndLogError, CLIProgressManager } from '@contentstack/cli-ut
55import { PATH_CONSTANTS } from '../../constants' ;
66
77import BaseClass , { ApiOptions } from './base-class' ;
8- import { fsUtil , fileHelper , MODULE_CONTEXTS , MODULE_NAMES , PROCESS_STATUS , PROCESS_NAMES } from '../../utils' ;
8+ import {
9+ fsUtil ,
10+ fileHelper ,
11+ MODULE_CONTEXTS ,
12+ MODULE_NAMES ,
13+ PROCESS_STATUS ,
14+ PROCESS_NAMES ,
15+ readEnvUidMapperSync ,
16+ warnIfEnvMapperEmpty ,
17+ serializePublishTaxonomies ,
18+ } from '../../utils' ;
919import { ModuleClassParams , TaxonomiesConfig } from '../../types' ;
1020
1121export default class ImportTaxonomies extends BaseClass {
@@ -20,7 +30,6 @@ export default class ImportTaxonomies extends BaseClass {
2030 private termsFailsPath : string ;
2131 private localesFilePath : string ;
2232 private envUidMapperPath : string ;
23- private envUidMapper : Record < string , string > = { } ;
2433 private isLocaleBasedStructure : boolean = false ;
2534 public createdTaxonomies : Record < string , unknown > = { } ;
2635 public failedTaxonomies : Record < string , unknown > = { } ;
@@ -387,30 +396,6 @@ export default class ImportTaxonomies extends BaseClass {
387396
388397 // --- Publish ---
389398
390- /**
391- * Reads source env UID → destination stack env UID map produced during environments import.
392- */
393- private readEnvUidMapperSync ( ) : Record < string , string > {
394- if ( ! fileHelper . fileExistsSync ( this . envUidMapperPath ) ) {
395- log . debug ( `Environment UID mapper not found at ${ this . envUidMapperPath } ` , this . importConfig . context ) ;
396- return { } ;
397- }
398-
399- try {
400- const raw = fsUtil . readFile ( this . envUidMapperPath , true ) as Record < string , unknown > ;
401- const out : Record < string , string > = { } ;
402- for ( const [ k , v ] of Object . entries ( raw || { } ) ) {
403- if ( v !== undefined && v !== null && String ( v ) . trim ( ) !== '' ) {
404- out [ k ] = String ( v ) ;
405- }
406- }
407- return out ;
408- } catch {
409- log . debug ( 'Failed to read environment UID mapper' , this . importConfig . context ) ;
410- return { } ;
411- }
412- }
413-
414399 private countPublishEligibleTaxonomies ( envMapper : Record < string , string > ) : number {
415400 let count = 0 ;
416401 for ( const key of Object . keys ( this . taxonomies || { } ) ) {
@@ -454,18 +439,9 @@ export default class ImportTaxonomies extends BaseClass {
454439 return jobs ;
455440 }
456441
457- private loadEnvUidMapper ( ) : void {
458- this . envUidMapper = this . readEnvUidMapperSync ( ) ;
459- if ( isEmpty ( this . envUidMapper ) ) {
460- log . warn (
461- 'Environment UID mapper is empty; taxonomy publishing is skipped. Import environments first or ensure mapper/environments/uid-mapping.json exists.' ,
462- this . importConfig . context ,
463- ) ;
464- }
465- }
466-
467442 async processTaxonomyPublishing ( ) : Promise < void > {
468- this . loadEnvUidMapper ( ) ;
443+ const envUidMapper = readEnvUidMapperSync ( this . envUidMapperPath , this . importConfig . context ) ;
444+ warnIfEnvMapperEmpty ( envUidMapper , this . importConfig . context ) ;
469445 const jobs = this . collectTaxonomyPublishJobs ( ) ;
470446
471447 if ( jobs . length === 0 ) {
@@ -506,7 +482,7 @@ export default class ImportTaxonomies extends BaseClass {
506482 apiContent : jobs as unknown as Record < string , any > [ ] ,
507483 processName : 'publish taxonomies' ,
508484 apiParams : {
509- serializeData : this . serializePublishTaxonomies . bind ( this ) ,
485+ serializeData : ( opts : ApiOptions ) => serializePublishTaxonomies ( opts , envUidMapper ) ,
510486 reject : onReject ,
511487 resolve : onSuccess ,
512488 entity : 'publish-taxonomies' ,
@@ -519,43 +495,6 @@ export default class ImportTaxonomies extends BaseClass {
519495 ) ;
520496 }
521497
522- /**
523- * Builds taxonomy publish payload: destination env UIDs from mapper, locales from taxonomy.locale, items: [{ uid }].
524- */
525- serializePublishTaxonomies ( apiOptions : ApiOptions ) : ApiOptions {
526- const job = apiOptions . apiData as { taxonomy ?: Record < string , any > } ;
527- const taxonomy = job ?. taxonomy ;
528-
529- if ( ! taxonomy ?. publish_details ?. length || ! taxonomy ?. locale ) {
530- apiOptions . apiData = undefined ;
531- return apiOptions ;
532- }
533-
534- const environments : string [ ] = [ ] ;
535- for ( const pub of taxonomy . publish_details as any [ ] ) {
536- const sourceEnvUid = pub ?. environment ;
537- if ( ! sourceEnvUid ) continue ;
538- const destUid = this . envUidMapper [ String ( sourceEnvUid ) ] ;
539- if ( destUid && ! environments . includes ( destUid ) ) {
540- environments . push ( destUid ) ;
541- }
542- }
543-
544- if ( environments . length === 0 ) {
545- apiOptions . apiData = undefined ;
546- return apiOptions ;
547- }
548-
549- const locales = [ String ( taxonomy . locale ) ] ;
550- apiOptions . apiData = {
551- environments,
552- locales,
553- items : [ { uid : taxonomy . uid } ] ,
554- } ;
555-
556- return apiOptions ;
557- }
558-
559498 // --- Mapper output ---
560499
561500 /**
@@ -631,7 +570,7 @@ export default class ImportTaxonomies extends BaseClass {
631570 this . isLocaleBasedStructure = this . detectAndScanLocaleStructure ( ) ;
632571
633572 const taxonomyCount = Object . keys ( this . taxonomies || { } ) . length ;
634- const envMapper = this . readEnvUidMapperSync ( ) ;
573+ const envMapper = readEnvUidMapperSync ( this . envUidMapperPath , this . importConfig . context ) ;
635574 const publishJobCount = this . countPublishEligibleTaxonomies ( envMapper ) ;
636575
637576 log . debug (
0 commit comments