|
4 | 4 | * Licensed under the BSD 3-Clause license. |
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 | +import * as os from 'os'; |
| 8 | +import { flags, FlagsConfig } from '@salesforce/command'; |
| 9 | +import { Lifecycle, Messages, SfdxError } from '@salesforce/core'; |
| 10 | +import { SourceRetrieveResult } from '@salesforce/source-deploy-retrieve'; |
| 11 | +import { Duration } from '@salesforce/kit'; |
| 12 | +import { DEFAULT_SRC_WAIT_MINUTES, MINIMUM_SRC_WAIT_MINUTES, SourceCommand } from '../../../sourceCommand'; |
| 13 | + |
| 14 | +Messages.importMessagesDirectory(__dirname); |
| 15 | +const messages = Messages.loadMessages('@salesforce/plugin-source', 'retrieve'); |
| 16 | + |
| 17 | +export class retrieve extends SourceCommand { |
| 18 | + public static readonly description = messages.getMessage('description'); |
| 19 | + public static readonly examples = messages.getMessage('examples').split(os.EOL); |
| 20 | + public static readonly requiresProject = true; |
| 21 | + public static readonly requiresUsername = true; |
| 22 | + public static readonly flagsConfig: FlagsConfig = { |
| 23 | + sourcepath: flags.array({ |
| 24 | + char: 'p', |
| 25 | + description: messages.getMessage('flags.sourcePath'), |
| 26 | + exclusive: ['manifest', 'metadata'], |
| 27 | + }), |
| 28 | + wait: flags.minutes({ |
| 29 | + char: 'w', |
| 30 | + default: Duration.minutes(DEFAULT_SRC_WAIT_MINUTES), |
| 31 | + min: MINIMUM_SRC_WAIT_MINUTES, |
| 32 | + description: messages.getMessage('flags.wait'), |
| 33 | + }), |
| 34 | + manifest: flags.filepath({ |
| 35 | + char: 'x', |
| 36 | + description: messages.getMessage('flags.manifest'), |
| 37 | + exclusive: ['metadata', 'sourcepath'], |
| 38 | + }), |
| 39 | + metadata: flags.array({ |
| 40 | + char: 'm', |
| 41 | + description: messages.getMessage('flags.metadata'), |
| 42 | + exclusive: ['manifest', 'sourcepath'], |
| 43 | + }), |
| 44 | + packagenames: flags.array({ |
| 45 | + char: 'n', |
| 46 | + description: messages.getMessage('flags.packagename'), |
| 47 | + }), |
| 48 | + }; |
| 49 | + protected readonly lifecycleEventNames = ['preretrieve', 'postretrieve']; |
| 50 | + |
| 51 | + public async run(): Promise<SourceRetrieveResult> { |
| 52 | + const hookEmitter = Lifecycle.getInstance(); |
| 53 | + const packages = await this.retrievePackageDirs(); |
| 54 | + const defaultPackage = packages.find((pkg) => pkg.default); |
| 55 | + |
| 56 | + const cs = await this.createComponentSet({ |
| 57 | + // safe to cast from the flags as an array of strings |
| 58 | + packagenames: this.flags.packagenames as string[], |
| 59 | + sourcepath: this.flags.sourcepath as string[], |
| 60 | + manifest: this.flags.manifest as string, |
| 61 | + metadata: this.flags.metadata as string[], |
| 62 | + }); |
| 63 | + |
| 64 | + // emit pre retrieve event |
| 65 | + // needs to be a path to the temp dir package.xml |
| 66 | + await hookEmitter.emit('preretrieve', { packageXmlPath: cs.getPackageXml() }); |
| 67 | + |
| 68 | + const results = await cs.retrieve(this.org.getUsername(), this.getAbsolutePath(defaultPackage.path), { |
| 69 | + merge: true, |
| 70 | + // TODO: fix this once wait has been updated in library |
| 71 | + wait: 1000000, |
| 72 | + }); |
| 73 | + |
| 74 | + // emit post retrieve event |
| 75 | + // results must match = { |
| 76 | + // "done": true, |
| 77 | + // "fileProperties": [ |
| 78 | + // { |
| 79 | + // "createdById": "0053B000005FbiuQAC", |
| 80 | + // "createdByName": "User User", |
| 81 | + // "createdDate": "2021-02-09T23:48:26.000Z", |
| 82 | + // "fileName": "unpackaged/classes/MyTest.cls", |
| 83 | + // "fullName": "MyTest", |
| 84 | + // "id": "01p3B000008hOVcQAM", |
| 85 | + // "lastModifiedById": "0053B000005FbiuQAC", |
| 86 | + // "lastModifiedByName": "User User", |
| 87 | + // "lastModifiedDate": "2021-02-11T23:00:49.000Z", |
| 88 | + // "manageableState": "unmanaged", |
| 89 | + // "type": "ApexClass" |
| 90 | + // }, |
| 91 | + // { |
| 92 | + // "createdById": "0053B000005FbiuQAC", |
| 93 | + // "createdByName": "User User", |
| 94 | + // "createdDate": "2021-02-09T23:48:27.000Z", |
| 95 | + // "fileName": "unpackaged/classes/force.cls", |
| 96 | + // "fullName": "force", |
| 97 | + // "id": "01p3B000008hOVdQAM", |
| 98 | + // "lastModifiedById": "0053B000005FbiuQAC", |
| 99 | + // "lastModifiedByName": "User User", |
| 100 | + // "lastModifiedDate": "2021-02-11T23:00:49.000Z", |
| 101 | + // "manageableState": "unmanaged", |
| 102 | + // "type": "ApexClass" |
| 103 | + // }, |
| 104 | + // { |
| 105 | + // "createdById": "0053B000005FbiuQAC", |
| 106 | + // "createdByName": "User User", |
| 107 | + // "createdDate": "2021-02-12T17:27:58.876Z", |
| 108 | + // "fileName": "unpackaged/package.xml", |
| 109 | + // "fullName": "unpackaged/package.xml", |
| 110 | + // "id": "", |
| 111 | + // "lastModifiedById": "0053B000005FbiuQAC", |
| 112 | + // "lastModifiedByName": "User User", |
| 113 | + // "lastModifiedDate": "2021-02-12T17:27:58.876Z", |
| 114 | + // "manageableState": "unmanaged", |
| 115 | + // "type": "Package" |
| 116 | + // } |
| 117 | + // ], |
| 118 | + // "id": "09S3B000002N5lcUAC", |
| 119 | + // "status": "Succeeded", |
| 120 | + // "success": true, |
| 121 | + // "zipFilePath": "/var/folders/28/dmr8rt4d5f5bq_ttscbspz580000gp/T/sdx_sourceRetrieve_pkg_1613150491146/unpackaged.zip" |
| 122 | + // } |
| 123 | + await hookEmitter.emit('postretrieve', results); |
| 124 | + |
| 125 | + if (results.status === 'InProgress') { |
| 126 | + throw new SfdxError(messages.getMessage('retrieveTimeout', [(this.flags.wait as Duration).minutes])); |
| 127 | + } |
| 128 | + this.printTable(results, true); |
| 129 | + |
| 130 | + return results; |
| 131 | + } |
| 132 | +} |
0 commit comments