Skip to content

Commit 47fab57

Browse files
committed
Use cached npm info
1 parent 8861750 commit 47fab57

File tree

5 files changed

+41
-23
lines changed

5 files changed

+41
-23
lines changed

packages/publisher/src/generate-packages.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import {
1919
Logger,
2020
writeTgz,
2121
withNpmCache,
22-
UncachedNpmInfoClient,
2322
CachedNpmInfoClient,
2423
} from "@definitelytyped/utils";
2524
import {
@@ -71,11 +70,11 @@ export default async function generatePackages(
7170
}
7271
log("## Generating deprecated packages");
7372
await withNpmCache(
74-
new UncachedNpmInfoClient(),
75-
async (client) => {
73+
undefined,
74+
async (offline) => {
7675
for (const pkg of changedPackages.changedNotNeededPackages) {
7776
log(` * ${pkg.libraryName}`);
78-
await generateNotNeededPackage(pkg, client, log);
77+
await generateNotNeededPackage(pkg, offline, log);
7978
}
8079
},
8180
cacheDirPath
@@ -102,11 +101,11 @@ async function generateTypingPackage(
102101

103102
async function generateNotNeededPackage(
104103
pkg: NotNeededPackage,
105-
client: CachedNpmInfoClient,
104+
offline: Omit<CachedNpmInfoClient, "fetchAndCacheNpmInfo">,
106105
log: Logger
107106
): Promise<void> {
108-
pkg = skipBadPublishes(pkg, client, log);
109-
const info = await client.fetchAndCacheNpmInfo(pkg.libraryName);
107+
pkg = skipBadPublishes(pkg, offline, log);
108+
const info = offline.getNpmInfoFromCache(pkg.libraryName);
110109
assert(info);
111110
const readme = `This is a stub types definition for ${getFullNpmName(pkg.name)} (${info.homepage}).\n
112111
${pkg.libraryName} provides its own type definitions, so you don't need ${getFullNpmName(pkg.name)} installed!`;

packages/publisher/src/lib/npm.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@ import * as semver from "semver";
77
* So the keys of 'time' give the actual 'latest'.
88
* If that's not equal to the expected latest, try again by bumping the patch version of the last attempt by 1.
99
*/
10-
export function skipBadPublishes(pkg: NotNeededPackage, client: CachedNpmInfoClient, log: Logger) {
10+
export function skipBadPublishes(
11+
pkg: NotNeededPackage,
12+
offline: Omit<CachedNpmInfoClient, "fetchAndCacheNpmInfo">,
13+
log: Logger
14+
) {
1115
// because this is called right after isAlreadyDeprecated, we can rely on the cache being up-to-date
12-
const info = assertDefined(client.getNpmInfoFromCache(pkg.fullNpmName));
16+
const info = assertDefined(offline.getNpmInfoFromCache(pkg.fullNpmName));
1317
const notNeeded = pkg.version;
1418
const latest = new semver.SemVer(findActualLatest(info.time));
1519
if (semver.lte(notNeeded, latest)) {

packages/publisher/src/publish-packages.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import {
1212
writeLog,
1313
NpmPublishClient,
1414
withNpmCache,
15-
UncachedNpmInfoClient,
1615
} from "@definitelytyped/utils";
1716
import { readChangedPackages, ChangedPackages } from "./lib/versions";
1817
import { skipBadPublishes } from "./lib/npm";
@@ -144,10 +143,10 @@ export default async function publishPackages(
144143
}
145144

146145
await withNpmCache(
147-
new UncachedNpmInfoClient(),
148-
async (infoClient) => {
146+
undefined,
147+
async (offline) => {
149148
for (const n of changedPackages.changedNotNeededPackages) {
150-
const target = skipBadPublishes(n, infoClient, log);
149+
const target = skipBadPublishes(n, offline, log);
151150
await publishNotNeededPackage(client, target, dry, log);
152151
}
153152
},

packages/publisher/src/publish-registry.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ export default async function publishRegistry(
7070

7171
// Don't include not-needed packages in the registry.
7272
const registryJsonData = await withNpmCache(
73-
client,
74-
(cachedClient) => generateRegistry(allPackages.allLatestTypings(), cachedClient),
73+
undefined,
74+
(offline) => generateRegistry(allPackages.allLatestTypings(), offline),
7575
cacheDirPath
7676
);
7777
const registry = JSON.stringify(registryJsonData);
@@ -249,13 +249,16 @@ interface Registry {
249249
};
250250
};
251251
}
252-
async function generateRegistry(typings: readonly TypingsData[], client: CachedNpmInfoClient): Promise<Registry> {
252+
async function generateRegistry(
253+
typings: readonly TypingsData[],
254+
offline: Omit<CachedNpmInfoClient, "fetchAndCacheNpmInfo">
255+
): Promise<Registry> {
253256
const entries: { [packageName: string]: { [distTags: string]: string } } = {};
254257
for (const typing of typings) {
255258
// Unconditionally use cached info, this should have been set in calculate-versions so should be recent enough.
256-
const info = client.getNpmInfoFromCache(typing.fullNpmName);
259+
const info = offline.getNpmInfoFromCache(typing.fullNpmName);
257260
if (!info) {
258-
const missings = typings.filter((t) => !client.getNpmInfoFromCache(t.fullNpmName)).map((t) => t.fullNpmName);
261+
const missings = typings.filter((t) => !offline.getNpmInfoFromCache(t.fullNpmName)).map((t) => t.fullNpmName);
259262
throw new Error(`${missings.toString()} not found in cached npm info.`);
260263
}
261264
entries[typing.name] = filterTags(info.distTags);

packages/utils/src/npm.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,17 @@ export interface CachedNpmInfoClient {
5050

5151
export async function withNpmCache<T>(
5252
uncachedClient: UncachedNpmInfoClient,
53-
cb: (client: CachedNpmInfoClient) => Promise<T>,
53+
cb: (client: CachedNpmInfoClient) => T,
54+
cacheDir?: string
55+
): Promise<T>;
56+
export async function withNpmCache<T>(
57+
uncachedClient: undefined,
58+
cb: (offline: Omit<CachedNpmInfoClient, "fetchAndCacheNpmInfo">) => T,
59+
cacheDir?: string
60+
): Promise<T>;
61+
export async function withNpmCache<T>(
62+
uncachedClient: UncachedNpmInfoClient | undefined,
63+
cb: (client: CachedNpmInfoClient) => T,
5464
cacheDir = defaultCacheDir
5565
): Promise<T> {
5666
const log = loggerWithErrors()[0];
@@ -69,9 +79,12 @@ export async function withNpmCache<T>(
6979
}
7080

7181
const res = await cb({ getNpmInfoFromCache, fetchAndCacheNpmInfo });
72-
log.info("Writing npm cache.");
73-
await ensureFile(cacheFile);
74-
await writeJson(cacheFile, mapToRecord(unroll, jsonFromNpmInfo));
82+
// Don't bother writing if there's no way we could have gone to the origin.
83+
if (uncachedClient) {
84+
log.info("Writing npm cache.");
85+
await ensureFile(cacheFile);
86+
await writeJson(cacheFile, mapToRecord(unroll, jsonFromNpmInfo));
87+
}
7588
return res;
7689

7790
/** May return old info -- caller should check that this looks up-to-date. */
@@ -81,7 +94,7 @@ export async function withNpmCache<T>(
8194

8295
/** Call this when the result of getNpmInfoFromCache looks potentially out-of-date. */
8396
async function fetchAndCacheNpmInfo(packageName: string): Promise<NpmInfo | undefined> {
84-
const info = await uncachedClient.fetchNpmInfo(packageName);
97+
const info = await uncachedClient!.fetchNpmInfo(packageName);
8598
if (info) {
8699
unroll.set(packageName, info);
87100
}

0 commit comments

Comments
 (0)