-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclean.js
99 lines (78 loc) · 2.68 KB
/
clean.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
'use strict';
var fs = require('fs');
var _ = feather.util;
var cleanList = /\.(?:txt|lock|md)|LICENSE|Grunt|gulp|Gemfile|webpack/i;
var configFinds =['bower.json', 'component.json', 'package.json', '.bower.json'];
var minReg = /[.-]min(?=\.)/;
var normalize = require('path').normalize;
function del(dir, file){
var name = file.substr(dir.length);
_.exists(file) && _.del(file);
}
module.exports = function(name){
var dir = normalize(process.cwd()) + '/components/' + name + '/';
var files = fs.readdirSync(dir);
if(files.length){
var config, json = {};
for(var i = 0; i < configFinds.length; i++){
var temp = dir + configFinds[i];
if(_.exists(temp)){
json = _.readJSON(temp);
config = temp;
break;
}
}
if(config && json.clean === false){
console.log('\nClear files completed!'.blue);
return;
}
var distDir = normalize(dir + 'dist/');
var hasDist = files.indexOf('dist') > -1 && _.isDir(distDir);
files.forEach(function(file){
if(
cleanList.test(file)
|| hasDist && file.indexOf('dist') == -1 && configFinds.indexOf(file) == -1
){
del(dir, dir + file);
return;
}
});
if(config){
var mains = _.makeArray(json.main), r = [];
for(var i = 0; i < mains.length; i++){
if(_.exists(dir + mains[i])){
json.main = mains[i];
break;
}
}
if(hasDist){
fs.renameSync(distDir, distDir = dir + '_dist_');
_.copy(distDir, dir);
_.del(distDir);
}
var main = json.main;
if(typeof main == 'string'){
if(/(?:^|\/)dist\//.test(main)){
main = main.replace(/(?:^|\/)dist\//, '');
}
if(minReg.test(main)){
var temp = main.replace(minReg, '');
if(_.exists(dir + temp)){
main = temp;
}
}
}
json.main = main;
}
//clean min
_.find(dir, minReg).forEach(function(file){
_.exists(file.replace(minReg, '')) && del(dir, file);
});
//clean source map
_.find(dir, /\.map$/).forEach(function(file){
del(dir, file);
})
_.write(dir + 'bower.json', JSON.stringify(json, null, 2));
}
console.log('\nClear files completed!'.blue);
};