-
Notifications
You must be signed in to change notification settings - Fork 821
Expand file tree
/
Copy pathhelpers.ts
More file actions
84 lines (71 loc) · 2.27 KB
/
helpers.ts
File metadata and controls
84 lines (71 loc) · 2.27 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
82
83
84
import { fetch } from 'bun';
import { type LibraryType } from '~/types';
export const REQUEST_SLEEP = 5000;
const GRAPHQL_API = 'https://api.github.com/graphql';
const AUTHORIZATION = `bearer ${process.env.CI_CHECKS_TOKEN ?? process.env.GH_TOKEN ?? process.env.GITHUB_TOKEN}`;
export async function makeGraphqlQuery(query: string, variables = {}) {
const result = await fetch(GRAPHQL_API, {
method: 'POST',
headers: {
Authorization: AUTHORIZATION,
Accept: 'application/json',
},
body: JSON.stringify({
query,
variables,
}),
});
return await result.json();
}
export async function getUpdatedUrl(url: string) {
try {
const result = await fetch(url);
return result.url;
} catch {
return url;
}
}
export function sleep(ms = 0, msMax: number | undefined = undefined) {
return new Promise(r =>
setTimeout(r, msMax ? Math.floor(Math.random() * (msMax - ms)) + ms : ms)
);
}
export function fillNpmName(library: LibraryType) {
if (!library.npmPkg) {
const parts = library.githubUrl.split('/');
library.npmPkg = parts[parts.length - 1].toLowerCase();
}
return library;
}
const STOPWORDS = ['react-native', 'reactnative', 'react', 'native'];
const STOPWORDS_PATTERN = new RegExp(`\\b(?:${STOPWORDS.join('|')})\\b`, 'gi');
export function processTopics(topics?: string[]) {
if (!Array.isArray(topics)) {
return [];
}
return topics
.map(topic =>
topic
.normalize('NFKC')
.toLowerCase()
.replace(/([ _])/g, '-')
.replace(STOPWORDS_PATTERN, '')
.replace(/-+/g, '-')
.replace(/^-|-$/g, '')
.trim()
)
.filter(topic => topic?.length);
}
function splitAndGetLastChunk(value: string, delimiter = '/') {
return value.split(delimiter).at(-1)?.toLowerCase();
}
export function hasMismatchedPackageData(library: LibraryType) {
const desiredName = library.npmPkg ?? splitAndGetLastChunk(library.githubUrl);
if (library.github.registry && library.github.registry !== 'https://registry.npmjs.org/') {
const registryScope = splitAndGetLastChunk(library.github.registry);
if (registryScope?.startsWith('@')) {
return library.github?.name.replace(`${registryScope}/`, '') !== desiredName;
}
}
return desiredName !== library.github?.name;
}