-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
checkLibPaths.ts
56 lines (45 loc) · 1.75 KB
/
checkLibPaths.ts
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
/* eslint-disable import/prefer-default-export */
import { readFile } from 'fs-extra';
import { normalize } from 'path';
import yargs = require('yargs');
import { dirname } from './tool-utils';
export const findInvalidImportPaths = (baseDir: string, fileContent: string): string[] => {
const output: string[] = [];
// We expect the code to be in a specific format, as formatted by eslint. So
// checkLibPath must only be called after the linter hook.
const regexes = [
/^import .* from '(.*\.\.\/lib\/.*)'/gm,
/^import .* = require\('(.*\.\.\/lib\/.*)'\)/gm,
/^const .* = require\('(.*\.\.\/lib\/.*)'\)/gm,
/^import .* from '(.*\.\.\/renderer\/.*)'/gm,
/^import .* = require\('(.*\.\.\/renderer\/.*)'\)/gm,
/^const .* = require\('(.*\.\.\/renderer\/.*)'\)/gm,
];
for (const regex of regexes) {
while (true) {
const matches = regex.exec(fileContent);
if (!matches) break;
const [line, packagePath] = matches;
const fullPath = normalize(`${baseDir}/${packagePath}`);
if (fullPath.includes('packages/lib/') || fullPath.includes('packages/renderer/')) output.push(line);
}
}
return output;
};
const main = async () => {
const argv = await yargs.argv;
const filePaths = argv._ as string[];
if (!filePaths || !filePaths.length) return;
for (const filePath of filePaths) {
const content = await readFile(filePath, 'utf8');
const invalidImportPaths = findInvalidImportPaths(dirname(filePath), content);
if (invalidImportPaths.length) throw new Error(`Invalid lib import paths in ${filePath}: ${invalidImportPaths.join(' / ')}`);
}
};
if (require.main === module) {
// eslint-disable-next-line promise/prefer-await-to-then -- Old code before rule was applied
main().catch((error) => {
console.error(error);
process.exit(1);
});
}