-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.js
109 lines (99 loc) · 3.5 KB
/
lib.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
const { EOL } = require('os');
const fs = require('fs');
const path = require('path');
const prependWarningText = ({
eslintDisableLine, fileList, log, warningText,
}) => {
const prependedData = `${eslintDisableLine}${EOL}${warningText}`;
log('Data to be prepended:');
log('---Begin---');
log(prependedData);
log('----End----');
fileList.forEach((file) => {
const data = fs.readFileSync(path.resolve(file), 'utf8');
if (data.includes('/* eslint-disable */')) {
log(`${file} already has ESLint disabled, ignoring`);
return;
}
if (data.startsWith('#!')) {
const firstLine = data.split(EOL)[0];
const remainingLines = data.split(EOL).slice(1, -1).join(EOL);
const fullData = `${firstLine}${EOL}${prependedData}${EOL}${EOL}${remainingLines}`;
fs.writeFileSync(path.resolve(file), fullData, 'utf8');
log(`Data was prepended to ${file} while preserving shebang`);
return;
}
const fullData = `${prependedData}${EOL}${EOL}${data}`;
fs.writeFileSync(path.resolve(file), fullData, 'utf8');
log(`Data was prepended to ${file}`);
});
};
const getIgnoredFiles = ({ log }) => {
try {
const data = fs.readFileSync('.eslintignore', 'utf8');
log('Found .eslintignore');
const ignoredFiles = data.split(EOL);
return ignoredFiles;
} catch (error) {
if (error.message.startsWith('ENOENT')) {
log('No .eslintignore found, using default of \'node_modules\'');
return ['node_modules'];
}
throw error;
}
};
const getJavaScriptFiles = ({
directory, log, fileList = [], searchHistory = [], originalDirectory = directory,
}) => {
if (directory.length < originalDirectory.length) {
log(`Searching finished at ${originalDirectory}`);
return fileList;
}
const parentDirectory = directory.slice(0, directory.lastIndexOf('/'));
if (searchHistory.includes(directory)) {
log(`${directory} has already been searched (early return case)`);
return getJavaScriptFiles({
directory: parentDirectory, log, fileList, searchHistory, originalDirectory,
});
}
log(`Searching ${directory}`);
const filesAndDirectories = fs.readdirSync(path.resolve(directory))
.map(fileOrDirectory => `${directory}/${fileOrDirectory}`);
const directories =
filesAndDirectories.filter(file => fs.statSync(path.resolve(file)).isDirectory());
for (let i = 0; i < directories.length; i += 1) {
if (!searchHistory.includes(directories[i])) {
log(`${directory} has already been searched (for loop)`);
return getJavaScriptFiles({
directory: directories[i], log, fileList, searchHistory, originalDirectory,
});
}
}
const files = filesAndDirectories.filter(file => !fs.statSync(file).isDirectory());
const jsFilesInCurrentDirectory = [...files.filter(file => file.toLowerCase().endsWith('.js'))];
const jsFiles = [...fileList, ...jsFilesInCurrentDirectory];
const newSearchHistory = [...searchHistory, directory];
if (!jsFilesInCurrentDirectory.length) {
log(`No JavaScript files found in ${directory}`);
return getJavaScriptFiles({
directory: parentDirectory,
log,
fileList: jsFiles,
searchHistory: newSearchHistory,
originalDirectory,
});
}
jsFilesInCurrentDirectory.forEach(file => log(`Found ${file}`));
return getJavaScriptFiles({
directory: parentDirectory,
log,
fileList: jsFiles,
searchHistory: newSearchHistory,
originalDirectory,
});
};
module.exports = {
getIgnoredFiles,
getJavaScriptFiles,
prependWarningText,
};