-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathindex.js
73 lines (62 loc) · 1.92 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
var through = require("through2"),
isEmpty = require("lodash.isempty"),
path = require("path"),
gutil = require("gulp-util");
module.exports = function (config) {
"use strict";
config = config || {};
var origin = config.filename || "urls.json",
subOjects = typeof config.subOjects !== 'undefined' ? config.subOjects : true,
firstFile,
directoryStructure = {};
function directoryMap(file, enc, callback) {
/*jshint validthis:true*/
if (!firstFile) {
firstFile = file;
}
// Do nothing if no contents
if (!file.isDirectory() && file.isNull()) {
this.emit("error", new gutil.PluginError("gulp-directory-map", "File is null"));
this.emit("end");
return callback();
}
// No support for streams yet.
if (file.isStream()) {
this.emit("error", new gutil.PluginError("gulp-directory-map", "No stream support!"));
this.emit("end");
return callback();
}
// But if it's a buffer...!
if (file.isBuffer()) {
var path = (config.prefix ? config.prefix + "/" : "") + file.path.replace(file.base, "");
var segments = path.replace(/\\/g,"/").split("/");
var parent = directoryStructure;
segments.forEach(function(seg, index){
if (index === segments.length-1){
parent[seg] = path.replace(/\\/g,"/");
} else if(subOjects) {
parent[seg] = parent[seg] || {};
parent = parent[seg];
}
});
}
return callback();
}
return through.obj(directoryMap,
function(cb) {
if (isEmpty(directoryStructure)) {
this.emit("error", new gutil.PluginError("gulp-directory-map", "No files found for directoryMap"));
this.emit("end");
return cb();
}
//create and push new vinyl file
this.push(new gutil.File({
cwd: firstFile.cwd,
base: firstFile.cwd,
path: path.join(firstFile.cwd, origin),
contents: new Buffer(JSON.stringify(directoryStructure))
}));
gutil.log("Generated", gutil.colors.blue(config.filename));
return cb();
});
};