-
Notifications
You must be signed in to change notification settings - Fork 357
/
Copy pathdirectory.js
209 lines (209 loc) · 5.8 KB
/
directory.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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.LocDir = void 0;
const globby_1 = __importDefault(require("globby"));
const fs_extra_1 = __importDefault(require("fs-extra"));
const path_1 = __importDefault(require("path"));
const slash2_1 = __importDefault(require("slash2"));
const file_1 = require("./file");
const languages_1 = require("./languages");
const defaultInfo = {
total: 0,
code: 0,
comment: 0,
};
const defaultExclude = [
// javascript
'**/*.map',
'**/yarn**',
'**/.github',
'**/node_modules/**',
'**/dist/**',
'**/*.snap',
// java
'**/target',
'**/*.class',
'**/*.o',
'**/bin',
'**/*.map',
// python
'**/*.pyc',
'**/*.pyo',
// other
'**/*.dil',
'**/*.ra',
// images
'**/*.png',
'**/*.jpg',
'**/*.jpeg',
'**/*.gif',
'**/*.ico',
'**/*.bmp',
'**/*.webp',
'**/*.tiff',
'**/*.psd',
'**/*.ai',
'**/*.ps',
'**/*.eps',
// fonts
'**/*.ttf',
'**/*.otf',
'**/*.woff',
'**/*.woff2',
'**/*.eot',
'**/*.ttc',
// audio
'**/*.mp3',
'**/*.wav',
'**/*.ogg',
'**/*.flac',
'**/*.aac',
'**/*.m4a',
'**/*.aif*',
// video
'**/*.mp4',
'**/*.mkv',
'**/*.avi',
'**/*.mov',
'**/*.wmv',
'**/*.mpg',
'**/*.mpeg',
'**/*.m2v',
'**/*.m4v',
// office
'**/*.doc',
'**/*.docx',
'**/*.docm',
'**/*.dot',
'**/*.dotx',
'**/*.xls',
'**/*.xlsx',
// documents
'**/*.pdf',
'**/*.epub',
'**/*.mobi',
// archives
'**/*.rar',
'**/*.zip',
'**/*.7z',
'**/*.tar',
'**/*.gz',
'**/*.bz2',
'**/*.bz',
'**/*.tbz',
'**/*.tgz',
];
function ensureArray(arr, dfault) {
if (!arr) {
return dfault ? [dfault] : [];
}
return Array.isArray(arr) ? arr : [arr];
}
/**
* Collect the info of a directory.
*/
class LocDir {
constructor(options) {
Object.defineProperty(this, "cwd", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "include", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "exclude", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "analysisLanguages", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "allLanguages", {
enumerable: true,
configurable: true,
writable: true,
value: new languages_1.Languages()
});
// ensure all excludes are globstar. Note that '**/*.ts/**' matches files
// that end in .ts because the globstar indicates 0 or more directory paths.
this.exclude = ensureArray(options.exclude)
.concat(defaultExclude)
.map((item) => (item.endsWith('**') ? item : `${item}/**`));
// remove all leading './' since this messes up globstar matches in the
// excludes.
this.include = ensureArray(options.include, '**')
.map((item) => (item.startsWith('./') ? item.substring(2) : item))
.map((item) => (item.endsWith('**') ? item : `${item}/**`));
this.cwd = options.cwd || process.cwd();
this.analysisLanguages = options.analysisLanguages;
}
/**
* Calculate directory info.
*/
async loadInfo() {
const paths = await globby_1.default(this.include, {
cwd: this.cwd,
ignore: this.exclude,
nodir: true,
});
const files = [];
const info = { ...defaultInfo };
let languages = {};
// We _could_ use Promise.all to count the files in parallel, but that
// would lead to out of memory errors when there are many files.
// eslint-disable-next-line no-restricted-syntax
for (const pathItem of paths) {
const fullPath = slash2_1.default(path_1.default.join(this.cwd, pathItem));
if (!pathItem ||
this.ignoreLanguage(pathItem) ||
!(await fs_extra_1.default.pathExists(fullPath)) ||
(await fs_extra_1.default.stat(fullPath)).isDirectory()) {
continue;
}
const file = new file_1.LocFile(fullPath);
const fileLineInfo = await file.getFileInfo();
const { lines } = fileLineInfo;
info.total += lines.total;
info.code += lines.code;
info.comment += lines.comment;
const language = { ...languages[fileLineInfo.languages] };
language.code = lines.code + (language.code || 0);
language.sum = (language.sum || 0) + 1;
language.comment = lines.comment + (language.comment || 0);
language.total = lines.total + (language.total || 0);
languages = {
...languages,
[fileLineInfo.languages]: language,
};
files.push(fullPath);
}
return {
files,
info,
languages,
};
}
/**
* Ignore analyzing this file if analysis languages are specified
* and this language is not one of them.
*/
ignoreLanguage(pathItem) {
return (this.analysisLanguages &&
!this.analysisLanguages.includes(this.allLanguages.getType(pathItem)));
}
}
exports.LocDir = LocDir;
//# sourceMappingURL=directory.js.map