Skip to content

Commit fbe2e97

Browse files
author
Samuel Bodin
authored
feat(ts): use jsdelivr to check for d.ts (#645)
1 parent dd386d2 commit fbe2e97

File tree

13 files changed

+183
-281
lines changed

13 files changed

+183
-281
lines changed

src/@types/pkg.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export interface GithubRepo {
2525

2626
export type TsType =
2727
| {
28-
ts: 'included' | false | { possible: true; dtsMain: string };
28+
ts: 'included' | false | { possible: true };
2929
}
3030
| {
3131
ts: 'definitely-typed';

src/__tests__/__snapshots__/formatPkg.test.ts.snap

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ Object {
6565
},
6666
"types": Object {
6767
"ts": Object {
68-
"dtsMain": "index.d.ts",
6968
"possible": true,
7069
},
7170
},
@@ -881,7 +880,6 @@ Are you in trouble? Read through our [contribution guidelines](https://bitbucket
881880
},
882881
"types": Object {
883882
"ts": Object {
884-
"dtsMain": "dist/bundle.d.ts",
885883
"possible": true,
886884
},
887885
},
@@ -1022,7 +1020,6 @@ Object {
10221020
},
10231021
"types": Object {
10241022
"ts": Object {
1025-
"dtsMain": "dist/atomic-package.tab.d.ts",
10261023
"possible": true,
10271024
},
10281025
},
@@ -1154,7 +1151,6 @@ Object {
11541151
},
11551152
"types": Object {
11561153
"ts": Object {
1157-
"dtsMain": "index.d.ts",
11581154
"possible": true,
11591155
},
11601156
},
@@ -1242,7 +1238,6 @@ index(arr, obj);
12421238
},
12431239
"types": Object {
12441240
"ts": Object {
1245-
"dtsMain": "index.d.ts",
12461241
"possible": true,
12471242
},
12481243
},
@@ -1418,7 +1413,6 @@ Thank you so much for contributing!!
14181413
},
14191414
"types": Object {
14201415
"ts": Object {
1421-
"dtsMain": "prism.d.ts",
14221416
"possible": true,
14231417
},
14241418
},
@@ -1520,7 +1514,6 @@ Object {
15201514
"tags": undefined,
15211515
"types": Object {
15221516
"ts": Object {
1523-
"dtsMain": "index.d.ts",
15241517
"possible": true,
15251518
},
15261519
},

src/__tests__/changelog.test.ts

Lines changed: 54 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { getChangelogs, baseUrlMap, getChangelog } from '../changelog';
2-
import * as jsDelivr from '../jsDelivr';
32

43
jest.mock('got', () => {
54
const gotSnapshotUrls = new Set([
@@ -17,12 +16,6 @@ jest.mock('got', () => {
1716
};
1817
});
1918

20-
const spy = jest
21-
.spyOn(jsDelivr, 'getFilesList')
22-
.mockImplementation((): Promise<any[]> => {
23-
return Promise.resolve([]);
24-
});
25-
2619
describe('should test baseUrlMap', () => {
2720
it('should work with paths', () => {
2821
const bitbucketRepo = {
@@ -116,7 +109,7 @@ it('should handle not found changelog for github', async () => {
116109
},
117110
};
118111

119-
const [{ changelogFilename }] = await getChangelogs([pkg]);
112+
const [{ changelogFilename }] = await getChangelogs([pkg], []);
120113
expect(changelogFilename).toBe(null);
121114
});
122115

@@ -135,7 +128,7 @@ it('should get changelog for github', async () => {
135128
},
136129
};
137130

138-
const [{ changelogFilename }] = await getChangelogs([pkg]);
131+
const [{ changelogFilename }] = await getChangelogs([pkg], []);
139132
expect(changelogFilename).toBe(
140133
'https://raw.githubusercontent.com/algolia/algoliasearch-netlify/master/CHANGELOG.md'
141134
);
@@ -148,7 +141,7 @@ it('should get changelog from unpkg if there is no repository field', async () =
148141
repository: null,
149142
};
150143

151-
const [{ changelogFilename }] = await getChangelogs([pkg]);
144+
const [{ changelogFilename }] = await getChangelogs([pkg], []);
152145

153146
expect(changelogFilename).toBe(
154147
'https://unpkg.com/@atlaskit/button@13.3.7/CHANGELOG.md'
@@ -170,7 +163,7 @@ it('should get changelog for gitlab', async () => {
170163
},
171164
};
172165

173-
const [{ changelogFilename }] = await getChangelogs([pkg]);
166+
const [{ changelogFilename }] = await getChangelogs([pkg], []);
174167
expect(changelogFilename).toBe(
175168
'https://gitlab.com/janslow/gitlab-fetch/raw/master/CHANGELOG.md'
176169
);
@@ -191,7 +184,7 @@ it('should get changelog for bitbucket', async () => {
191184
},
192185
};
193186

194-
const [{ changelogFilename }] = await getChangelogs([pkg]);
187+
const [{ changelogFilename }] = await getChangelogs([pkg], []);
195188
expect(changelogFilename).toBe(
196189
'https://bitbucket.org/atlassian/aui/raw/master/changelog.md'
197190
);
@@ -212,81 +205,77 @@ it('should work with HISTORY.md', async () => {
212205
},
213206
};
214207

215-
const [{ changelogFilename }] = await getChangelogs([pkg]);
208+
const [{ changelogFilename }] = await getChangelogs([pkg], []);
216209
expect(changelogFilename).toBe(
217210
'https://raw.githubusercontent.com/expressjs/body-parser/master/HISTORY.md'
218211
);
219212
});
220213

221214
describe('jsDelivr', () => {
222215
it('should early return when finding changelog', async () => {
223-
spy.mockResolvedValue([
224-
{ name: '/package.json', hash: '', time: '1', size: 1 },
225-
{ name: '/CHANGELOG.md', hash: '', time: '1', size: 1 },
226-
]);
227-
228-
const { changelogFilename } = await getChangelog({
229-
name: 'foo',
230-
version: '1.0.0',
231-
repository: {
232-
url: '',
233-
host: 'github.com',
234-
user: 'expressjs',
235-
project: 'body-parser',
236-
path: '',
237-
head: 'master',
238-
branch: 'master',
216+
const { changelogFilename } = await getChangelog(
217+
{
218+
name: 'foo',
219+
version: '1.0.0',
220+
repository: {
221+
url: '',
222+
host: 'github.com',
223+
user: 'expressjs',
224+
project: 'body-parser',
225+
path: '',
226+
head: 'master',
227+
branch: 'master',
228+
},
239229
},
240-
});
241-
expect(jsDelivr.getFilesList).toHaveBeenCalled();
230+
[
231+
{ name: '/package.json', hash: '', time: '1', size: 1 },
232+
{ name: '/CHANGELOG.md', hash: '', time: '1', size: 1 },
233+
]
234+
);
242235
expect(changelogFilename).toEqual(
243236
'https://cdn.jsdelivr.net/npm/foo@1.0.0/CHANGELOG.md'
244237
);
245238
});
246239

247240
it('should early return when finding changelog in nested file', async () => {
248-
spy.mockResolvedValue([
249-
{ name: '/pkg/CHANGELOG.md', hash: '', time: '1', size: 1 },
250-
]);
251-
252-
const { changelogFilename } = await getChangelog({
253-
name: 'foo',
254-
version: '1.0.0',
255-
repository: {
256-
url: '',
257-
host: 'github.com',
258-
user: 'expressjs',
259-
project: 'body-parser',
260-
path: '',
261-
head: 'master',
262-
branch: 'master',
241+
const { changelogFilename } = await getChangelog(
242+
{
243+
name: 'foo',
244+
version: '1.0.0',
245+
repository: {
246+
url: '',
247+
host: 'github.com',
248+
user: 'expressjs',
249+
project: 'body-parser',
250+
path: '',
251+
head: 'master',
252+
branch: 'master',
253+
},
263254
},
264-
});
265-
expect(jsDelivr.getFilesList).toHaveBeenCalled();
255+
[{ name: '/pkg/CHANGELOG.md', hash: '', time: '1', size: 1 }]
256+
);
266257
expect(changelogFilename).toEqual(
267258
'https://cdn.jsdelivr.net/npm/foo@1.0.0/pkg/CHANGELOG.md'
268259
);
269260
});
270261

271262
it('should not register a file looking like a changelog', async () => {
272-
spy.mockResolvedValue([
273-
{ name: '/dist/changelog.js', hash: '', time: '1', size: 1 },
274-
]);
275-
276-
const { changelogFilename } = await getChangelog({
277-
name: 'foo',
278-
version: '1.0.0',
279-
repository: {
280-
url: '',
281-
host: 'github.com',
282-
user: 'hello',
283-
project: 'foo',
284-
path: '',
285-
head: 'master',
286-
branch: 'master',
263+
const { changelogFilename } = await getChangelog(
264+
{
265+
name: 'foo',
266+
version: '1.0.0',
267+
repository: {
268+
url: '',
269+
host: 'github.com',
270+
user: 'hello',
271+
project: 'foo',
272+
path: '',
273+
head: 'master',
274+
branch: 'master',
275+
},
287276
},
288-
});
289-
expect(jsDelivr.getFilesList).toHaveBeenCalled();
277+
[{ name: '/dist/changelog.js', hash: '', time: '1', size: 1 }]
278+
);
290279
expect(changelogFilename).toEqual(null);
291280
});
292281
});

src/__tests__/formatPkg.test.ts

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -245,41 +245,6 @@ describe('adds TypeScript information', () => {
245245
})
246246
).toEqual(expect.objectContaining({ types: { ts: 'included' } }));
247247
});
248-
249-
it('adds types possible if we can find a main file', () => {
250-
expect(
251-
formatPkg({
252-
...BASE,
253-
name: 'xxx',
254-
main: 'main.js',
255-
})
256-
).toEqual(
257-
expect.objectContaining({
258-
types: { ts: { possible: true, dtsMain: 'main.d.ts' } },
259-
})
260-
);
261-
262-
expect(
263-
formatPkg({
264-
...BASE,
265-
name: 'xxx',
266-
})
267-
).toEqual(
268-
expect.objectContaining({
269-
types: { ts: { possible: true, dtsMain: 'index.d.ts' } },
270-
})
271-
);
272-
});
273-
274-
it('gives up when no main is not js', () => {
275-
expect(
276-
formatPkg({
277-
...BASE,
278-
name: 'xxx',
279-
main: 'shell-script.sh',
280-
})
281-
).toEqual(expect.objectContaining({ types: { ts: false } }));
282-
});
283248
});
284249

285250
describe('getRepositoryInfo', () => {

src/changelog.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -95,23 +95,25 @@ async function raceFromPaths(files: string[]): Promise<{
9595
}
9696

9797
export async function getChangelog(
98-
pkg: Pick<RawPkg, 'repository' | 'name' | 'version'>
98+
pkg: Pick<RawPkg, 'repository' | 'name' | 'version'>,
99+
filelist: jsDelivr.File[]
99100
): Promise<{
100101
changelogFilename: string | null;
101102
}> {
102-
// Do a quick call to jsDelivr
103-
// Only work if the package has published their changelog along with the code
104-
const filesList = await jsDelivr.getFilesList(pkg);
105-
for (const file of filesList) {
103+
for (const file of filelist) {
106104
const name = path.basename(file.name);
107105
if (!fileRegex.test(name)) {
108106
// eslint-disable-next-line no-continue
109107
continue;
110108
}
111109

110+
datadog.increment('jsdelivr.getChangelog.hit');
111+
112112
return { changelogFilename: jsDelivr.getFullURL(pkg, file) };
113113
}
114114

115+
datadog.increment('jsdelivr.getChangelog.miss');
116+
115117
const { repository, name, version } = pkg;
116118

117119
// Rollback to brute-force the source code
@@ -120,19 +122,19 @@ export async function getChangelog(
120122
);
121123

122124
if (repository === null) {
123-
return raceFromPaths(unpkgFiles);
125+
return await raceFromPaths(unpkgFiles);
124126
}
125127

126128
const user = repository.user || '';
127129
const project = repository.project || '';
128130
const host = repository.host || '';
129131
if (user.length < 1 || project.length < 1) {
130-
return raceFromPaths(unpkgFiles);
132+
return await raceFromPaths(unpkgFiles);
131133
}
132134

133135
// Check if we know how to handle this host
134136
if (!baseUrlMap.has(host)) {
135-
return raceFromPaths(unpkgFiles);
137+
return await raceFromPaths(unpkgFiles);
136138
}
137139

138140
const baseUrl = baseUrlMap.get(host)!(repository);
@@ -141,19 +143,24 @@ export async function getChangelog(
141143
[baseUrl.replace(/\/$/, ''), file].join('/')
142144
);
143145

144-
return raceFromPaths([...files, ...unpkgFiles]);
146+
return await raceFromPaths([...files, ...unpkgFiles]);
145147
}
146148

147149
export async function getChangelogs(
148-
pkgs: Array<Pick<RawPkg, 'repository' | 'name' | 'version'>>
150+
pkgs: Array<Pick<RawPkg, 'repository' | 'name' | 'version'>>,
151+
filelists: jsDelivr.File[][]
149152
): Promise<
150153
Array<{
151154
changelogFilename: string | null;
152155
}>
153156
> {
154157
const start = Date.now();
155158

156-
const all = await Promise.all(pkgs.map(getChangelog));
159+
const all = await Promise.all(
160+
pkgs.map((pkg, index) => {
161+
return getChangelog(pkg, filelists[index] || []);
162+
})
163+
);
157164

158165
datadog.timing('changelogs.getChangelogs', Date.now() - start);
159166
return all;

0 commit comments

Comments
 (0)