/
index.js
61 lines (51 loc) · 1.8 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
'use strict';
var PLUGIN_NAME = 'gulp-ng-module-sort';
var pathUtil = require('path'),
through = require('through2'),
gulpUtil = require('gulp-util'),
path = require('path'),
PluginError = gulpUtil.PluginError;
function gulpNgModuleSort(options) {
options = options || {};
if(!!options) {
options.fileType = '.js';
}
var moduleFiles = [];
var otherFiles = [];
var sortedFiles = [];
return through.obj(onFile, onEnd);
function onFile(file, enc, cb) {
if(!file) {
this.emit('error', new PluginError(PLUGIN_NAME, 'Error reading file stream'));
return cb();
}
var absolutePath = file.path;
var ext = pathUtil.extname(absolutePath);
if(ext === options.fileType) {
var baseFilename = pathUtil.basename(absolutePath, ext).toLowerCase()
var parentDirectory = pathUtil.dirname(absolutePath).split(path.sep).slice(-1)[0];
if(!parentDirectory || parentDirectory.length < 1) {
var message = 'Parent directory not detected. Matching: fileType: ' + options.fileType;
this.emit('error', new PluginError(PLUGIN_NAME, message), {
fileName: file.path
});
return cb();
}
if(baseFilename.toLowerCase() === parentDirectory.toLowerCase()) {
moduleFiles.push(file);
} else {
otherFiles.push(file);
}
}
cb();
}
function onEnd(cb) {
var that = this;
sortedFiles = moduleFiles.concat(otherFiles);
sortedFiles.forEach(function(file) {
that.push(file);
});
cb();
}
};
module.exports = gulpNgModuleSort;