This repository has been archived by the owner on Oct 30, 2018. It is now read-only.
/
utils.ts
242 lines (214 loc) · 6.81 KB
/
utils.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import * as vscode from 'vscode';
import * as fs from 'fs';
import * as xml2js from 'xml2js';
import {Config} from './config';
var stream = require('readable-stream');
var unzip = require('unzip');
var fstream = require('fstream');
var archiver = require('archiver');
const PROVIDER = 'sf://salesforce.com';
/**
* Retrieve a file name from an URI
*
* @param {vscode.Uri} uri file uri
*
* @return {string} Filename
*/
export function getFileNameFromUri(uri: vscode.Uri): string {
return uri.path.replace(/^.*[\\\/]/, '');
}
/**
* Build a Salesforce URI for the content provider
*
* @param {vscode.Uri} uri file uri
*
* @return {vscode.Uri} Salesforce URI
*/
export function buildSalesforceUriFromLocalUri(uri: vscode.Uri): vscode.Uri {
return vscode.Uri.parse(`${PROVIDER}/${getSalesforceTypeFromFileName(getFileNameFromUri(uri))}/${Config.getInstance().customNamespace}/${getFileNameFromUri(uri)}`);
}
/**
* Build a Salesforce URI for the content provider
*
* @param {vscode.Uri} uri file uri
*
* @return {vscode.Uri} Salesforce URI
*/
export function buildSalesforceUriFromQuery(query: string): vscode.Uri {
return vscode.Uri.parse(`${PROVIDER}/soqlquery?${query}`);
}
export function getNamespaceOrNull(): string {
return Config.getInstance().customNamespace == 'c' ? null : Config.getInstance().customNamespace;
}
export function getFileNameFromPath(path: string): string {
return path.replace(/^.*[\\\/]/, '');
}
export function getSalesforceTypeFromFileName(filename: string): string {
var ext = filename.split('.')[1];
switch (ext) {
case 'component':
return 'ApexComponent';
case 'page':
return 'ApexPage';
case 'trigger':
return 'ApexTrigger';
case 'cls':
return 'ApexClass';
}
}
export function getSalesforceMetadata(filename: string): Thenable<any> {
return new Promise<any>((resolve, reject) => {
fs.readFile(filename, 'UTF8', (err, data) => {
if (err) {
reject(err.message);
}
xml2js.parseString(data, (errParse, result) => {
if (errParse) {
reject(errParse.message);
}
resolve(result);
});
});
});
}
/**
* Finds the different files called "package.xml", if there is more than one asks the user to select one.
*
* @return {Promise<string>} The selected package.xml full path
*/
export function choosePackageXml(): Promise<string> {
return new Promise<string>((resolve, reject) => {
vscode.workspace.findFiles('**/package.xml', '**/node_modules/**')
.then((files: vscode.Uri[]) => {
if (files.length == 1) { // Only one package.xml found, using this one
resolve(files[0].fsPath);
} else if (files.length > 1) { // Multiple package.xml found, asking user to choose
let packages: vscode.QuickPickItem[] = [];
files.forEach(file => { // Create the quickpick options
packages.push({
label: file.fsPath.replace(vscode.workspace.rootPath, ''),
description: '',
detail: file.fsPath
});
});
packages = packages.sort((a, b) => { // Sort package.xml ordering according to their path alphabetically.
return a.detail.localeCompare(b.detail);
});
vscode.window.showQuickPick(packages).then((selected) => { // Show a selection of the existing package.xml
if (selected) {
resolve(selected.detail); // Resolve with the selected value
} else {
reject();
}
}, (reason) => {
reject(reason);
});
} else { // No package.xml found.
reject(new Error('Cannot find a package.xml'));
}
});
});
}
/**
* Wrapper in the form of a promise for the callback function to read a file
*
* @param {path: string} The path to the file to read
*
* @return {Promise<Buffer>} Resolves with a Buffer of the file read
*/
export function readFileAsync(path: string): Promise<Buffer> {
return new Promise<Buffer>((resolve, reject) => {
fs.readFile(path, 'utf-8', (err: NodeJS.ErrnoException, data: Buffer) => {
if (err) {
vscode.window.showErrorMessage(err.message);
reject(err);
} else {
resolve(data);
}
});
});
}
/**
* Parses a xml buffer to a js object wrapped in a promise
*
* @param {data: Buffer} The xml buffer
*
* @return {Promise<any>} Resolves with the js object
*/
export function xml2jsAsync(data: Buffer): Promise<any> {
return new Promise<any>((resolve, reject) => {
xml2js.parseString(data.toString(), { explicitArray: false }, (err, dom) => {
if (err) {
vscode.window.showErrorMessage(err.message);
reject(err);
} else {
resolve(dom);
}
});
});
}
export function zipFolder(path: string): Promise<Buffer> {
return new Promise<Buffer>((resolve, reject) => {
let archive = archiver('zip');
// Error during writing to the archive will throw this event
archive.on('error', function (err) {
reject(err.message);
});
// Push the selected directory to the archive
archive.directory(path, 'pkg');
archive.finalize();
// The archive is also a Buffer so we can substitute
resolve(archive);
});
}
/**
* Unzips the files in the base64 string returned by Salesforce on a retrieve request.
*
* @param {content: string} The base64 string
* @param {target: string} The path target where to unzip
*
* @return {Promise<any>} Resolves when the files are extracted or reject on error
*/
export function extractZipFromBase64String(content: string, target: string): Promise<any> {
return new Promise<any>((resolve, reject) => {
let zipstream = new stream.PassThrough();
zipstream.end(new Buffer(content, 'base64'));
zipstream.pipe(unzip.Parse())
.on('entry', (entry: any) => {
entry.path = entry.path.replace('unpackaged/', '');
entry.pipe(fstream.Writer({
type: entry.type,
path: target + entry.path
}))
.on('end', () => { resolve(true); }) // Finished
.on('error', (err: any) => { reject(err); });
});
});
}
/**
* Parse the packageXML file structure to return a javascript object
*
* @param {path: string} The path of the packageXML file
*
* @return {Promise<any>} The parsed object
*/
export function parsePackageXML(path: string): Promise<any> {
return readFileAsync(path)
.then((data: Buffer) => {
return xml2jsAsync(data);
});
}
/**
* Salesforce will not send an array of just one element in the responses. Convert to simpler to use array
*
* @param {obj: any} The param to convert to array
*
* @return {Array<any>} Array, either the original array or an array with one element
*/
export function asArray(obj: any): Array<any> {
if (obj instanceof Array) {
return obj;
} else {
return new Array(obj);
}
}