Skip to content

Commit 62aba6b

Browse files
committed
Simply read the npm cache
1 parent 47fab57 commit 62aba6b

File tree

6 files changed

+36
-76
lines changed

6 files changed

+36
-76
lines changed

packages/publisher/src/generate-packages.ts

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { makeTypesVersionsForPackageJson } from "@definitelytyped/header-parser";
2-
import assert = require("assert");
32
import { emptyDir, mkdir, mkdirp, readFileSync } from "fs-extra";
43
import path = require("path");
54
import yargs = require("yargs");
@@ -17,9 +16,8 @@ import {
1716
writeLog,
1817
writeFile,
1918
Logger,
19+
NpmInfoRaw,
2020
writeTgz,
21-
withNpmCache,
22-
CachedNpmInfoClient,
2321
} from "@definitelytyped/utils";
2422
import {
2523
getDefinitelyTyped,
@@ -69,16 +67,11 @@ export default async function generatePackages(
6967
log(` * ${pkg.desc}`);
7068
}
7169
log("## Generating deprecated packages");
72-
await withNpmCache(
73-
undefined,
74-
async (offline) => {
75-
for (const pkg of changedPackages.changedNotNeededPackages) {
76-
log(` * ${pkg.libraryName}`);
77-
await generateNotNeededPackage(pkg, offline, log);
78-
}
79-
},
80-
cacheDirPath
81-
);
70+
const offline: Record<string, NpmInfoRaw> = await import(`${cacheDirPath}/npmInfo.json`);
71+
for (const pkg of changedPackages.changedNotNeededPackages) {
72+
log(` * ${pkg.libraryName}`);
73+
await generateNotNeededPackage(pkg, offline, log);
74+
}
8275
await writeLog("package-generator.md", logResult());
8376
}
8477
async function generateTypingPackage(
@@ -101,12 +94,11 @@ async function generateTypingPackage(
10194

10295
async function generateNotNeededPackage(
10396
pkg: NotNeededPackage,
104-
offline: Omit<CachedNpmInfoClient, "fetchAndCacheNpmInfo">,
97+
offline: Record<string, NpmInfoRaw>,
10598
log: Logger
10699
): Promise<void> {
107100
pkg = skipBadPublishes(pkg, offline, log);
108-
const info = offline.getNpmInfoFromCache(pkg.libraryName);
109-
assert(info);
101+
const info = offline[pkg.libraryName];
110102
const readme = `This is a stub types definition for ${getFullNpmName(pkg.name)} (${info.homepage}).\n
111103
${pkg.libraryName} provides its own type definitions, so you don't need ${getFullNpmName(pkg.name)} installed!`;
112104
await writeCommonOutputs(pkg, createNotNeededPackageJSON(pkg), readme);

packages/publisher/src/lib/npm.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
import { NotNeededPackage } from "@definitelytyped/definitions-parser";
2-
import { Logger, assertDefined, CachedNpmInfoClient, max } from "@definitelytyped/utils";
2+
import { Logger, NpmInfoRaw, assertDefined, max } from "@definitelytyped/utils";
33
import * as semver from "semver";
44

55
/**
66
* When we fail to publish a deprecated package, it leaves behind an entry in the time property.
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(
11-
pkg: NotNeededPackage,
12-
offline: Omit<CachedNpmInfoClient, "fetchAndCacheNpmInfo">,
13-
log: Logger
14-
) {
10+
export function skipBadPublishes(pkg: NotNeededPackage, offline: Record<string, NpmInfoRaw>, log: Logger) {
1511
// because this is called right after isAlreadyDeprecated, we can rely on the cache being up-to-date
16-
const info = assertDefined(offline.getNpmInfoFromCache(pkg.fullNpmName));
12+
const info = assertDefined(offline[pkg.fullNpmName]);
1713
const notNeeded = pkg.version;
1814
const latest = new semver.SemVer(findActualLatest(info.time));
1915
if (semver.lte(notNeeded, latest)) {
@@ -24,9 +20,9 @@ export function skipBadPublishes(
2420
return pkg;
2521
}
2622

27-
function findActualLatest(times: Map<string, string>) {
23+
function findActualLatest(times: NpmInfoRaw["time"]) {
2824
const actual = max(
29-
[...times].filter(([version]) => version !== "modified" && version !== "created"),
25+
Object.entries(times).filter(([version]) => version !== "modified" && version !== "created"),
3026
([, a], [, b]) => (new Date(a) as never) - (new Date(b) as never)
3127
);
3228
if (!actual) {

packages/publisher/src/publish-packages.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import {
99
logUncaughtErrors,
1010
logger,
1111
Fetcher,
12+
NpmInfoRaw,
1213
writeLog,
1314
NpmPublishClient,
14-
withNpmCache,
1515
} from "@definitelytyped/utils";
1616
import { readChangedPackages, ChangedPackages } from "./lib/versions";
1717
import { skipBadPublishes } from "./lib/npm";
@@ -142,16 +142,11 @@ export default async function publishPackages(
142142
}
143143
}
144144

145-
await withNpmCache(
146-
undefined,
147-
async (offline) => {
148-
for (const n of changedPackages.changedNotNeededPackages) {
149-
const target = skipBadPublishes(n, offline, log);
150-
await publishNotNeededPackage(client, target, dry, log);
151-
}
152-
},
153-
cacheDirPath
154-
);
145+
const offline: Record<string, NpmInfoRaw> = await import(`${cacheDirPath}/npmInfo.json`);
146+
for (const n of changedPackages.changedNotNeededPackages) {
147+
const target = skipBadPublishes(n, offline, log);
148+
await publishNotNeededPackage(client, target, dry, log);
149+
}
155150

156151
await writeLog("publishing.md", logResult());
157152
console.log("Done!");

packages/publisher/src/publish-registry.ts

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,12 @@ import {
2424
writeJson,
2525
writeFile,
2626
Logger,
27+
NpmInfoRaw,
2728
sleep,
2829
npmInstallFlags,
2930
readJson,
3031
UncachedNpmInfoClient,
31-
withNpmCache,
3232
NpmPublishClient,
33-
CachedNpmInfoClient,
3433
isObject,
3534
max,
3635
} from "@definitelytyped/utils";
@@ -69,11 +68,8 @@ export default async function publishRegistry(
6968
assert.strictEqual(npmVersion.minor, 1);
7069

7170
// Don't include not-needed packages in the registry.
72-
const registryJsonData = await withNpmCache(
73-
undefined,
74-
(offline) => generateRegistry(allPackages.allLatestTypings(), offline),
75-
cacheDirPath
76-
);
71+
const offline: Record<string, NpmInfoRaw> = await import(`${cacheDirPath}/npmInfo.json`);
72+
const registryJsonData = await generateRegistry(allPackages.allLatestTypings(), offline);
7773
const registry = JSON.stringify(registryJsonData);
7874
const newContentHash = computeHash(registry);
7975
const newVersion = semver.inc(npmVersion, "patch")!;
@@ -251,30 +247,24 @@ interface Registry {
251247
}
252248
async function generateRegistry(
253249
typings: readonly TypingsData[],
254-
offline: Omit<CachedNpmInfoClient, "fetchAndCacheNpmInfo">
250+
offline: Record<string, NpmInfoRaw>
255251
): Promise<Registry> {
256252
const entries: { [packageName: string]: { [distTags: string]: string } } = {};
257253
for (const typing of typings) {
258254
// Unconditionally use cached info, this should have been set in calculate-versions so should be recent enough.
259-
const info = offline.getNpmInfoFromCache(typing.fullNpmName);
255+
const info = offline[typing.fullNpmName];
260256
if (!info) {
261-
const missings = typings.filter((t) => !offline.getNpmInfoFromCache(t.fullNpmName)).map((t) => t.fullNpmName);
257+
const missings = typings.filter((t) => !(t.fullNpmName in offline)).map((t) => t.fullNpmName);
262258
throw new Error(`${missings.toString()} not found in cached npm info.`);
263259
}
264-
entries[typing.name] = filterTags(info.distTags);
260+
entries[typing.name] = filterTags(info["dist-tags"]);
265261
}
266262
return { entries };
267263

268-
function filterTags(tags: Map<string, string>): { readonly [tag: string]: string } {
269-
const latestTag = "latest";
270-
const latestVersion = tags.get(latestTag);
271-
const out: { [tag: string]: string } = {};
272-
tags.forEach((value, tag) => {
273-
if (tag === latestTag || value !== latestVersion) {
274-
out[tag] = value;
275-
}
276-
});
277-
return out;
264+
function filterTags(tags: NpmInfoRaw["dist-tags"]): { readonly [tag: string]: string } {
265+
return Object.fromEntries(
266+
Object.entries(tags).filter(([tag, version]) => tag === "latest" || version !== tags.latest)
267+
);
278268
}
279269
}
280270

packages/utils/src/npm.ts

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

5151
export async function withNpmCache<T>(
5252
uncachedClient: UncachedNpmInfoClient,
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,
53+
cb: (client: CachedNpmInfoClient) => Promise<T>,
6454
cacheDir = defaultCacheDir
6555
): Promise<T> {
6656
const log = loggerWithErrors()[0];
@@ -79,12 +69,9 @@ export async function withNpmCache<T>(
7969
}
8070

8171
const res = await cb({ getNpmInfoFromCache, fetchAndCacheNpmInfo });
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-
}
72+
log.info("Writing npm cache.");
73+
await ensureFile(cacheFile);
74+
await writeJson(cacheFile, mapToRecord(unroll, jsonFromNpmInfo));
8875
return res;
8976

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

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

tsconfig.base.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"compilerOptions": {
3-
"target": "es2018",
3+
"target": "es2019",
44
"module": "commonjs",
55
"strict": true,
66
"esModuleInterop": true,

0 commit comments

Comments
 (0)