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