-
Notifications
You must be signed in to change notification settings - Fork 822
Expand file tree
/
Copy pathvalidate-libraries.ts
More file actions
81 lines (66 loc) · 2.86 KB
/
validate-libraries.ts
File metadata and controls
81 lines (66 loc) · 2.86 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
import { fillNpmName } from '~/scripts/helpers';
import { type LibraryType } from '~/types';
import { VALID_ENTRY_KEYS } from '~/util/Constants';
import libraries from '../react-native-libraries.json';
const GITHUB_URL_PATTERN = /^https:\/\/github\.com\/[\w.-]+\/[\w.-]+(\/tree\/[\w-\\/.%@]+)?$/g;
function validateLibrariesFormat(libraries: LibraryType[]) {
console.log('🔍️Checking all libraries have the correct format');
// Reduces the libraries array to an object of errors for each library
const errorsList = libraries.reduce<Record<string, string[]>>((errors, library) => {
const libraryErrors = [];
const libraryProperties = Object.keys(library);
const libraryWithNpmName = fillNpmName(library);
// Check that it has the githubUrl property and that it is in the correct format
if (!libraryProperties.includes('githubUrl')) {
libraryErrors.push(`- Must contain a 'githubUrl' property`);
} else if (!GITHUB_URL_PATTERN.test(library.githubUrl)) {
libraryErrors.push(
`- The 'githubUrl' of ${library.githubUrl} must be in the format:\nhttps://github.com/owner/repo-name or https://github.com/owner/repo-name/tree/default-branch/name for monorepos`
);
}
const invalidKeys = libraryProperties.filter(key => !VALID_ENTRY_KEYS.has(key));
if (invalidKeys.length > 0) {
invalidKeys.forEach(key => {
libraryErrors.push(`- Uses invalid key - ${key}`);
});
}
// If there were errors, add them to the object
if (libraryErrors.length > 0) {
errors[libraryWithNpmName.npmPkg] = libraryErrors;
}
return errors;
}, {});
if (Object.keys(errorsList).length > 0) {
const errorDescriptions = Object.entries(errorsList).map(
([npmPkg, libraryErrors], index) =>
`Library entry for '${npmPkg}' contains errors:\n${libraryErrors.join('\n')}`
);
console.error('❌ Malformed libraries found:\n' + errorDescriptions.join('\n'));
process.exit(1);
} else {
console.log('✅ No malformed libraries\n');
}
}
function validateDuplicateLibraries(libraries: LibraryType[]) {
console.log('🔍️Checking for duplicate libraries');
const librariesName = libraries.map(
library => library.npmPkg ?? library.githubUrl.split('/').at(-1)
);
const occurrences = librariesName.reduce<Record<string, number>>((acc, item) => {
acc[item] = (acc[item] ?? 0) + 1;
return acc;
}, {});
const duplicateLibraries = [...new Set(librariesName.filter(item => occurrences[item] !== 1))];
if (duplicateLibraries.length > 0) {
console.error(
'❌ Duplicate libraries found:\n' +
duplicateLibraries.map(library => `- ${library}`).join('\n')
);
console.error('Remove these duplicates before commiting.');
process.exit(1);
} else {
console.log('✅ No duplicates');
}
}
validateLibrariesFormat(libraries);
validateDuplicateLibraries(libraries);