-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
112 lines (106 loc) · 4.54 KB
/
gulpfile.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
var gulp = require('gulp');
var forceDeploy = require('gulp-jsforce-deploy');
var zip = require('gulp-zip');
var builder = require('xmlbuilder');
var fs = require('fs');
var path = require('path');
//Base folder name if there is one. If not use './' instead;
const baseFolder = './src/';
const ignoreContent = fs.readFileSync('./.gitignore', 'utf8');
//Task for generating package.xml
gulp.task('buildPackageXml', function(cb){
var packageObj = {};
packageObj.Package = {
'@xmlns': 'http://soap.sforce.com/2006/04/metadata',
types: [ generateXmlSection(baseFolder+'classes/','.cls','ApexClass'),
generateXmlSection(baseFolder+'components/','.cmp','ApexComponent'),
generateXmlSection(baseFolder+'pages/','.page','ApexPage'),
generateXmlSection(baseFolder+'flexipages/','.flexipage','FlexiPage'),
generateXmlSection(baseFolder+'triggers/','.trigger','ApexTrigger'),
generateXmlSection(baseFolder+'staticresources/','.resource','StaticResource'),
generateXmlSection(baseFolder+'objects/','.object','CustomObject'),
generateXmlSection(baseFolder+'layouts/','.layout','Layout'),
generateXmlSection(baseFolder+'tabs/','.tab','CustomTab'),
generateXmlSection(baseFolder+'flows/','.flow','Flow'),
generateXmlSection(baseFolder+'applications/','.app','CustomApplication'),
generateAuraSection(baseFolder+'aura/','AuraDefinitionBundle'),
generateCustomMetadataSection(baseFolder+'customMetadata/','CustomMetadata')
],
version:'36.0'
}
//Removing empty type sections from package.xml
packageObj.Package.types = packageObj.Package.types.filter(function(val){
return val;
});
var root = builder.create(packageObj);
fs.writeFileSync(baseFolder+'package.xml', '<?xml version="1.0" encoding="UTF-8"?>'+root.toString());
return gulp.src(baseFolder+'package.xml');
});
//Function for generating particular section of package.xml
function generateXmlSection(basePath, extension, typeVal){
if (!fs.existsSync(basePath)) {
return '';
}
var typeObj = {};
//Removing extensions
var memberArray = fs.readdirSync(basePath).map(function(val){
//Checking if the file is ignored
if(ignoreContent.indexOf((basePath+val).substring(1)) === -1){
if(val.match(extension+"$") == extension){
val = val.substring(0, val.length - extension.length);
return val;
}else{
return null;
}
}else{
return null;
}
});
//Removing empty elements;
memberArray = memberArray.filter(function(val){
return val;
});
typeObj.members = memberArray;
typeObj.name = typeVal;
return typeObj;
}
//Function for generating aura section of package.xml
function generateAuraSection(basePath,typeVal){
if (!fs.existsSync(basePath)) {
return '';
}
var typeObj = {};
typeObj.members = fs.readdirSync(basePath).filter(function(file) {
return fs.statSync(path.join(basePath, file)).isDirectory();
});
typeObj.name = typeVal;
return typeObj;
}
//Function for generating custom metadata section of package.xml
function generateCustomMetadataSection(basePath, typeVal) {
if (!fs.existsSync(basePath)) {
return '';
}
var typeObj = {};
typeObj.members = '*';
typeObj.name = typeVal;
return typeObj;
}
//Task to deploy the changes to Salesforce
gulp.task('deployToSF',['buildPackageXml'], function (cb) {
var gulpFolder = baseFolder;
//removing ./ from start of the path and / form end for gulp
gulpFolder = gulpFolder.replace(/^\.\//,'');
gulpFolder = gulpFolder.replace(/\/$/,'');
return gulp.src(gulpFolder+'*/**/*')
.pipe(zip('package.zip'))
.pipe(gulp.dest('./'))
.pipe(forceDeploy({
username: process.env.SF_USERNAME,
password: process.env.SF_PASSWORD,
loginUrl: 'https://login.salesforce.com'
, pollTimeout: 10*60*1000
, version: '36.0'
//, pollInterval: 10*1000
}));
});