-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.js
More file actions
288 lines (265 loc) · 12.2 KB
/
app.js
File metadata and controls
288 lines (265 loc) · 12.2 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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
// Imports.
var fs = require('fs'),
xml2js = require('xml2js'),
_ = require('lodash'),
Promise = require('promise'),
glob = require('glob'),
upath = require('upath'),
extend = require('extend'),
minimatch = require('minimatch');
// Initialize variables.
var nrOfCheckedFiles = 0,
nrOfIncludedFiles = 0,
nrOfPhysicalFiles = 0,
nrOfTargetedFiles = 0,
nrOfNotIncludedFiles = 0,
notIncludedFiles = [],
maxFilesToShowDefault = 20,
options = {},
basePath;
// Polyfills for ES6 functions.
if (!String.prototype.startsWith) {
String.prototype.startsWith = function(prefix) {
return this.indexOf(prefix) === 0;
}
}
// Source: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith
if (!String.prototype.endsWith) {
String.prototype.endsWith = function (searchString, position) {
var subjectString = this.toString();
if (position === undefined || position > subjectString.length) {
position = subjectString.length;
}
position -= searchString.length;
var lastIndex = subjectString.indexOf(searchString, position);
return lastIndex !== -1 && lastIndex === position;
};
}
/**
* Print a debug message to the console (only if debug flag is set).
* @param {string} The message to print.
*/
function debug(message) {
if (options.verbose) {
console.log(message);
}
}
/**
* Return the path of a given filename.
* @param {string} The full path to a file, for instance '../MyApp.Web/App.Web.csproj'.
* @returns {string} Only the path to the file excluding the filename, for instance 'App.Web.csproj'
*/
function getBasePath(pathAndFile) {
if (!pathAndFile) {
throw new Error('getBasePath: Supplied path is empty!');
}
// Remove a potential end slash from path to prevent errors
if (pathAndFile.endsWith('/')) {
pathAndFile.substring(0, pathAndFile.length - 1)
}
var index = pathAndFile.lastIndexOf('/');
return index === -1 ? "" : pathAndFile.substring(0, index + 1);
}
/**
* @param {string} csProjFilename
* @returns {string} All files from the given cs project file.
*/
function readCsProjeFile(csProjFilename) {
var fileIncludes = [];
var parser = new xml2js.Parser();
return new Promise(function (resolve, reject) {
fs.readFile(csProjFilename, function (err, data) {
if (err) {
reject('File \'' + csProjFilename + '\' not found.');
return;
}
parser.parseString(data, function (err, result) {
var itemgroups = result.Project.ItemGroup;
if (!itemgroups || itemgroups.length === 0) {
reject('No item groups found in ' + csProjFilename);
}
_.each(result.Project.ItemGroup, function (itemGroup, groupKey, groupList) {
var contentItems = itemGroup.Content || [];
var tsItems = itemGroup.TypeScriptCompile || [];
var items = contentItems.concat(tsItems);
if (items.length === 0) {
debug('Warning: No <Content> or <TypeScriptCompile> tags found in <ItemGroup> nr ' + groupKey + '.');
// When we are at the last group iteration then filling the 'fileIncludes' is done, so 'return' it.
if (groupKey === groupList.length - 1) {
resolve(fileIncludes);
}
} else {
debug('Scanning ' + items.length + ' <Content> or <TypeScriptCompile> tags found in <ItemGroup> nr ' + groupKey + '.');
}
_.each(items, function (content, contentKey, contentList)
{
// 6. Filter uit de fileContents alle <Content Include="x"> uit alle <ItemGroups> en sla elk van deze 'x'-en op in een array 'includedFiles'
if (content && content.Link) {
fileIncludes.push(content.Link[0]);
} else
if (content && content.$ && content.$.Include /* && content.$.Include.endsWith('.js') */) {
fileIncludes.push(content.$.Include);
}
if (contentKey === contentList.length - 1 && groupKey === groupList.length - 1) {
// Then filling the 'fileIncludes' is done, so 'return' it.
resolve(fileIncludes);
}
});
});
});
});
});
}
// Determine the filename of the .csproj file, if not provided.
function getCsProjFile(csProjFilename) {
return new Promise(function (resolve, reject) {
// If csproj filename was provided then simply return that one :)
if (typeof csProjFilename !== 'undefined' && csProjFilename) {
resolve(csProjFilename);
} else {
// Try to get a .csproj file from the (root of) the current working directory (with node-glob).
glob('*.csproj', { cwd: options.cwd }, function(err, files) {
if ((files.length) === 0) {
reject('No .csproj was found in the current working directory, check-vs-includes cannot run!');
return;
}
if (files.length > 1) {
reject('More than one .csproj file was found in the working directory. Please specify an explicit .csproj file in the \'csproj\' parameter, or (temporarily) change the extension of the rest from \'.csproj\' so only one is left:\n' + files.join('\n'));
return;
}
csProjFilename = files[0];
csProjFilename = options.cwd + csProjFilename;
resolve(csProjFilename);
});
}
});
}
function searchFilesNotIncludedIn(csProjFilename, filter) {
return new Promise(function(resolve, reject) {
if (filter.length === 0) {
reject('No stuff to check!');
return;
}
var includedFiles = [];
readCsProjeFile(csProjFilename)
.then(function(result) {
return new Promise(function (resolve, reject) {
if (!result || result.length === 0) reject('No files in result');
nrOfIncludedFiles = result.length;
_.each(result, function (filename, fileKey, fileList) {
var pattern = filter[0];
// Do some conversions to allow simple string comparison to the files as returned by node - glob.
// 1. The files in the .csproj are in windows format, so convert to unix;
var unixFilename = upath.normalize(filename);
// 2. Add the cwd to the file - if set.
if (basePath) {
unixFilename = basePath + unixFilename;
}
// 3. Check if the filename from the .csproj file matches any of the patterns in the filter (for instance it has a .js or .less extension e.g. filter = ['**/*.js/', '**/*.less']).
if (_.any(_.map(filter, function(pattern) {
return minimatch(unixFilename, pattern);
}))) {
// if so then include in list to test agains.
includedFiles.push(unixFilename);
}
if (fileKey === fileList.length - 1) {
resolve(includedFiles);
}
});
}).then(function(includedFiles) {
_.each(filter, function(pattern, patternKey, patternList) {
glob(pattern, { debug: options.verbose, cwd: options.cwd }, function(err, files) {
if (err) {
reject('Error for glob pattern \'' + pattern + '\' in stuff to check!');
}
if (files.length === 0) {
reject('No files for pattern \'' + pattern + '\' in stuff to check.');
}
nrOfTargetedFiles = files.length;
_.each(files, function(filename, fileKey, fileList) {
// 4. Filter de bestandsnamen op het filter bv. alleen **/*.js files - en sla op in array 'filesToCheck'.
var isIncluded = _.contains(includedFiles, filename);
nrOfCheckedFiles++;
if (!isIncluded) {
notIncludedFiles.push(filename);
}
// When we are at the last item of the last group in the (nested) iteration.
if (patternKey === patternList.length - 1 && fileKey === fileList.length - 1) {
// Then searching for not included files is done, so 'return' result.
resolve(notIncludedFiles);
}
});
});
});
});
}).catch(function(error) {
reject(error);
return;
});
});
}
function check(patterns, userOptions, csProj) {
var defaultOptions = {
cwd: '.',
maxFilesToShow: maxFilesToShowDefault,
verbose: false
};
options = extend(defaultOptions, userOptions);
// If stuffToCheck contains a single string then convert it to array of one.
if (patterns && typeof patterns === 'string') {
patterns = [patterns];
}
options.stuffToCheck = patterns;
debug('Starting.');
debug('Options settings: ');
debug(options);
if (options.cwd) {
// Append a slash if missing.
if (!options.cwd.endsWith('/')) {
options.cwd += '/';
}
// Add base path to all patterns in stuff to check.
_.each(options.stuffToCheck, function (pattern, patternKey, patternList) {
// Remove slash from beginning of globs if present.
if (pattern.startsWith('/')) {
pattern = pattern.substring(1);
}
patternList[patternKey] = options.cwd + pattern;
});
// And prefix the .csproj file with the base path as well.
if (typeof csProj !== 'undefined' && csProj ) {
options.csProjFilename = options.cwd + csProj;
}
}
var message;
getCsProjFile(options.csProjFilename).then(function(csProjFilename) {
basePath = getBasePath(csProjFilename);
options.csProjFilename = csProjFilename;
return csProjFilename;
}).then(function() {
return searchFilesNotIncludedIn(options.csProjFilename, options.stuffToCheck);
})
.then(function(notIncludedFiles) {
var nrOfNotIncludedFiles = notIncludedFiles.length;
if (nrOfNotIncludedFiles > 0) {
message = 'There ' + (nrOfNotIncludedFiles === 1 ? 'is ' : 'are ') + nrOfNotIncludedFiles + ' of the total ' + nrOfCheckedFiles + ' checked files NOT included in \'' + options.csProjFilename + '\':';
console.log(message);
var limitResultsShown = options.maxFilesToShow > 0;
var notIncludedFilesShown = limitResultsShown ? notIncludedFiles.slice(0, options.maxFilesToShow) : notIncludedFiles;
_.each(notIncludedFilesShown, function (filename) {
console.log(filename);
message += "\n" + filename;
});
if (limitResultsShown && nrOfNotIncludedFiles > options.maxFilesToShow) {
var subMessage = 'Only the first ' + options.maxFilesToShow + ' files are shown (you can change \'maxFilesToShow\' parameter, default: ' + maxFilesToShowDefault + ')';
console.log(subMessage);
}
} else {
message = 'All ' + nrOfCheckedFiles + ' checked files are included in \'' + options.csProjFilename + '\'.';
console.log(message);
}
}).catch(function(error) {
console.log('Error: ' + error);
});
}
module.exports = check;