forked from vendure-ecommerce/vendure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck-imports.ts
69 lines (63 loc) · 2.29 KB
/
check-imports.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
57
58
59
60
61
62
63
64
65
66
67
68
69
/* eslint-disable no-console */
import fs from 'fs';
import path from 'path';
// eslint-disable-next-line @typescript-eslint/no-var-requires
const find = require('find');
/**
* An array of regular expressions defining illegal import patterns to be checked in the
* source files of the monorepo packages. This prevents bad imports (which work locally
* and go undetected) from getting into published releases of Vendure.
*/
const illegalImportPatterns: RegExp[] = [
/@vendure\/common\/src/,
/@vendure\/core\/src/,
/@vendure\/admin-ui\/src/,
];
const exclude: string[] = [
path.join(__dirname, '../packages/dev-server'),
];
findInFiles(illegalImportPatterns, path.join(__dirname, '../packages'), /\.ts$/, exclude);
function findInFiles(patterns: RegExp[], directory: string, fileFilter: RegExp, excludePaths: string[]) {
find.file(fileFilter, directory, async (files: string[]) => {
const nonNodeModulesFiles = files.filter(f => !f.includes('node_modules'));
console.log(`Checking imports in ${nonNodeModulesFiles.length} files...`);
const matches = await getMatchedFiles(patterns, nonNodeModulesFiles, excludePaths);
if (matches.length) {
console.error(`Found illegal imports in the following files:`);
console.error(matches.join('\n'));
process.exitCode = 1;
} else {
console.log('Imports check ok!');
}
});
}
async function getMatchedFiles(patterns: RegExp[], files: string[], excludePaths: string[]) {
const matchedFiles = [];
outer:
for (let i = files.length - 1; i >= 0; i--) {
for (const excludedPath of excludePaths) {
if (files[i].includes(excludedPath)) {
continue outer;
}
}
const content = await readFile(files[i]);
for (const pattern of patterns) {
if (pattern.test(content)) {
matchedFiles.push(files[i]);
continue;
}
}
}
return matchedFiles;
}
function readFile(filePath: string): Promise<string> {
return new Promise((resolve, reject) => {
fs.readFile(filePath, 'utf-8', (err, data) => {
if (err) {
reject(err);
} else {
resolve(data);
}
});
});
}