forked from afilp/gulp-inject-version
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
62 lines (53 loc) · 2.2 KB
/
Copy pathindex.js
File metadata and controls
62 lines (53 loc) · 2.2 KB
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
var fs = require('fs'),
through = require('through2'),
gutil = require('gulp-util'),
plugin_name = 'gulp-inject-version',
defaults = {
package_file: 'package.json',
version_property: 'version',
prepend: 'v',
append: '',
replace: /%%GULP_INJECT_VERSION%%/g
};
module.exports = function (opts) {
var opt_key,
inject = function (file, encoding, callback) {
var package_obj,
version_text;
if (file.isNull()) {
return callback(null, file);
}
if (file.isStream()) {
return callback(new gutil.PluginError(plugin_name, 'doesn\'t support Streams'));
}
fs.readFile(opts.package_file, 'utf8', function (err, data) {
if (err) {
return callback(new gutil.PluginError(plugin_name, opts.package_file + ' could not be read.\n\t' + err.toString()));
} else {
try {
package_obj = JSON.parse(data);
if (package_obj[opts.version_property]) {
version_text = [
opts.prepend,
package_obj[opts.version_property],
opts.append,
].join('');
file.contents = new Buffer(file.contents.toString().replace(opts.replace, version_text));
callback(null, file);
} else {
return callback(new gutil.PluginError(plugin_name, opts.version_property + ' not found in package data object.'));
}
} catch (ignore) {
return callback(new gutil.PluginError(plugin_name, opts.package_file + ' not valid JSON'));
}
}
});
};
opts = opts || {};
for (opt_key in defaults) {
if (Object.prototype.hasOwnProperty.call(defaults, opt_key)) {
opts[opt_key] = (opts[opt_key] !== undefined) ? opts[opt_key] : defaults[opt_key];
}
}
return through.obj(inject);
};