-
Notifications
You must be signed in to change notification settings - Fork 778
/
Copy pathutils.js
78 lines (64 loc) · 1.71 KB
/
utils.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
'use strict';
const fs = require('fs');
const path = require('path');
const glob = require('tiny-glob/sync');
function existsStat () {
try {
return fs.statSync.apply(fs, arguments);
} catch (e) {
return null;
}
}
function getIgnoreList (baseDir) {
const gitFilePath = path.join(baseDir, '.gitignore');
if (fs.existsSync(gitFilePath)) {
const gitIgnore = fs.readFileSync(gitFilePath, 'utf8');
// Account for Windows-style line endings
return gitIgnore.trim().split(/\r?\n/);
}
return [];
}
function getFilesFromArgs (args) {
const patterns = args.slice();
// Default to files in the test directory
if (!patterns.length) {
patterns.push('test/**/*.{js,mjs,cjs}');
}
const files = new Set();
// For each of the potential globs, we check if it is a directory path and
// update it so that it matches the JS files in that directory.
patterns.forEach(pattern => {
const stat = existsStat(pattern);
if (stat && stat.isFile()) {
// Optimisation:
// For non-glob simple files, skip (slow) directory-wide scanning.
// https://github.com/qunitjs/qunit/pull/1385
files.add(pattern);
} else {
if (stat && stat.isDirectory()) {
pattern = `${pattern}/**/*.{js,mjs,cjs}`;
}
const results = glob(pattern, {
cwd: process.cwd(),
filesOnly: true,
flush: true
});
for (const result of results) {
files.add(result);
}
}
});
if (!files.size) {
error('No files were found matching: ' + args.join(', '));
}
return Array.from(files).sort();
}
function error (message) {
console.error(message);
process.exit(1);
}
module.exports = {
error,
getFilesFromArgs,
getIgnoreList
};