This repository has been archived by the owner on Jul 31, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
201 lines (160 loc) · 5.25 KB
/
utils.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
var config = require('./config');
var fs = require('fs');
var path = require('path');
var merge = require('merge');
var plumber = require('gulp-plumber');
var notify = require('gulp-notify');
var browsersync = require('browser-sync');
module.exports.gulp = null;
/**
* Define a new Gulp task.
*/
module.exports.task = function() {
return this.gulp.task.apply(this.gulp, arguments);
};
/**
* Map a set of file globs onto a common root.
*/
module.exports.mapFiles = function(paths, root) {
if(! Array.isArray(paths)) {
paths = [ paths ];
}
return paths.map(function(lp) {
if(lp[0] === '!') {
// This is a negating filter
return '!' + path.join(root, lp.substring(1));
}
return path.join(root, lp);
});
};
/**
* Take a stream and change so that any errors are shown in a notification.
*/
module.exports.noErrors = function(stream, title) {
return stream.pipe(plumber({
errorHandler: notify.onError({
title: title || 'Build failed',
message: '<%= error.message %>'
})
}));
};
/**
* Get the glob to use for finding files for the given type and sub key.
*/
module.exports.glob = function(type, key, base) {
var root = typeof base === 'undefined' ? config.shared.src : base;
var localConfig = config[type];
if(! localConfig) throw 'No configuration for ' + type + ', check your configuration';
if(! localConfig[key]) throw 'No files defined for ' + type + ' and value ' + key + ', check your configuration';
var srcDir = path.join(root, localConfig.root || '');
return this.mapFiles(localConfig[key], srcDir);
};
/**
* Get the source directory for the given type by looking it up in the
* configuration.
*/
module.exports.srcDir = function(type, base) {
var root = typeof base === 'undefined' ? config.shared.src : base;
var localConfig = config[type];
if(! localConfig) throw 'No configuration for ' + type + ', check your configuration';
return path.join(root, localConfig.dir);
};
/**
* Get the glob to use for finding the source files for the given type.
*/
module.exports.srcGlob = function(type, base) {
var root = typeof base === 'undefined' ? config.shared.src : base;
var localConfig = config[type];
if(! localConfig) throw 'No configuration for ' + type + ', check your configuration';
if(! localConfig.src) throw 'No source files defined for ' + type + ', check your configuration';
var srcDir = path.join(root, localConfig.root || '');
return this.mapFiles(localConfig.src, srcDir);
};
/**
* Start a stream via gulp.src looking up the source via a configuration value.
*/
module.exports.srcFor = function(type, opts, base) {
return this.noErrors(
this.gulp.src(this.srcGlob(type, base), opts)
);
};
/**
* Get the destination directory for the given type.
*/
module.exports.destDir = function(type, base) {
var root = typeof base === 'undefined' ? config.shared.dest : base;
var localConfig = config[type];
if(! localConfig) throw 'No configuration for ' + type + ', check your configuration';
var local = typeof localConfig.dest !== 'undefined' ? localConfig.dest : localConfig.root;
return path.join(root, local);
};
/**
* Get the path to a destination file for the given type.
*/
module.exports.destFile = function(type, file, base) {
var dir = this.destDir(type, base);
return path.join(dir, file);
};
/**
* Get the Gulp destination for the given type.
*/
module.exports.destFor = function(type, base) {
return this.gulp.dest(this.destDir(type, base));
};
/**
* Get the glob to use for files that should be watched for the given type.
*/
module.exports.watchGlob = function(type, base) {
var root = typeof base === 'undefined' ? config.shared.src : base;
var localConfig = config[type];
if(! localConfig) throw 'No configuration for ' + type + ', check your configuration';
if(! localConfig.watch && ! localConfig.src) throw 'No source files defined for ' + type + ', check your configuration';
var srcDir = path.join(root, localConfig.root || '');
return this.mapFiles(localConfig.watch || localConfig.src, srcDir);
};
/**
* Utility that can be used in pipes to reload the brower of the user.
*/
module.exports.reloadBrowser = function(file) {
if(file) {
browsersync.reload(file);
} else {
return browsersync.reload({ stream: true });
}
};
/**
* Find and return all plugins to Brygga that can be found.
*/
module.exports.plugins = function() {
if(this._plugins) return this._plugins;
// Loop through any file and folder in node_modules of the current dir
this._plugins = fs.readdirSync('node_modules')
.map(function(m) {
var root = path.join(process.cwd(), 'node_modules', m);
var file = path.join(root, 'package.json');
if(! fs.existsSync(file)) return;
var pkg = require(file);
if(! pkg.keywords || pkg.keywords.indexOf('brygga-plugin') === -1) return;
var plugin = require(root);
plugin.name = m;
if(plugin.config) {
// Merge configurations
merge.recursive(config, plugin.config);
// Do updates of configs for watch and build
Object.keys(plugin.config).forEach(function(k) {
var subConfig = plugin.config[k];
if(subConfig.watch) {
config.watch.types.push(k);
}
if(subConfig.buildStep) {
config.build[subConfig.buildStep].push(k);
}
});
}
return plugin;
})
.filter(function(m) {
return !! m;
});
return this._plugins;
};