5
5
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
6
6
*/
7
7
8
+ import * as os from 'os' ;
8
9
import { flags , FlagsConfig , SfdxCommand } from '@salesforce/command' ;
9
- import { Messages , SfdxPropertyKeys } from '@salesforce/core' ;
10
+ import { Messages , SfError } from '@salesforce/core' ;
11
+ import { SaveResult } from 'jsforce' ;
12
+ import {
13
+ BY_LABEL ,
14
+ getHasMetadataRemoved ,
15
+ getPackageIdFromAlias ,
16
+ getPackageVersionId ,
17
+ Package ,
18
+ validateId ,
19
+ } from '@salesforce/packaging' ;
10
20
11
21
Messages . importMessagesDirectory ( __dirname ) ;
12
22
const messages = Messages . loadMessages ( '@salesforce/plugin-packaging' , 'package_version_promote' ) ;
13
23
24
+ export type PackageVersionPromoteResponse = { id : string ; success : boolean ; errors : Error [ ] } ;
25
+
14
26
export class PackageVersionPromoteCommand extends SfdxCommand {
15
27
public static readonly description = messages . getMessage ( 'cliDescription' ) ;
16
28
public static readonly longDescription = messages . getMessage ( 'cliDescriptionLong' ) ;
17
29
public static readonly help = messages . getMessage ( 'help' ) ;
18
- public static readonly orgType = SfdxPropertyKeys . DEFAULT_DEV_HUB_USERNAME ;
30
+ public static readonly examples = messages . getMessage ( 'examples' ) . split ( os . EOL ) ;
19
31
public static readonly requiresDevhubUsername = true ;
20
32
public static readonly requiresProject = true ;
21
33
public static readonly flagsConfig : FlagsConfig = {
@@ -32,8 +44,48 @@ export class PackageVersionPromoteCommand extends SfdxCommand {
32
44
} ) ,
33
45
} ;
34
46
35
- public async run ( ) : Promise < unknown > {
36
- process . exitCode = 1 ;
37
- return Promise . resolve ( 'Not yet implemented' ) ;
47
+ public async run ( ) : Promise < PackageVersionPromoteResponse > {
48
+ const conn = this . hubOrg . getConnection ( ) ;
49
+ let packageId = getPackageIdFromAlias ( this . flags . package , this . project ) ?? ( this . flags . package as string ) ;
50
+
51
+ // ID can be 04t or 05i at this point
52
+ validateId ( [ BY_LABEL . SUBSCRIBER_PACKAGE_VERSION_ID , BY_LABEL . PACKAGE_VERSION_ID ] , packageId ) ;
53
+
54
+ // lookup the 05i ID, if needed
55
+ if ( ! packageId . startsWith ( '05i' ) ) {
56
+ packageId = await getPackageVersionId ( packageId , conn ) ;
57
+ }
58
+
59
+ if ( ! this . flags . noprompt ) {
60
+ // Warn when a Managed package has removed metadata
61
+ if ( await getHasMetadataRemoved ( packageId , conn ) ) {
62
+ this . ux . warn ( messages . getMessage ( 'hasMetadataRemovedWarning' ) ) ;
63
+ }
64
+
65
+ // Prompt for confirmation
66
+ if ( ! ( await this . ux . confirm ( messages . getMessage ( 'packageVersionPromoteConfirm' , [ this . flags . package ] ) ) ) ) {
67
+ return ;
68
+ }
69
+ }
70
+
71
+ const pkg = new Package ( { connection : conn } ) ;
72
+ let result : SaveResult ;
73
+
74
+ try {
75
+ result = await pkg . promote ( packageId ) ;
76
+ if ( ! result . success ) {
77
+ throw SfError . wrap ( result . errors . join ( os . EOL ) ) ;
78
+ }
79
+ } catch ( e ) {
80
+ const err = SfError . wrap ( e ) ;
81
+ if ( err . name === 'DUPLICATE_VALUE' && err . message . includes ( 'previously released' ) ) {
82
+ err . message = messages . getMessage ( 'previouslyReleasedMessage' ) ;
83
+ err . actions = [ messages . getMessage ( 'previouslyReleasedAction' ) ] ;
84
+ }
85
+ throw err ;
86
+ }
87
+ result . id = packageId ;
88
+ this . ux . log ( messages . getMessage ( 'humanSuccess' , [ result . id ] ) ) ;
89
+ return result ;
38
90
}
39
91
}
0 commit comments