1 | 'use strict';
|
2 |
|
3 | var chalk = require('chalk');
|
4 | var JSZip = require('jszip');
|
5 |
|
6 | var fs = require('fs');
|
7 | var path = require('path');
|
8 | var through = require('through2');
|
9 | var gutil = require('gulp-util');
|
10 | var PluginError = gutil.PluginError;
|
11 | var Q = require("q");
|
12 |
|
13 |
|
14 | var jsforce = require("jsforce")
|
15 |
|
16 |
|
17 | function StaticResource( opts) {
|
18 |
|
19 | opts = opts || {};
|
20 | opts.compress = true;
|
21 |
|
22 | var firstFile;
|
23 | var zip = new JSZip();
|
24 |
|
25 | return through.obj(function (file, enc, cb) {
|
26 | if (file.isNull()) {
|
27 | cb();
|
28 | return;
|
29 | }
|
30 |
|
31 | if (file.isStream()) {
|
32 | cb(new gutil.PluginError('gulp-salesforce', 'Streaming not supported'));
|
33 | return;
|
34 | }
|
35 |
|
36 | if (!firstFile) {
|
37 | firstFile = file;
|
38 | }
|
39 |
|
40 |
|
41 | var pathname = file.relative.replace(/\\/g, '/');
|
42 |
|
43 | zip.file(pathname, file.contents, {
|
44 | date: file.stat ? file.stat.mtime : new Date(),
|
45 | createFolders: true
|
46 | });
|
47 |
|
48 | cb();
|
49 | }, function (cb) {
|
50 | if (!firstFile) {
|
51 | cb();
|
52 | return;
|
53 | }
|
54 |
|
55 |
|
56 | var zipFile = new gutil.File({
|
57 | cwd: firstFile.cwd,
|
58 | base: firstFile.base,
|
59 | contents: zip.generate({
|
60 | type: 'nodebuffer',
|
61 | compression: opts.compress ? 'DEFLATE' : 'STORE',
|
62 | comment: opts.comment
|
63 | })
|
64 | });
|
65 |
|
66 |
|
67 | upload( zipFile, opts, cb )
|
68 | });
|
69 | };
|
70 |
|
71 | function upload( file , opts, cb ){
|
72 | var zip64 = file.contents.toString('base64');
|
73 | var name = opts.name
|
74 |
|
75 | if( !opts.namespace ) opts.namespace = ""
|
76 |
|
77 | var fullNames = [{
|
78 | fullName: opts.namespace + opts.name,
|
79 | content: zip64,
|
80 | contentType: "application/zip",
|
81 | cacheControl: "Public" ,
|
82 | }];
|
83 |
|
84 | login( process.env.SF_USERNAME,process.env.SF_PASSWORD + process.env.SF_TOKEN , process.env.SF_HOST )
|
85 | .then( function(){
|
86 | var conn = new jsforce.Connection({
|
87 | accessToken: process.env.ACCESS_TOKEN,
|
88 | instanceUrl: process.env.INSTANCE_URL
|
89 | });
|
90 |
|
91 | gutil.log('Starting', gutil.colors.cyan( 'Static Resource Upload' ));
|
92 | conn.metadata.upsert( 'StaticResource', fullNames, function( err, results ) {
|
93 | console.log(err)
|
94 | console.log(results)
|
95 | if( err ) cb( err );
|
96 | gutil.log("Finished", gutil.colors.cyan( 'Static Resource Upload' ));
|
97 | return cb();
|
98 | });
|
99 | }).fail( cb )
|
100 | }
|
101 |
|
102 | function login( username, password, host ){
|
103 | var deferred = Q.defer();
|
104 |
|
105 |
|
106 |
|
107 | if ( process.env.INSTANCE_URL ) process.nextTick( function(){ deferred.resolve() } );
|
108 | else
|
109 |
|
110 |
|
111 | gutil.log( gutil.colors.cyan( 'Login to Salesforce' ));
|
112 |
|
113 | var username = username;
|
114 | var password = password;
|
115 | var host = host || "login.salesforce.com"
|
116 |
|
117 | var conn = new jsforce.Connection({
|
118 | loginUrl : 'https://' + host
|
119 | });
|
120 |
|
121 | conn.login( username, password, function( err, userinfo ){
|
122 | if(err) deferred.reject(err)
|
123 | else{
|
124 | process.env.INSTANCE_URL = conn.instanceUrl;
|
125 | process.env.ACCESS_TOKEN = conn.accessToken;
|
126 | deferred.resolve();
|
127 | }
|
128 | });
|
129 | return deferred.promise;
|
130 | }
|
131 |
|
132 | module.exports = StaticResource; |
\ | No newline at end of file |