-
Notifications
You must be signed in to change notification settings - Fork 22
/
index.js
176 lines (151 loc) · 5.6 KB
/
index.js
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
'use strict';
const jsforce = require('jsforce');
const Downloader = require('./lib/downloader.js');
const ExcelBuilder = require('./lib/excelbuilder.js');
const Utils = require('./lib/utils.js');
module.exports = (config, logger) => {
// Check all mandatory config options
if (typeof config.username === 'undefined' || config.username === null ||
typeof config.password === 'undefined' || config.password === null) {
throw new Error('Not enough config options');
}
// Set default values
if (typeof config.loginUrl === 'undefined' || config.loginUrl === null) {
config.loginUrl = 'https://login.salesforce.com';
}
if (typeof config.apiVersion === 'undefined' || config.apiVersion === null) {
config.apiVersion = '48.0';
}
if (typeof config.output === 'undefined' || config.output === null) {
config.output = '.';
}
if (typeof config.debug === 'undefined' || config.debug === null) {
config.debug = false;
}
config.debug = (config.debug === "true" || config.debug === true);
if (typeof config.excludeManagedPackage === 'undefined' || config.excludeManagedPackage === null) {
config.excludeManagedPackage = true;
}
config.excludeManagedPackage = (config.excludeManagedPackage === "true" || config.excludeManagedPackage === true);
if (typeof config.projectName === 'undefined' || config.projectName === null) {
config.projectName = 'PROJECT';
}
if (typeof config.outputTime === 'undefined' || config.outputTime === null) {
config.outputTime = false;
}
if (typeof config.allCustomObjects === 'undefined' || config.allCustomObjects === null) {
config.allCustomObjects = true;
}
config.allCustomObjects = (config.allCustomObjects === "true" || config.allCustomObjects === true);
if (typeof config.lucidchart === 'undefined' || config.lucidchart === null) {
config.lucidchart = true;
}
config.lucidchart = (config.lucidchart === "true" || config.lucidchart === true);
if (typeof config.sobjects === 'undefined' || config.sobjects === null) {
config.objects = [
'Account',
'Contact',
'User'
];
} else {
// If an array is passed to the module
if (Array.isArray(config.sobjects)) {
config.objects = config.sobjects;
} else {
// Check and parse standObjects string for command-line
try {
config.objects = config.sobjects.split(',');
} catch (e) {
let errorMessage = 'Unable to parse sobjects parameter';
if (config.debug)
errorMessage += ' : ' + e;
throw new Error(errorMessage);
}
}
}
if (typeof config.techFieldPrefix === 'undefined' || config.techFieldPrefix === null) {
config.techFieldPrefix = 'TECH_';
}
if (typeof config.hideTechFields === 'undefined' || config.hideTechFields === null) {
config.hideTechFields = false;
}
if (typeof config.columns === 'undefined' || config.columns === null) {
config.columns = {
'ReadOnly': 5,
'Mandatory': 3,
'Name': 25,
'Description': 90,
'Helptext': 90,
'APIName': 25,
'Type': 27,
'Values': 45
};
}
var utils = new Utils();
// Clean folders that contain API files
if (config.cleanFolders) {
const statusRmDescribe = utils.rmDir(__dirname + '/files/describe', '.json', false);
const statusRmMetadata = utils.rmDir(__dirname + '/files/metadata', '.json', false);
logger('File folders cleaned');
}
// Main promise
const promise = new Promise((resolve, reject) => {
const conn = new jsforce.Connection({
loginUrl: config.loginUrl,
version: config.apiVersion
});
// Salesforce connection
conn.login(config.username, config.password).then(result => {
logger('Connected as ' + config.username);
if (config.debug) {
utils.log('Connected as ' + config.username, config);
}
if (config.allCustomObjects) {
conn.describeGlobal().then(res => {
for (let i = 0; i < res.sobjects.length; i++) {
let object = res.sobjects[i];
if (config.objects === undefined)
config.objects = [];
// If the sObject is a real custom object
if (object.custom && (object.name.indexOf('__c') !== -1)) {
if (config.debug)
utils.log('# excludeManagedPackage (' + config.excludeManagedPackage + '): ' + object.name, config);
if (config.excludeManagedPackage) {
if ((object.name.split('__').length - 1 < 2))
config.objects.push(object.name);
} else {
config.objects.push(object.name);
}
}
}
if (config.debug)
utils.log(JSON.stringify(config.objects), config);
const downloader = new Downloader(config, logger, conn);
const builder = new ExcelBuilder(config, logger);
// Download metadata files
downloader.execute().then(result => {
logger(result + ' downloaded');
// Generate the excel file
builder.generate().then(result => {
resolve();
});
})
});
} else {
if (config.objects.length > 0) {
const downloader = new Downloader(config, logger, conn);
const builder = new ExcelBuilder(config, logger);
// Download metadata files
downloader.execute().then(result => {
logger(result + ' downloaded');
// Generate the excel file
return builder.generate();
}).then(result => {
resolve();
});
}
}
}).catch(reject);
});
return promise;
};