forked from apigee/openbank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
343 lines (271 loc) · 11.6 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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
Copyright 2017 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
var gulp = require('gulp');
var cucumber = require('gulp-cucumber');
var eslint = require('gulp-eslint');
var prompt_lib = require('prompt');
var gulpSequence = require('gulp-sequence')
var mustache = require('mustache');
var path = require('path');
var fs = require('fs-extra');
var request = require('request');
const Datastore = require('@google-cloud/datastore');
gulp.task('lint', function() {
return gulp.src(['./src/**/*.js', '!**/node_modules/**', '!**/target/**','!**/public/js/**', '!**/devportal/**', '!**/jsrsasign-all-min.js', '!**/ValidateRequest.js'])
.pipe(eslint({
fix: true
}))
.pipe(eslint.format())
.pipe(eslint.failAfterError());
});
gulp.task('startdeploy', function( cb) {
var switch_opdk = [];
switch_opdk.push({name: 'hasdatastore', description: 'Do you have cloud datastore instance? Enter - true/false' ,type: 'boolean', required: true});
prompt_lib.get(switch_opdk, function(err, results) {
if(results['hasdatastore']){
var required_values = [];
required_values.push({name: 'datastoreProject', description: 'Enter the cloud datastore project name', type: 'string', required: true});
required_values.push({name: 'serviceaccount_Private_Key', description: 'Enter the service account private key', type: 'string', required: true});
required_values.push({name: 'token_uri', description: 'Enter the token uri of the service account', type: 'string', required: true});
required_values.push({name: 'client_email', description: 'Enter the client email of the service account', type: 'string', required: true});
required_values.push({name: 'bankPrivateKey', description: 'Enter the complete path of banks private key', type: 'string', required: true});
required_values.push({name: 'bankPublicKey', description: 'Enter the complete path of banks public key', type: 'string', required: true});
required_values.push({name: 'tppPrivateKey', description: 'Enter the complete path of tpp private key', type: 'string', required: true});
required_values.push({name: 'tppPublicKey', description: 'Enter the complete path of tpp public key', type: 'string', required: true});
prompt_lib.start();
prompt_lib.get(required_values, function(err, results) {
UploadDataDatastore(results, function(){
results.hasdatastore = true;
post_prompt(err, results, function( callback)
{
require('edge-launchpad')(gulp);
cb();
});
});
});
} else {
var required_values = [];
required_values.push({name: 'bankPrivateKey', description: 'Enter the complete path of banks private key', type: 'string', required: true});
required_values.push({name: 'bankPublicKey', description: 'Enter the complete path of banks public key', type: 'string', required: true});
required_values.push({name: 'tppPrivateKey', description: 'Enter the complete path of tpp private key', type: 'string', required: true});
required_values.push({name: 'tppPublicKey', description: 'Enter the complete path of tpp public key', type: 'string', required: true});
prompt_lib.start();
prompt_lib.get(required_values, function(err, results) {
results.hasdatastore = false;
results["datastoreProject"] = "projectIdHere";
results["serviceaccount_Private_Key"] = "privateKey";
results["token_uri"] = "http://tokenUriHere";
results["client_email"] = "clientmailhere";
post_prompt(err, results, function( callback)
{
require('edge-launchpad')(gulp);
cb();
});
});
}
});
});
gulp.task('test', function() {
return gulp.src('test/features/*')
.pipe(cucumber({
'steps': ['test/features/step_definitions/env.js','test/features/step_definitions/apickli-gherkin.js','test/features/step_definitions/auth+consent.js','test/features/step_definitions/accounts.js','test/features/step_definitions/payments.js'],
'support': 'test/features/support/*.js',
'format': 'pretty'
}));
});
gulp.task('openbankdeploy', function (cb) {
gulpSequence('startdeploy','deploy', cb);
})
function post_prompt(err, results, cb) {
var inject_object = {};
var files_list = ['config.yml.template','test/testbank_jwt.pem.template','test/testbank_jwt_pub.pem.template','test/testtpp_jwt.pem.template','test/testtpp_jwt_pub.pem.template'];
var paths = [];
for (var j=0; j<files_list.length; j++) {
paths.push(path.join(__dirname, files_list[j]))
}
if(results["bankPrivateKey"])
{
var bankPrivateKey = fs.readFileSync(results.bankPrivateKey, 'utf8');
results["bankPrivateKey"] = bankPrivateKey;
results["bankPrivateKeySingleLine"] = bankPrivateKey.split("\n").join("");
}
if(results["bankPublicKey"])
{
var bankPublicKey = fs.readFileSync(results.bankPublicKey, 'utf8');
results["bankPublicKey"] = bankPublicKey;
results["bankPublicKeySingleLine"] = bankPublicKey.split("\n").join("");
}
if(results["tppPrivateKey"])
{
var tppPrivateKey = fs.readFileSync(results.tppPrivateKey, 'utf8');
results["tppPrivateKey"] = tppPrivateKey;
results["tppPrivateKeySingleLine"] = tppPrivateKey.split("\n").join("");
}
if(results["tppPublicKey"])
{
var tppPublicKey = fs.readFileSync(results.tppPublicKey, 'utf8');
results["tppPublicKey"] = tppPublicKey;
results["tppPublicKeySingleLine"] = tppPublicKey.split("\n").join("");
}
var keys = Object.keys(results)
for(var i=0; i<keys.length; i++){
inject_object[keys[i]] = results[keys[i]]
}
if(!results.hasdatastore){
inject_object.sbconnector = "/connector/v1.0.1/apisbank";
}
replace_variables(paths, inject_object, function(callback)
{
cb(err,results);
});
}
function replace_variables(paths, inject_object, cb) {
mustache.escape = function (value) {
return value;
};
for(var i=0; i<paths.length; i++){
var path_to_template = paths[i]
var data
try {
data = fs.readFileSync(path_to_template, 'utf8')
} catch(e){
console.log(e);
cb();
}
var mu_template = String(data)
try {
var output = mustache.render(mu_template, inject_object)
} catch(e) {
console.log(e)
cb();
}
try {
fs.outputFileSync(path_to_template.split('.').slice(0,2).join('.'), output)
} catch (e){
console.log(e)
cb();
}
output = '< yet to copy from original template >'
}
cb();
}
function populateDatastore(datastore,data,kind,cb)
{
//var requestPayload = apigee.getVariable(req, 'request.content');
var requestPayload = data;
var tasks = JSON.parse(requestPayload);
var kind = kind;
var entities = [];
var uuid = "";
for(var i=0; i< tasks.length; i++)
{
var obj = {};
tasks[i].uuid = generateUUID();
obj.key = datastore.key([kind,tasks[i].uuid]);
obj.data = tasks[i];
entities.push(obj);
}
datastore.upsert(entities).then(() => {
// entities inserted successfully.
cb();
});
}
function UploadDataDatastore(results,cb)
{
var keyFilename = "service.json";
var keyFilePath = path.join(__dirname, keyFilename);
var keyData = fs.readFileSync(keyFilePath, 'utf8');
var keyDataObj = JSON.parse(keyData);
keyDataObj.project_id = results["datastoreProject"];
keyDataObj.private_key = results["serviceaccount_Private_Key"];
keyDataObj.token_uri = results["token_uri"];
keyDataObj.client_email = results["client_email"];
fs.outputFileSync(keyFilePath, JSON.stringify(keyDataObj).split("\\\\n").join("\\n"));
var datastore = Datastore({
projectId: results["datastoreProject"],
keyFilename : "./service.json"
});
var dsDataFilesPath = "setup/data";
dsDataFilesPath = path.join(__dirname, dsDataFilesPath);
var required_values = [];
var datafilelist = fs.readdirSync(dsDataFilesPath);
var index = 0;
syncLoop(datafilelist.length, function(loop){
var j = index;
var filepath = dsDataFilesPath + "/" +datafilelist[j];
var data = fs.readFileSync(filepath, 'utf8');
populateDatastore(datastore, data,datafilelist[j].split(".")[0], function()
{
index++;
console.log(datafilelist[j].split(".")[0] + " created successfully.");
if(index == datafilelist.length)
{
cb(null);
}
else
{
loop.next();
}
});
}, null);
/*fs.readdirSync(dsDataFilesPath).forEach(file =>
{
var filepath = dsDataFilesPath + "/" +file;
var data = fs.readFileSync(filepath, 'utf8');
populateDatastore(datastore, data,file.split(".")[0], function()
{
console.log(file.split(".")[0] + " created successfully.");
});
})*/
}
function generateUUID() {
var d = new Date().getTime();
var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = (d + Math.random()*16)%16 | 0;
d = Math.floor(d/16);
return (c=='x' ? r : (r&0x3|0x8)).toString(16);
});
return uuid;
};
function syncLoop(iterations, process, exit){
var index = 0,
done = false,
shouldExit = false;
var loop = {
next:function(){
if(done){
if(shouldExit && exit){
return exit(); // Exit if we're done
}
}
// If we're not finished
if(index < iterations){
index++; // Increment our index
process(loop); // Run our process, pass in the loop
// Otherwise we're done
} else {
done = true; // Make sure we say we're done
if(exit) exit(); // Call the callback on exit
}
},
iteration:function(){
return index - 1; // Return the loop number we're on
},
break:function(end){
done = true; // End the loop
shouldExit = end; // Passing end as true means we still call the exit callback
}
};
loop.next();
return loop;
}