8
8
import { Command , Flags } from '@oclif/core' ;
9
9
import { fs , Messages } from '@salesforce/core' ;
10
10
import { Env } from '@salesforce/kit' ;
11
- import { Deployable , Deployer , generateTableChoices , Prompter } from '@salesforce/plugin-project-utils' ;
11
+ import {
12
+ Deployable ,
13
+ Deployer ,
14
+ generateTableChoices ,
15
+ ProjectDeployOptions ,
16
+ Prompter ,
17
+ } from '@salesforce/plugin-project-utils' ;
12
18
13
19
Messages . importMessagesDirectory ( __dirname ) ;
14
20
15
21
const messages = Messages . loadMessages ( '@salesforce/plugin-project' , 'project.deploy' ) ;
16
22
23
+ export const DEPLOY_OPTIONS_FILE = 'project-deploy-options.json' ;
24
+
17
25
export default class ProjectDeploy extends Command {
18
26
public static summary = messages . getMessage ( 'summary' ) ;
19
27
public static description = messages . getMessage ( 'description' ) ;
@@ -31,23 +39,41 @@ export default class ProjectDeploy extends Command {
31
39
const { flags } = await this . parse ( ProjectDeploy ) ;
32
40
33
41
flags . interactive = await this . isInteractive ( flags . interactive ) ;
42
+ const options = await this . readOptions ( ) ;
34
43
35
44
this . log ( 'Analyzing project' ) ;
36
45
37
- let deployers = ( await this . config . runHook ( 'project:findDeployers' , { } ) ) as Deployer [ ] ;
46
+ if ( ! flags . interactive ) {
47
+ this . log ( `Using options found in ${ DEPLOY_OPTIONS_FILE } ` ) ;
48
+ }
49
+
50
+ let deployers = ( await this . config . runHook ( 'project:findDeployers' , options ) ) as Deployer [ ] ;
38
51
deployers = deployers . reduce ( ( x , y ) => x . concat ( y ) , [ ] as Deployer [ ] ) ;
39
52
40
53
if ( deployers . length === 0 ) {
41
54
this . log ( 'Found nothing in the project to deploy' ) ;
42
55
} else {
43
- deployers = await this . selectDeployers ( deployers ) ;
56
+ if ( flags . interactive ) {
57
+ deployers = await this . selectDeployers ( deployers ) ;
58
+ }
44
59
45
60
if ( deployers . length === 0 ) {
46
61
this . log ( 'Nothing was selected to deploy.' ) ;
47
62
}
48
63
64
+ const deployOptions : ProjectDeployOptions = { } ;
65
+ for ( const deployer of deployers ) {
66
+ const opts = options [ deployer . getName ( ) ] ?? { } ;
67
+ deployOptions [ deployer . getName ( ) ] = await deployer . setup ( flags , opts ) ;
68
+ }
69
+
70
+ if ( flags . interactive && ( await this . askToSave ( ) ) ) {
71
+ await fs . writeJson ( DEPLOY_OPTIONS_FILE , deployOptions , { space : 2 } ) ;
72
+ this . log ( ) ;
73
+ this . log ( `Your deploy options have been saved to ${ DEPLOY_OPTIONS_FILE } ` ) ;
74
+ }
75
+
49
76
for ( const deployer of deployers ) {
50
- await deployer . setup ( flags ) ;
51
77
await deployer . deploy ( ) ;
52
78
}
53
79
}
@@ -59,10 +85,28 @@ export default class ProjectDeploy extends Command {
59
85
*/
60
86
public async isInteractive ( interactive : boolean ) : Promise < boolean > {
61
87
if ( interactive ) return true ;
62
- const deployFileExists = await fs . fileExists ( 'project-deploy-options.json' ) ;
88
+ const deployFileExists = await fs . fileExists ( DEPLOY_OPTIONS_FILE ) ;
63
89
return deployFileExists ? false : true ;
64
90
}
65
91
92
+ public async readOptions ( ) : Promise < ProjectDeployOptions > {
93
+ if ( await fs . fileExists ( DEPLOY_OPTIONS_FILE ) ) {
94
+ return ( await fs . readJson ( DEPLOY_OPTIONS_FILE ) ) as ProjectDeployOptions ;
95
+ } else {
96
+ return { } ;
97
+ }
98
+ }
99
+
100
+ public async askToSave ( ) : Promise < boolean > {
101
+ const prompter = new Prompter ( ) ;
102
+ const { save } = await prompter . prompt < { save : boolean } > ( {
103
+ name : 'save' ,
104
+ message : 'Would you like to save these deploy options for future runs?' ,
105
+ type : 'confirm' ,
106
+ } ) ;
107
+ return save ;
108
+ }
109
+
66
110
public async selectDeployers ( deployers : Deployer [ ] ) : Promise < Deployer [ ] > {
67
111
const deployables : Deployable [ ] = deployers . reduce ( ( x , y ) => x . concat ( y . deployables ) , [ ] as Deployable [ ] ) ;
68
112
const columns = { name : 'APP OR PACKAGE' , type : 'TYPE' , path : 'PATH' } ;
0 commit comments