1
- import { Logger , LogManager } from '@vlocode/core' ;
2
- import { OmniScriptActivator , OmniScriptVersionDetail } from '@vlocode/vlocity-deploy' ;
1
+ import { Logger , LogManager , FileSystem } from '@vlocode/core' ;
2
+ import { OmniScriptActivator , OmniScriptVersionDetail , ScriptDefinitionProvider } from '@vlocode/vlocity-deploy' ;
3
3
import { Argument , Option } from '../command' ;
4
4
import * as logSymbols from 'log-symbols' ;
5
5
import { forEachAsyncParallel , getErrorMessage , groupBy , isSalesforceId , Iterable , sortBy , Timer } from '@vlocode/util' ;
@@ -40,6 +40,8 @@ export default class extends SalesforceCommand {
40
40
new Option ( '--remote-activation' , 'use anonymous apex to activate OmniScripts.' +
41
41
'By default Vlocode will generate script definitions locally which is faster and more reliable than remote activation. ' +
42
42
'Enable this when you experience issues or inconsistencies in scripts deployed through Vlocode.' ) . default ( false ) ,
43
+ new Option ( '--debug-activation' , 'save the updated script definitions as JSON file. ' +
44
+ 'Use this option while debugging to compare scripts activate with `--remote-activation` and local activation' ) . default ( false ) ,
43
45
] ;
44
46
45
47
constructor ( private logger : Logger = LogManager . get ( 'vlocode-cli' ) ) {
@@ -73,8 +75,13 @@ export default class extends SalesforceCommand {
73
75
}
74
76
75
77
await forEachAsyncParallel ( scriptsToActivate , async info => {
78
+ if ( options . debugActivation ) {
79
+ // Save scripts before running activator when debugging
80
+ await this . saveDefinition ( info . script , { preFix : 'before' } ) ;
81
+ }
76
82
this . logger . info ( `Activating ${ info . type } (version: ${ info . script . version } , id: ${ info . script . id } )` ) ;
77
83
try {
84
+ // Activation
78
85
await activator . activate ( info . script . id , {
79
86
toolingApi : ! options . useMetadataApi ,
80
87
skipLwcDeployment : options . skipLwc ,
@@ -83,6 +90,10 @@ export default class extends SalesforceCommand {
83
90
} ) ;
84
91
info . status = 'activated' ;
85
92
this . logger . info ( `${ logSymbols . success } Activated: ${ info . type } (${ info . script . id } )` ) ;
93
+
94
+ if ( options . debugActivation ) {
95
+ await this . saveDefinition ( info . script , { preFix : 'after' } ) ;
96
+ }
86
97
} catch ( error ) {
87
98
info . status = 'error' ;
88
99
errors . push ( { script : info . type , error } ) ;
@@ -108,6 +119,26 @@ export default class extends SalesforceCommand {
108
119
}
109
120
}
110
121
122
+
123
+ private async saveDefinition ( script : OmniScriptVersionDetail , options ?: { preFix ?: string , postFix ?: string } ) {
124
+ const fileNameParts = [
125
+ options ?. preFix ?? '' ,
126
+ script . type ,
127
+ script . subType ,
128
+ options ?. postFix ?? ''
129
+ ] ;
130
+ const fileName = ( fileNameParts . filter ( f => ! ! f ) . join ( '-' ) . replace ( / [ \s ] + / g, '' ) . replace ( / [ ^ a - z 0 - 9 _ - ] + / ig, '' ) + `.json` ) . toLowerCase ( ) ;
131
+ try {
132
+ const definition = await this . container . get ( ScriptDefinitionProvider ) . getScriptDefinition ( script . id ) ;
133
+ if ( definition ) {
134
+ this . logger . info ( `Saving definition for ${ script . id } to ${ fileName } ` ) ;
135
+ await this . container . get ( FileSystem ) . outputFile ( fileName , JSON . stringify ( definition , null , 4 ) ) ;
136
+ }
137
+ } catch ( error ) {
138
+ this . logger . error ( `Failed to save definition for ${ script . id } to ${ fileName } : ${ getErrorMessage ( error ) } ` ) ;
139
+ }
140
+ }
141
+
111
142
private * getScriptsToActivate ( scripts : Map < string , ScriptActivationInfo > ) {
112
143
for ( const [ , script ] of scripts ) {
113
144
if ( script . status === 'activated' || script . status === 'error' ) {
0 commit comments