Skip to content

Commit d1eb3a8

Browse files
committed
Add fetchTypesPackageVersionInfo() tests
1 parent 4011b70 commit d1eb3a8

File tree

3 files changed

+97
-20
lines changed

3 files changed

+97
-20
lines changed

packages/publisher/src/calculate-versions.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ async function computeAndSaveChangedPackages(
4848
async function computeChangedPackages(allPackages: AllPackages, log: LoggerWithErrors): Promise<ChangedPackages> {
4949
log.info("# Computing changed packages...");
5050
const changedTypings = await mapDefinedAsync(allPackages.allTypings(), async (pkg) => {
51-
const { version, needsPublish } = await fetchTypesPackageVersionInfo(pkg, /*publish*/ true, log);
52-
if (needsPublish) {
53-
log.info(`Need to publish: ${pkg.desc}@${version}`);
51+
const { incipientVersion } = await fetchTypesPackageVersionInfo(pkg, log);
52+
if (incipientVersion) {
53+
log.info(`Need to publish: ${pkg.desc}@${incipientVersion}`);
5454
for (const { name } of pkg.packageJsonDependencies) {
5555
// Assert that dependencies exist on npm.
5656
// Also checked when we install the dependencies, in dtslint-runner.
@@ -65,8 +65,12 @@ async function computeChangedPackages(allPackages: AllPackages, log: LoggerWithE
6565
}
6666
const latestVersion = pkg.isLatest
6767
? undefined
68-
: (await fetchTypesPackageVersionInfo(allPackages.getLatest(pkg), /*publish*/ true)).version;
69-
return { pkg, version, latestVersion };
68+
: await fetchTypesPackageVersionInfo(allPackages.getLatest(pkg), log);
69+
return {
70+
pkg,
71+
version: incipientVersion,
72+
latestVersion: latestVersion?.incipientVersion || latestVersion?.maxVersion,
73+
};
7074
}
7175
return undefined;
7276
});

packages/retag/src/index.ts

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,19 @@ async function tag(dry: boolean, nProcesses: number, name?: string) {
6161
const publishClient = await NpmPublishClient.create(token, {});
6262
if (name) {
6363
const pkg = await AllPackages.readSingle(name);
64-
const version = await getLatestTypingVersion(pkg);
65-
await updateTypeScriptVersionTags(pkg, version, publishClient, consoleLogger.info, dry);
66-
await updateLatestTag(pkg.fullNpmName, version, publishClient, consoleLogger.info, dry);
64+
const { maxVersion } = await fetchTypesPackageVersionInfo(pkg);
65+
if (maxVersion) {
66+
await updateTypeScriptVersionTags(pkg, maxVersion, publishClient, consoleLogger.info, dry);
67+
await updateLatestTag(pkg.fullNpmName, maxVersion, publishClient, consoleLogger.info, dry);
68+
}
6769
} else {
6870
await nAtATime(10, await AllPackages.readLatestTypings(), async (pkg) => {
6971
// Only update tags for the latest version of the package.
70-
const version = await getLatestTypingVersion(pkg);
71-
await updateTypeScriptVersionTags(pkg, version, publishClient, consoleLogger.info, dry);
72-
await updateLatestTag(pkg.fullNpmName, version, publishClient, consoleLogger.info, dry);
72+
const { maxVersion } = await fetchTypesPackageVersionInfo(pkg);
73+
if (maxVersion) {
74+
await updateTypeScriptVersionTags(pkg, maxVersion, publishClient, consoleLogger.info, dry);
75+
await updateLatestTag(pkg.fullNpmName, maxVersion, publishClient, consoleLogger.info, dry);
76+
}
7377
});
7478
}
7579
// Don't tag notNeeded packages
@@ -109,15 +113,16 @@ export async function updateLatestTag(
109113
}
110114
}
111115

112-
export async function getLatestTypingVersion(pkg: TypingsData): Promise<string> {
113-
return (await fetchTypesPackageVersionInfo(pkg, /*publish*/ false)).version;
114-
}
115-
116+
/**
117+
* Used for two purposes: to determine whether a @types package has changed since it was last published, and to get a package's version in the npm registry.
118+
* We ignore whether the cached metadata is fresh or stale: We always revalidate if the content hashes differ (fresh or not) and never revalidate if they match (stale or not).
119+
* Because the decider is the content hash, this isn't applicable to other npm packages.
120+
* Target JS packages and not-needed stubs don't have content hashes.
121+
*/
116122
export async function fetchTypesPackageVersionInfo(
117123
pkg: TypingsData,
118-
canPublish: boolean,
119124
log?: LoggerWithErrors
120-
): Promise<{ version: string; needsPublish: boolean }> {
125+
): Promise<{ maxVersion?: string; incipientVersion?: string }> {
121126
const spec = `${pkg.fullNpmName}@~${pkg.major}.${pkg.minor}`;
122127
let info = await pacote.manifest(spec, { cache: cacheDir, fullMetadata: true, offline: true }).catch((reason) => {
123128
if (reason.code !== "ENOTCACHED" && reason.code !== "ETARGET") throw reason;
@@ -132,7 +137,7 @@ export async function fetchTypesPackageVersionInfo(
132137
return undefined;
133138
});
134139
if (!info) {
135-
return { version: `${pkg.major}.${pkg.minor}.0`, needsPublish: true };
140+
return { incipientVersion: `${pkg.major}.${pkg.minor}.0` };
136141
}
137142
}
138143

@@ -143,6 +148,10 @@ export async function fetchTypesPackageVersionInfo(
143148
`Package ${pkg.name} has been deprecated, so we shouldn't have parsed it. Was it re-added?`
144149
);
145150
}
146-
const needsPublish = canPublish && pkg.contentHash !== info.typesPublisherContentHash;
147-
return { version: needsPublish ? semver.inc(info.version, "patch")! : info.version, needsPublish };
151+
return {
152+
maxVersion: info.version,
153+
...(((pkg.contentHash === info.typesPublisherContentHash) as {}) || {
154+
incipientVersion: semver.inc(info.version, "patch")!,
155+
}),
156+
};
148157
}

packages/retag/test/index.test.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import * as util from "util";
2+
import { TypingsData } from "@definitelytyped/definitions-parser";
3+
import * as pacote from "pacote";
4+
import { fetchTypesPackageVersionInfo } from "../src/";
5+
6+
jest.mock("pacote", () => ({
7+
async manifest(spec: string, opts: pacote.Options) {
8+
switch (spec) {
9+
case "@types/already-published@~1.2": // An already-published @types package.
10+
return { version: "1.2.3", typesPublisherContentHash: "already-published-content-hash" };
11+
case "@types/first-publish@~1.2": // A new, not-yet-published @types package.
12+
// eslint-disable-next-line no-throw-literal
13+
throw { code: opts.offline ? "ENOTCACHED" : "E404" };
14+
}
15+
throw new Error(`Unexpected npm registry fetch: ${util.inspect(spec)}`);
16+
},
17+
}));
18+
19+
const unchanged = new TypingsData(
20+
{
21+
typingsPackageName: "already-published",
22+
libraryMajorVersion: 1,
23+
libraryMinorVersion: 2,
24+
contentHash: "already-published-content-hash",
25+
} as never,
26+
/*isLatest*/ true
27+
);
28+
const changed = new TypingsData(
29+
{
30+
typingsPackageName: "already-published",
31+
libraryMajorVersion: 1,
32+
libraryMinorVersion: 2,
33+
contentHash: "changed-content-hash",
34+
} as never,
35+
/*isLatest*/ true
36+
);
37+
const firstPublish = new TypingsData(
38+
{
39+
typingsPackageName: "first-publish",
40+
libraryMajorVersion: 1,
41+
libraryMinorVersion: 2,
42+
contentHash: "first-publish-content-hash",
43+
} as never,
44+
/*isLatest*/ true
45+
);
46+
47+
test("Increments already-published patch version", () => {
48+
return expect(fetchTypesPackageVersionInfo(changed)).resolves.toEqual({
49+
maxVersion: "1.2.3",
50+
incipientVersion: "1.2.4",
51+
});
52+
});
53+
54+
test("Doesn't increment unchanged @types package version", () => {
55+
return expect(fetchTypesPackageVersionInfo(unchanged)).resolves.toEqual({
56+
maxVersion: "1.2.3",
57+
});
58+
});
59+
60+
test("First-publish version", () => {
61+
return expect(fetchTypesPackageVersionInfo(firstPublish)).resolves.toEqual({
62+
incipientVersion: "1.2.0",
63+
});
64+
});

0 commit comments

Comments
 (0)