-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathindex.js
41 lines (32 loc) · 891 Bytes
/
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
'use strict';
const path = require('path');
const normalize = path.normalize;
const sep = path.sep;
module.exports = function (task) {
task.plugin('flatten', { every:false }, function * (files, opts) {
if (!files.length) {
return task.emit('plugin_warning', {
plugin: '@taskr/flatten',
warning: 'No source files to flatten!'
});
}
const levels = opts.levels || 0;
// find shortest `dir` path; glob originated here
const globBaseDir = files.map(o => o.dir).sort()[0];
for (let file of files) {
file.dir = trimmer(globBaseDir, file.dir, levels);
}
});
};
function trimmer(baseDir, fileDir, levels) {
if (levels === 0) {
return baseDir;
}
let dirs = fileDir.replace(baseDir, '');
const arr = dirs.split(sep);
const len = arr.length;
if (levels < len) {
dirs = arr.splice(len - levels).join(sep);
}
return normalize(baseDir + sep + dirs);
}