-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
155 lines (129 loc) · 3.38 KB
/
index.js
File metadata and controls
155 lines (129 loc) · 3.38 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
const glob = require("glob"),
fs = require('fs'),
path = require('path');
const log = require('nl-clilog'),
mkdirp = require('mkdirp');
const format = require('./format');
/**
* Reg Parse
*/
const LESSPARSE = /([..\/]+[\w-]+)+\.less/g;
//TODO Other fileTypes
const IMGPARSE = 0;
//TODO
//目前不可变
const DEFAULTDIST = 'build/';
/**
* Tree of Dependenies
*/
var Tree = {};
/**
* @param {varType} path Description
* @param {varType} option Description
* @return {void} description
*/
function LessDiff(options) {
this.options = options || {};
this.options.isAbsolute = this.options.isAbsolute || false;
}
/**
* @param {varType} src Description
* @return {void} description
*/
LessDiff.prototype._getTree = function(srcPath, opts, callback) {
glob(srcPath, opts, (err, files) => {
if (err) return;
files.forEach((file, idx) => {
const data = fs.readFileSync(file, 'utf8');
const dpPathList = this.getDpList(data, file);
this.writeDpList(dpPathList, file);
});
fs.writeFileSync('tree.js', format(JSON.stringify(Tree)));
log.debug('已在当前目录生成依赖关系文件"tree.js"');
callback && callback(Tree);
});
}
/**
* @param {varType} path Description
* @return {void} description
*/
LessDiff.prototype._getChangedList = function(path) {
//if (!pathList) pathList = [];
this.pathList = [];
this.changedList = [];
this.redex(path);
this.changedList.forEach((chaneged, idx) => {
log.error('changed:' + chaneged);
});
return _unique(this.pathList);
}
LessDiff.prototype.redex = function(path, hL) {
if (!hL) {
hL = [];
hL.push('\033[1;32m' + path + '\033[0m');
} else {
hL.push('\033[1;33m' + path + '\033[0m');
}
var dpList = [];
this.pathList.push(path);
this.changedList.push(hL.join('\033[31m' + '=>' + '\033[0m'));
if (Tree[path] && Tree[path].length > 0) {
// console.log( Tree[path]);
Tree[path].forEach((_path, idx) => {
//changedList.push(_path);
//console.log('redex:'+path);
dpList.push(this.redex(_path, hL));
});
}
}
/**
* Get Dependencies Path List
* @param {varType} buffer Description
* @return {void} description
*/
LessDiff.prototype.getDpList = function(buffer, currentPath) {
const list = buffer.match(LESSPARSE) || [];
return list.map((_path, idx) => {
const dirPath = path.dirname(currentPath);
return this.options.isAbsolute ? path.resolve(path.join(dirPath, _path)) : path.join(dirPath, _path);
});
}
/**
* @param {varType} srcPath Description
* @param {varType} pathsList Description
* @return {void} description
*/
LessDiff.prototype.writeDpList = function(dpPathList, currentPath) {
//if (!Tree[currentPath]) Tree[currentPath] = [];
dpPathList.forEach((_path, idx) => {
if (!Tree[_path]) Tree[_path] = [];
if (this.options.isAbsolute) {
Tree[_path].push(path.resolve(currentPath));
} else {
Tree[_path].push(currentPath);
}
});
return Tree;
}
/**
* @param {varType} _array Description
* @return {void} description
*/
function _unique(_array) {
var res = [],
json = {};
for (var i = 0; i < _array.length; i++) {
if (!json[_array[i]]) {
res.push(_array[i]);
json[_array[i]] = 1;
}
}
return res;
}
/**
* Export LessDiff
* @param {varType} path Description
* @param {varType} options Description
* @return {void} description
*/
exports.LessDiff = LessDiff;