Skip to content

Commit d9ed7ec

Browse files
author
Mohith Shrivastava
committed
complete changeset functionality
1 parent 2c2335e commit d9ed7ec

File tree

6 files changed

+83
-14
lines changed

6 files changed

+83
-14
lines changed

package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@
7676
{
7777
"command": "changeset.retrieve",
7878
"title": "CCDX: Retrieve Source From Changeset"
79+
},
80+
{
81+
"command": "changeset.addorgs",
82+
"title": "CCDX: Add Orgs For Changeset Deployment"
7983
}
8084
],
8185
"menus": {
@@ -139,6 +143,12 @@
139143
"title": "CCDX: Retrieve Source From Changeset",
140144
"group": "DXCompanion",
141145
"when": "explorerResourceIsFolder && resourceFilename == changesets"
146+
},
147+
{
148+
"command": "changeset.addorgs",
149+
"title": "CCDX: Add Orgs For Changeset Deployment",
150+
"group": "DXCompanion",
151+
"when": "resourceFilename == orgs.json"
142152
}
143153
]
144154
},

src/extension.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ export function activate(context: vscode.ExtensionContext) {
3838
await changeset.retrieve();
3939
});
4040

41+
const changesetOrgAdd = vscode.commands.registerCommand('changeset.addorgs', async () => {
42+
let changeset = new Changeset();
43+
await changeset.addOrgs();
44+
});
45+
4146
const openSLDS = vscode.commands.registerCommand('open.slds', () => {
4247
NavigationService.openSLDSDocument();
4348
});
@@ -116,6 +121,7 @@ export function activate(context: vscode.ExtensionContext) {
116121
context.subscriptions.push(switchorg);
117122
context.subscriptions.push(openSalesforceOrg);
118123
context.subscriptions.push(deploysrc);
124+
context.subscriptions.push(changesetOrgAdd);
119125
}
120126
// this method is called when your extension is deactivated
121127
export function deactivate() {

src/services/changeset.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import * as fs from 'fs';
44
import * as vscode from 'vscode';
5+
import {Config} from './config';
56
import { VSCodeCore } from './vscodeCore';
67
import {VSCodeUI} from './vscodeUI';
78

@@ -13,10 +14,18 @@ export class Changeset {
1314
if(vscode.workspace.workspaceFolders){
1415
const changesetDir = vscode.workspace.workspaceFolders[0].uri.fsPath + '/changesets' ;
1516
const subDirectory = changesetDir + '/' + name.trim().toLowerCase().replace(/\s/g,'');
17+
const orgsJson = vscode.workspace.workspaceFolders[0].uri.fsPath + '/changesets/orgs.json';
1618

1719
if(!fs.existsSync(changesetDir)) {
1820
//create changesets directory
1921
fs.mkdirSync(changesetDir);
22+
const defaultorg = await Config.getDefaultUsername();
23+
const orgArray: Array<string | undefined> = [defaultorg];
24+
await fs.writeFile(orgsJson,JSON.stringify(orgArray), 'UTF-8', (err) => {
25+
if (err) {
26+
throw err;
27+
}
28+
});
2029
}
2130
if(!fs.existsSync(subDirectory)) {
2231
fs.mkdirSync(subDirectory);
@@ -29,4 +38,29 @@ export class Changeset {
2938
}
3039
}
3140
}
41+
42+
public async addOrgs() : Promise<void> {
43+
if(vscode.workspace.workspaceFolders){
44+
const orgsJson = vscode.workspace.workspaceFolders[0].uri.fsPath + '/changesets/orgs.json';
45+
await fs.readFile(orgsJson, async (err, currentorgs) => {
46+
if (err) {
47+
throw err;
48+
}
49+
if(currentorgs) {
50+
const currentOrgsArray : string[] = JSON.parse(currentorgs.toString());
51+
let allOrgsListArray = await Config.getAllOrgs();
52+
allOrgsListArray = allOrgsListArray.filter((el) => !currentOrgsArray.includes(el));
53+
const selectedOrgs = await VSCodeUI.showQuickPickMultiple(allOrgsListArray);
54+
if(selectedOrgs) {
55+
currentOrgsArray.push.apply(currentOrgsArray, selectedOrgs);
56+
await fs.writeFile(orgsJson,JSON.stringify(currentOrgsArray), 'UTF-8', (err) => {
57+
if (err) {
58+
throw err;
59+
}
60+
});
61+
}
62+
}
63+
});
64+
}
65+
}
3266
}

src/services/config.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import * as path from 'path';
66

77
export class Config {
88

9-
private static async getAllOrgs(): Promise<string[]> {
9+
public static async getAllOrgs(): Promise<string[]> {
1010
const authFiles = await sfcore.AuthInfo.listAllAuthFiles();
1111
const orgs = authFiles.map(authfile => authfile.replace('.json', ''));
1212
return orgs;
@@ -31,6 +31,14 @@ export class Config {
3131
}
3232

3333
public static async getConnection(): Promise<any> {
34+
let defaultusername = await Config.getDefaultUsername();
35+
const connection = await sfcore.Connection.create({
36+
authInfo: await sfcore.AuthInfo.create({ username: defaultusername})
37+
});
38+
return connection;
39+
}
40+
41+
public static async getDefaultUsername() {
3442
if(vscode.workspace && vscode.workspace.workspaceFolders) {
3543
const rootPath = vscode.workspace.workspaceFolders[0].uri.fsPath;
3644
const myLocalConfig = await sfcore.ConfigFile.create({
@@ -40,12 +48,7 @@ export class Config {
4048
});
4149
const localValue = myLocalConfig.get('defaultusername');
4250
let defaultusername = await sfcore.Aliases.fetch(JSON.stringify(localValue).replace(/\"/g, ''));
43-
const connection = await sfcore.Connection.create({
44-
authInfo: await sfcore.AuthInfo.create({ username: defaultusername})
45-
});
46-
return connection;
47-
} else {
48-
throw vscode.FileSystemError.FileNotFound('Project does not have workspace opened');
49-
}
51+
return defaultusername;
52+
}
5053
}
5154
}

src/services/deploy.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,13 +199,21 @@ export class DeploySource {
199199
if(vscode.workspace.workspaceFolders){
200200
let activeTerminal = VSCodeCore.setupTerminal();
201201
let deployCommand = 'sfdx force:mdapi:deploy -d ' + OSUtil.toUnixStyle(uripath) + ' -w -1';
202+
let runCommand = true;
202203
if(uripath.indexOf('changesets') > -1) {
203204
//changesets folder so ask for relevant org
204-
const orgs = fs.readdirSync(vscode.workspace.workspaceFolders[0].uri.fsPath +'/.sfdx/orgs');
205-
const username = await VSCodeUI.showQuickPick(orgs, 'Select Org for deployment(Orgs found in .sfdx/orgs are only displayed)');
206-
deployCommand = deployCommand + ' -u ' + username;
205+
try {
206+
const orgs = fs.readFileSync(vscode.workspace.workspaceFolders[0].uri.fsPath +'/changesets/orgs.json');
207+
const username = await VSCodeUI.showQuickPick(JSON.parse(orgs.toString()), 'Select Org for deployment(To display more orgs add usernames in orgs.json)');
208+
deployCommand = deployCommand + ' -u ' + username;
209+
if(!username) {
210+
runCommand = false;
211+
}
212+
} catch(error) {
213+
throw error;
214+
}
207215
}
208-
if(activeTerminal) {
216+
if(activeTerminal && runCommand) {
209217
activeTerminal.sendText(deployCommand);
210218
}
211219
}

src/services/vscodeUI.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,18 @@ export class VSCodeUI {
1515
return result;
1616
}
1717

18-
public static async showQuickPick(options: string[], placeholder?:string ){
18+
public static async showQuickPick(options: string[], placeholder?:string, canPickMany?:true ) {
1919
const result = await vscode.window.showQuickPick(options, {
2020
placeHolder: placeholder,
21-
ignoreFocusOut : true
21+
ignoreFocusOut : true,
22+
canPickMany : canPickMany
23+
});
24+
return result;
25+
}
26+
27+
public static async showQuickPickMultiple(options: string[] ) {
28+
const result = await vscode.window.showQuickPick(options, {
29+
canPickMany : true
2230
});
2331
return result;
2432
}

0 commit comments

Comments
 (0)