-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
85 lines (70 loc) · 1.69 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
74
75
76
77
78
79
80
81
82
83
84
85
'use strict';
var globby = require('globby');
var fs = require('fs');
var eachAsync = require('each-async');
var slug = require('slug');
var objectAssign = require('object-assign');
var UPPER = /([A-Z])/g;
function kebab (input) {
return input.replace(UPPER, function (upLetter, _, offset) {
return (offset === 0 ? '' : '-') + (upLetter.toLowerCase());
});
}
module.exports = function (patterns, opts, cb) {
if (typeof opts !== 'object') {
cb = opts;
opts = {};
}
opts = objectAssign({}, opts);
cb = cb || function () {};
var sluggedFiles = [];
globby(patterns, opts, function (err, files) {
// Error
if (err) {
cb(err);
return;
}
eachAsync(files, function (el, i, next) {
// Path
var path = el.split('/');
var dirs = path.slice(0, path.length - 1);
// Get extension
var file = path[path.length - 1].split('.');
var ext = '';
var old = '';
var filename = '';
// If extension
if (file.length > 1) {
ext = file[file.length - 1];
filename = file.slice(0, file.length - 1);
old = filename.join('.');
} else {
old = el;
}
// Slug it
var slugged = slug(kebab(old), {
lower: true
});
slugged += '.' + ext;
dirs.push(slugged);
var fullSlugged = dirs.join('/');
fs.rename(el, fullSlugged, function (err) {
if (err) {
cb(err);
next();
}
sluggedFiles.push({
new: fullSlugged,
old: el
});
next();
});
}, function (err) {
if (err) {
cb(err, null);
return;
}
cb(null, sluggedFiles);
});
});
};