-
Notifications
You must be signed in to change notification settings - Fork 15
/
index.js
75 lines (61 loc) · 2 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
// Copyright IBM Corp. 2014. All Rights Reserved.
// Node module: gulp-loopback-sdk-angular
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
'use strict';
var gutil = require('gulp-util');
var through = require('through2');
var generator = require('loopback-sdk-angular');
var path = require('path');
var WatchJS = require('watchjs');
var watch = WatchJS.watch;
var unwatch = WatchJS.unwatch;
// This callback will be passed to watch, this refers to the datasource
function disconnectDataSource(prop, action, newValue) {
if (newValue === true) {
this.disconnect();
unwatch(this, 'connected');
}
}
module.exports = function (options) {
return through.obj(function (file, enc, cb) {
if (file.isNull()) {
this.push(file);
cb();
return;
}
var app;
try {
app = require(path.resolve(file.path));
// Incase options is undefined.
options = options || {ngModuleName: 'lbServices', apiUrl: undefined};
options.ngModuleName = options.ngModuleName || 'lbServices';
options.apiUrl = options.apiUrl || app.get('restApiRoot') || '/api';
gutil.log('Loaded LoopBack app', gutil.colors.magenta(file.path));
gutil.log('Generating',
gutil.colors.magenta(options.ngModuleName),
'for the API endpoint',
gutil.colors.magenta(options.apiUrl)
);
var script = generator.services(
app,
options.ngModuleName,
options.apiUrl
);
file.contents = new Buffer(script);
gutil.log('Generated Angular services file.');
this.push(file);
var dataSources = app.dataSources;
for (var dataSource in dataSources) {
if (dataSources.hasOwnProperty(dataSource)) {
var ds = dataSources[dataSource];
watch(ds, 'connected', disconnectDataSource);
}
}
} catch (err) {
this.emit('error', new gutil.PluginError('gulp-loopback-sdk-angular', err));
}
cb();
return;
});
};