1+ /* eslint-disable @typescript-eslint/no-unsafe-assignment */
2+ /* eslint-disable @typescript-eslint/no-unsafe-member-access */
3+ /* eslint-disable @typescript-eslint/no-explicit-any */
4+ /*
5+ * Copyright (c) 2024, your-company, inc.
6+ * All rights reserved.
7+ * Licensed under the BSD 3-Clause license.
8+ * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
9+ */
10+ import * as os from 'os' ;
11+ import { flags } from '@salesforce/command' ;
12+ import { Messages } from '@salesforce/core' ;
13+ import '../../../utils/prototypes' ;
14+ import OmniStudioBaseCommand from '../../basecommand' ;
15+ import { LWCComponentMigrationTool , CustomLabelMigrationTool , ApexClassMigrationTool } from '../../../migration/interfaces' ;
16+ import { DebugTimer , MigratedObject , MigratedRecordInfo } from '../../../utils' ;
17+ import { MigrationResult , MigrationTool } from '../../../migration/interfaces' ;
18+ import { ResultsBuilder } from '../../../utils/resultsbuilder' ;
19+
20+ // Initialize Messages with the current plugin directory
21+ Messages . importMessagesDirectory ( __dirname ) ;
22+
23+ // Load the specific messages for this file. Messages from @salesforce /command, @salesforce/core,
24+ // or any library that is using the messages framework can also be loaded this way.
25+ const messages = Messages . loadMessages ( '@salesforce/plugin-omnistudio-related-object-migration-tool' , 'migrate' ) ;
26+
27+ export default class OmnistudioRelatedObjectMigrationFacade extends OmniStudioBaseCommand {
28+ public static description = messages . getMessage ( 'commandDescription' ) ;
29+
30+ public static examples = messages . getMessage ( 'examples' ) . split ( os . EOL ) ;
31+
32+ public static args = [ { name : 'file' } ] ;
33+
34+ protected static flagsConfig = {
35+ namespace : flags . string ( {
36+ char : 'n' ,
37+ description : messages . getMessage ( 'namespaceFlagDescription' ) ,
38+ } ) ,
39+ only : flags . string ( {
40+ char : 'o' ,
41+ description : messages . getMessage ( 'onlyFlagDescription' ) ,
42+ } ) ,
43+ allversions : flags . boolean ( {
44+ char : 'a' ,
45+ description : messages . getMessage ( 'allVersionsDescription' ) ,
46+ required : false ,
47+ } ) ,
48+ } ;
49+
50+ public async migrateAll ( migrationResult : MigrationResult , namespace : string , relatedObjects : string [ ] ) : Promise < any > {
51+ const apiVersion = '55.0' ; // Define the API version or make it configurable
52+ const conn = this . org . getConnection ( ) ;
53+ conn . setApiVersion ( apiVersion ) ;
54+
55+ // Start the debug timer
56+ DebugTimer . getInstance ( ) . start ( ) ;
57+
58+ // Register the migration tools based on the relatedObjects parameter
59+ let migrationTools : MigrationTool [ ] = [ ] ;
60+ if ( relatedObjects . includes ( 'lwc' ) ) {
61+ migrationTools . push ( new LWCComponentMigrationTool ( namespace , conn , this . logger , messages , this . ux ) ) ;
62+ }
63+ if ( relatedObjects . includes ( 'labels' ) ) {
64+ migrationTools . push ( new CustomLabelMigrationTool ( namespace , conn , this . logger , messages , this . ux ) ) ;
65+ }
66+ if ( relatedObjects . includes ( 'apex' ) ) {
67+ migrationTools . push ( new ApexClassMigrationTool ( namespace , conn , this . logger , messages , this . ux ) ) ;
68+ }
69+
70+ if ( migrationTools . length === 0 ) {
71+ throw new Error ( messages . getMessage ( 'noMigrationToolsSelected' ) ) ;
72+ }
73+
74+ // Migrate individual objects
75+ const debugTimer = DebugTimer . getInstance ( ) ;
76+ let objectMigrationResults : MigratedObject [ ] = [ ] ;
77+
78+ // Truncate existing objects if necessary
79+ let allTruncateComplete = true ;
80+ for ( const tool of migrationTools . reverse ( ) ) {
81+ try {
82+ this . ux . log ( 'Cleaning: ' + tool . getName ( ) ) ;
83+ debugTimer . lap ( 'Cleaning: ' + tool . getName ( ) ) ;
84+ await tool . truncate ( ) ;
85+ } catch ( ex : any ) {
86+ allTruncateComplete = false ;
87+ objectMigrationResults . push ( {
88+ name : tool . getName ( ) ,
89+ errors : [ ex . message ] ,
90+ } ) ;
91+ }
92+ }
93+
94+ if ( allTruncateComplete ) {
95+ for ( const tool of migrationTools . reverse ( ) ) {
96+ try {
97+ this . ux . log ( 'Migrating: ' + tool . getName ( ) ) ;
98+ debugTimer . lap ( 'Migrating: ' + tool . getName ( ) ) ;
99+ const results = await tool . migrate ( ) ;
100+
101+ objectMigrationResults = objectMigrationResults . concat (
102+ results . map ( ( r ) => ( {
103+ name : r . name ,
104+ data : this . mergeRecordAndUploadResults ( r , tool ) ,
105+ } ) )
106+ ) ;
107+ } catch ( ex : any ) {
108+ this . logger . error ( JSON . stringify ( ex ) ) ;
109+ objectMigrationResults . push ( {
110+ name : tool . getName ( ) ,
111+ errors : [ ex . message ] ,
112+ } ) ;
113+ }
114+ }
115+ }
116+
117+ // Stop the debug timer
118+ const timer = DebugTimer . getInstance ( ) . stop ( ) ;
119+
120+ await ResultsBuilder . generate ( objectMigrationResults , conn . instanceUrl ) ;
121+
122+ // Save timer to debug logger
123+ this . logger . debug ( timer ) ;
124+
125+ // Return results needed for --json flag
126+ return { objectMigrationResults } ;
127+ }
128+
129+ private mergeRecordAndUploadResults (
130+ migrationResults : MigrationResult ,
131+ migrationTool : MigrationTool
132+ ) : MigratedRecordInfo [ ] {
133+ const mergedResults : MigratedRecordInfo [ ] = [ ] ;
134+
135+ for ( const record of Array . from ( migrationResults . records . values ( ) ) ) {
136+ const obj = {
137+ id : record [ 'Id' ] ,
138+ name : migrationTool . getRecordName ( record ) ,
139+ status : 'Skipped' ,
140+ errors : record [ 'errors' ] ,
141+ migratedId : undefined ,
142+ warnings : [ ] ,
143+ migratedName : '' ,
144+ } ;
145+
146+ if ( migrationResults . results . has ( record [ 'Id' ] ) ) {
147+ const recordResults = migrationResults . results . get ( record [ 'Id' ] ) ;
148+
149+ let errors : any [ ] = obj . errors || [ ] ;
150+ errors = errors . concat ( recordResults . errors || [ ] ) ;
151+
152+ obj . status = ! recordResults || recordResults . hasErrors ? 'Error' : 'Complete' ;
153+ obj . errors = errors ;
154+ obj . migratedId = recordResults . id ;
155+ obj . warnings = recordResults . warnings ;
156+ obj . migratedName = recordResults . newName ;
157+ }
158+
159+ mergedResults . push ( obj ) ;
160+ }
161+
162+ return mergedResults ;
163+ }
164+ }
0 commit comments