1 | var fs = require('fs');
|
2 | var path = require('path');
|
3 | var through = require('through2');
|
4 | var request = require("superagent");
|
5 | var cheerio = require("cheerio");
|
6 | var gutil = require('gulp-util');
|
7 | var PluginError = gutil.PluginError;
|
8 | var Q = require("q");
|
9 | var jsforce = require("jsforce");
|
10 | var open = require('open');
|
11 |
|
12 |
|
13 | function gulpVisualforceHtml(opts){
|
14 |
|
15 | return through.obj(function(file, enc, callback){
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | if (file.isNull() || file.isDirectory()) {
|
21 | this.push(file);
|
22 | return callback();
|
23 | }
|
24 |
|
25 |
|
26 |
|
27 | if (['.html'].indexOf(path.extname(file.path)) === -1) {
|
28 | return callback();
|
29 | }
|
30 |
|
31 |
|
32 | if (file.isStream()) {
|
33 | throw new PluginError({
|
34 | plugin: 'R2-VFTRANSFORM',
|
35 | message: 'Streams are not supported.'
|
36 | });
|
37 | return callback();
|
38 | }
|
39 |
|
40 | if (file.isBuffer()) {
|
41 |
|
42 | transform.call(this, file, opts);
|
43 | login( process.env.SF_USERNAME,process.env.SF_PASSWORD + process.env.SF_TOKEN, process.env.SF_HOST )
|
44 | .then( function(){
|
45 | upload( file, opts, callback);
|
46 | }).fail( callback )
|
47 | }
|
48 |
|
49 | });
|
50 | }
|
51 |
|
52 | function transform(file, opts){
|
53 |
|
54 | var clayprod = '<script>';
|
55 | clayprod += 'window._sf = { staticResource: "{!URLFOR($Resource.'+ opts.name +')}" };';
|
56 | clayprod += '\n window._sf.staticResource = window._sf.staticResource.split("?")[0]';
|
57 | clayprod +='</script></head>';
|
58 |
|
59 | var claylocal = '<script>';
|
60 | claylocal += 'window._sf = { staticResource: "'+ opts.host +'" };';
|
61 | claylocal += '\n window._sf.staticResource = window._sf.staticResource.split("?")[0]';
|
62 | claylocal +='</script></head>\n';
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 | var cheerio = require('cheerio'),
|
71 | $ = cheerio.load( file.contents.toString(), { xmlMode: true });
|
72 |
|
73 | if( opts.host ) $("head").append( claylocal );
|
74 | else $("head").append( clayprod );
|
75 |
|
76 | $("link").each(function(i, elem) {
|
77 | var el = $(this)
|
78 | var url = el.attr("href");
|
79 |
|
80 | if( opts.host ){
|
81 | url = url.replace("{3vot}", opts.host);
|
82 | el.attr("href", url);
|
83 | }
|
84 | else{
|
85 | url = url.replace("{3vot}/", "");
|
86 | var transformed = "{!URLFOR($Resource." + opts.name + ", '" +url +"')}"
|
87 | el.attr("href", transformed);
|
88 | }
|
89 |
|
90 | });
|
91 |
|
92 | $("script").each(function(i, elem) {
|
93 | var el = $(this)
|
94 | var url = el.attr("src");
|
95 |
|
96 | if(url){
|
97 | if( opts.host ){
|
98 | url = url.replace("{3vot}", opts.host);
|
99 | el.attr("src", url);
|
100 | }
|
101 | else{
|
102 | url = url.replace("{3vot}/", "");
|
103 | if(url && url.indexOf("http") !=0 ){
|
104 | var transformed = "{!URLFOR($Resource." + opts.name + ", '" +url +"')}"
|
105 | el.attr("src", transformed);
|
106 | }
|
107 | el.html(";");
|
108 | }
|
109 | }
|
110 |
|
111 | });
|
112 | this.push(file);
|
113 | var page = $.html();
|
114 | page = page.replace(/'/g,'"');
|
115 | file.contents = new Buffer( page );
|
116 | }
|
117 |
|
118 |
|
119 | function upload( file , opts, cb ){
|
120 | var url = process.env.INSTANCE_URL + "/services/data/v30.0/sobjects/ApexPage/Name/" + opts.name;
|
121 |
|
122 | gutil.log( 'Starting', gutil.colors.cyan('Visualforce Page Upload'));
|
123 |
|
124 | body = {
|
125 | Markup : file.contents.toString(),
|
126 | ControllerType : 3,
|
127 | MasterLabel: opts.name,
|
128 | ApiVersion: "30.0"
|
129 | }
|
130 |
|
131 | var req = request.patch( url )
|
132 | .type( "application/json" )
|
133 | .set( 'Authorization', 'Bearer ' + process.env.ACCESS_TOKEN )
|
134 | .send( body )
|
135 | .end( function( err, res ){
|
136 | if( err ) return cb( err );
|
137 | if( res.body[0] && res.body[0].errorCode ) return cb( new Error("Salesforce.com rejected the upsert of a Visualforce Page with the HTML we send, it's posibly due to unvalid HTML. Please review your template files. ERROR: " + res.body[0].message ) );
|
138 | if( res.body.success == false || res.body.errorCode ) cb( new Error( "ERROR: " + JSON.stringify( res.body ) ) );
|
139 | gutil.log( 'Finished', gutil.colors.cyan('Visualforce Page Upload'));
|
140 | if(opts.open) open( process.env.INSTANCE_URL + "/apex/" + process.env.NAME )
|
141 | cb();
|
142 | })
|
143 | };
|
144 |
|
145 | function login( username, password, host ){
|
146 | var deferred = Q.defer();
|
147 |
|
148 | if ( process.env.INSTANCE_URL ) process.nextTick( function(){ deferred.resolve() } );
|
149 | else
|
150 | gutil.log( "Starting", gutil.colors.cyan('Login to Salesforce'));
|
151 | var username = username;
|
152 | var password = password;
|
153 | var host = host || "login.salesforce.com"
|
154 |
|
155 | var conn = new jsforce.Connection({
|
156 | loginUrl : 'https://' + host
|
157 | });
|
158 |
|
159 | conn.login( username, password, function( err, userinfo ){
|
160 | if(err) deferred.reject(err)
|
161 | else{
|
162 | process.env.INSTANCE_URL = conn.instanceUrl;
|
163 | process.env.ACCESS_TOKEN = conn.accessToken;
|
164 | deferred.resolve();
|
165 | }
|
166 | });
|
167 | return deferred.promise;
|
168 | }
|
169 |
|
170 | module.exports = gulpVisualforceHtml; |
\ | No newline at end of file |