This repository has been archived by the owner on Nov 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
pkgsource.ts
93 lines (78 loc) · 3.25 KB
/
pkgsource.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import { core, flags, SfdxCommand } from '@salesforce/command';
import { AnyJson } from '@salesforce/ts-types';
import * as AdmZip from 'adm-zip';
import * as chalk from 'chalk';
import * as child from 'child_process';
import fs = require('fs-extra');
import * as util from 'util';
// Initialize Messages with the current plugin directory
core.Messages.importMessagesDirectory(__dirname);
// Load the specific messages for this file. Messages from @salesforce/command, @salesforce/core,
// or any library that is using the messages framework can also be loaded this way.
const messages = core.Messages.loadMessages('mo-dx-plugin', 'org');
const exec = util.promisify(child.exec);
export default class Pkgsource extends SfdxCommand {
public static description = messages.getMessage('retrieveSource');
public static examples = [
'$ sfdx retrieve:pkgsource -n <package/changeset>',
'$ sfdx retrieve:pkgsource -n <package/changeset> -r <relative path where source is retrieved and unzipped>',
'$ sfdx retrieve:pkgsource -n <package/changeset> -r /changesets/src'
];
protected static flagsConfig = {
// flag with a value (-n, --name=VALUE)
packagename: flags.string({
required: true,
char: 'n',
description: 'the name of the package you want to retrieve'
}),
retrievedir : flags.string({
required: false,
char: 'r',
description: 'directory path to retrieve'
})
};
// Comment this out if your command does not require an org username
protected static requiresUsername = true;
// Set this to true if your command requires a project workspace; 'requiresProject' is false by default
protected static requiresProject = true;
public async run(): Promise<AnyJson> {
const defaultusername = this.flags.targetusername
? this.flags.targetusername
: this.org.getUsername();
const tmpDir = this.flags.retrievedir ? this.flags.retrievedir : 'src' ;
let errored = false;
this.ux.startSpinner(chalk.yellowBright('Retrieving Metadata...'));
const retrieveCommand = `sfdx force:mdapi:retrieve -s -p "${
this.flags.packagename
}" -u ${defaultusername} -r ./${tmpDir} -w -1 --json`;
try {
await exec(retrieveCommand, { maxBuffer: 1000000 * 1024 });
} catch (exception) {
errored = true;
this.ux.errorJson(exception);
this.ux.stopSpinner(chalk.redBright('Retrieve Operation Failed ✖'));
}
if (!errored) {
this.ux.stopSpinner(
chalk.greenBright('Retrieve Completed ✔. Unzipping...')
);
// unzip result to a temp folder mdapi
if (process.platform.includes('darwin')) {
await exec(`unzip -qqo ./${tmpDir}/unpackaged.zip -d ./${tmpDir}`); // Use standard MACOSX unzip
} else {
// use a third party library to unzip the zipped resource
try {
const zip = new AdmZip('./' + tmpDir + '/unpackaged.zip');
await zip.extractAllTo('./' + tmpDir, true);
} catch (error) {
console.error(chalk.redBright(error + '✖'));
return;
}
}
this.ux.startSpinner(chalk.yellowBright('Unzip Completed ✔'));
await fs.unlink('./' + tmpDir + '/unpackaged.zip');
this.ux.stopSpinner(chalk.greenBright('Finished ✔'));
}
return '';
}
}