make retag export core functions

This commit is contained in:
Nathan Shively-Sanders
2020-05-04 17:04:19 -07:00
parent 843bcba3d2
commit 3f5696d6a7
8 changed files with 124 additions and 126 deletions

View File

@@ -8,6 +8,7 @@
"dependencies": {
"@definitelytyped/definitions-parser": "^0.0.30-next.2",
"@definitelytyped/header-parser": "^0.0.30-next.2",
"@definitelytyped/retag": "^0.0.30-next.2",
"@definitelytyped/typescript-versions": "^0.0.30-next.0",
"@definitelytyped/utils": "^0.0.30-next.2",
"@octokit/rest": "^16.1.0",

View File

@@ -1,29 +1,18 @@
import assert = require("assert");
import { defaultLocalOptions } from "./lib/common";
import { ChangedPackages, ChangedPackagesJson, ChangedTypingJson, versionsFilename } from "./lib/versions";
import {
getDefinitelyTyped,
AllPackages,
TypingsData,
NotNeededPackage,
writeDataFile
} from "@definitelytyped/definitions-parser";
import { getDefinitelyTyped, AllPackages, NotNeededPackage, writeDataFile } from "@definitelytyped/definitions-parser";
import {
assertDefined,
best,
logUncaughtErrors,
mapDefined,
mapDefinedAsync,
logUncaughtErrors,
loggerWithErrors,
FS,
LoggerWithErrors,
Semver,
UncachedNpmInfoClient,
withNpmCache,
CachedNpmInfoClient,
NpmInfoVersion
CachedNpmInfoClient
} from "@definitelytyped/utils";
import { fetchTypesPackageVersionInfo } from "@definitelytyped/retag";
import { cacheDirPath } from "./lib/settings";
if (!module.parent) {
@@ -104,47 +93,6 @@ async function computeChangedPackages(
return { changedTypings, changedNotNeededPackages };
}
async function fetchTypesPackageVersionInfo(
pkg: TypingsData,
client: CachedNpmInfoClient,
canPublish: boolean,
log?: LoggerWithErrors
): Promise<{ version: string; needsPublish: boolean }> {
let info = client.getNpmInfoFromCache(pkg.fullEscapedNpmName);
let latestVersion = info && getHighestVersionForMajor(info.versions, pkg);
let latestVersionInfo = latestVersion && assertDefined(info!.versions.get(latestVersion.versionString));
if (!latestVersionInfo || latestVersionInfo.typesPublisherContentHash !== pkg.contentHash) {
if (log) {
log.info(`Version info not cached for ${pkg.desc}`);
}
info = await client.fetchAndCacheNpmInfo(pkg.fullEscapedNpmName);
latestVersion = info && getHighestVersionForMajor(info.versions, pkg);
latestVersionInfo = latestVersion && assertDefined(info!.versions.get(latestVersion.versionString));
if (!latestVersionInfo) {
return { version: versionString(pkg, /*patch*/ 0), needsPublish: true };
}
}
if (latestVersionInfo.deprecated) {
// https://github.com/DefinitelyTyped/DefinitelyTyped/pull/22306
assert(
pkg.name === "angular-ui-router" || pkg.name === "ui-router-extras",
`Package ${pkg.name} has been deprecated, so we shouldn't have parsed it. Was it re-added?`
);
}
const needsPublish = canPublish && pkg.contentHash !== latestVersionInfo.typesPublisherContentHash;
const patch = needsPublish
? latestVersion!.minor === pkg.minor
? latestVersion!.patch + 1
: 0
: latestVersion!.patch;
return { version: versionString(pkg, patch), needsPublish };
}
function versionString(pkg: TypingsData, patch: number): string {
return new Semver(pkg.major, pkg.minor, patch).versionString;
}
async function isAlreadyDeprecated(
pkg: NotNeededPackage,
client: CachedNpmInfoClient,
@@ -161,32 +109,3 @@ async function isAlreadyDeprecated(
}
return !!latestVersionInfo.deprecated;
}
function getHighestVersionForMajor(
versions: ReadonlyMap<string, NpmInfoVersion>,
{ major, minor }: TypingsData
): Semver | undefined {
const patch = latestPatchMatchingMajorAndMinor(versions.keys(), major, minor);
return patch === undefined ? undefined : new Semver(major, minor, patch);
}
/** Finds the version with matching major/minor with the latest patch version. */
function latestPatchMatchingMajorAndMinor(
versions: Iterable<string>,
newMajor: number,
newMinor: number
): number | undefined {
const versionsWithTypings = mapDefined(versions, v => {
const semver = Semver.tryParse(v);
if (!semver) {
return undefined;
}
const { major, minor, patch } = semver;
return major === newMajor && minor === newMinor ? patch : undefined;
});
return best(versionsWithTypings, (a, b) => a > b);
}
export async function getLatestTypingVersion(pkg: TypingsData, client: CachedNpmInfoClient): Promise<string> {
return (await fetchTypesPackageVersionInfo(pkg, client, /*publish*/ false)).version;
}

View File

@@ -1,3 +1 @@
export { clean } from "./clean";
export { getLatestTypingVersion } from "./calculate-versions";
export { updateLatestTag, updateTypeScriptVersionTags } from "./lib/package-publisher";

View File

@@ -1,7 +1,7 @@
import assert = require("assert");
import { Logger, joinPaths, readFileAndWarn, NpmPublishClient, Registry } from "@definitelytyped/utils";
import { NotNeededPackage, AnyPackage } from "@definitelytyped/definitions-parser";
import { TypeScriptVersion } from "@definitelytyped/typescript-versions";
import { updateTypeScriptVersionTags, updateLatestTag } from "@definitelytyped/retag";
import { ChangedTyping } from "./versions";
import { outputDirectory } from "../util/util";
@@ -69,36 +69,3 @@ export async function deprecateNotNeededPackage(
}
}
export async function updateTypeScriptVersionTags(
pkg: AnyPackage,
version: string,
client: NpmPublishClient,
log: Logger,
dry: boolean
): Promise<void> {
const tags = TypeScriptVersion.tagsToUpdate(pkg.minTypeScriptVersion);
const name = pkg.fullNpmName;
log(`Tag ${name}@${version} as ${JSON.stringify(tags)}`);
if (dry) {
log("(dry) Skip tag");
} else {
for (const tagName of tags) {
await client.tag(name, version, tagName, dry, log);
}
}
}
export async function updateLatestTag(
fullName: string,
version: string,
client: NpmPublishClient,
log: Logger,
dry: boolean
): Promise<void> {
log(` but tag ${fullName}@${version} as "latest"`);
if (dry) {
log(' (dry) Skip move "latest" back to newest version');
} else {
await client.tag(fullName, version, "latest", dry, log);
}
}

View File

@@ -12,6 +12,7 @@
"references": [
{ "path": "../definitions-parser" },
{ "path": "../header-parser" },
{ "path": "../retag" },
{ "path": "../utils" }
]
}

View File

@@ -1,19 +1,28 @@
import assert = require("assert");
import yargs from "yargs";
import process = require("process");
import os = require("os");
import { TypeScriptVersion } from "@definitelytyped/typescript-versions";
import {
Logger,
assertDefined,
withNpmCache,
NpmPublishClient,
UncachedNpmInfoClient,
consoleLogger,
NpmInfoVersion,
logUncaughtErrors,
Semver,
best,
mapDefined,
loggerWithErrors,
LoggerWithErrors,
Registry,
nAtATime
nAtATime,
CachedNpmInfoClient
} from "@definitelytyped/utils";
import { AllPackages, parseDefinitions, getDefinitelyTyped } from "@definitelytyped/definitions-parser";
import { updateLatestTag, updateTypeScriptVersionTags, getLatestTypingVersion } from "@definitelytyped/publisher";
import { AnyPackage, TypingsData, AllPackages, parseDefinitions, getDefinitelyTyped } from "@definitelytyped/definitions-parser";
logUncaughtErrors(main);
@@ -65,3 +74,107 @@ async function tag(dry: boolean, github: boolean, nProcesses: number, name?: str
});
// Don't tag notNeeded packages
}
export async function updateTypeScriptVersionTags(
pkg: AnyPackage,
version: string,
client: NpmPublishClient,
log: Logger,
dry: boolean
): Promise<void> {
const tags = TypeScriptVersion.tagsToUpdate(pkg.minTypeScriptVersion);
const name = pkg.fullNpmName;
log(`Tag ${name}@${version} as ${JSON.stringify(tags)}`);
if (dry) {
log("(dry) Skip tag");
} else {
for (const tagName of tags) {
await client.tag(name, version, tagName, dry, log);
}
}
}
export async function updateLatestTag(
fullName: string,
version: string,
client: NpmPublishClient,
log: Logger,
dry: boolean
): Promise<void> {
log(` but tag ${fullName}@${version} as "latest"`);
if (dry) {
log(' (dry) Skip move "latest" back to newest version');
} else {
await client.tag(fullName, version, "latest", dry, log);
}
}
export async function getLatestTypingVersion(pkg: TypingsData, client: CachedNpmInfoClient): Promise<string> {
return (await fetchTypesPackageVersionInfo(pkg, client, /*publish*/ false)).version;
}
export async function fetchTypesPackageVersionInfo(
pkg: TypingsData,
client: CachedNpmInfoClient,
canPublish: boolean,
log?: LoggerWithErrors
): Promise<{ version: string; needsPublish: boolean }> {
let info = client.getNpmInfoFromCache(pkg.fullEscapedNpmName);
let latestVersion = info && getHighestVersionForMajor(info.versions, pkg);
let latestVersionInfo = latestVersion && assertDefined(info!.versions.get(latestVersion.versionString));
if (!latestVersionInfo || latestVersionInfo.typesPublisherContentHash !== pkg.contentHash) {
if (log) {
log.info(`Version info not cached for ${pkg.desc}`);
}
info = await client.fetchAndCacheNpmInfo(pkg.fullEscapedNpmName);
latestVersion = info && getHighestVersionForMajor(info.versions, pkg);
latestVersionInfo = latestVersion && assertDefined(info!.versions.get(latestVersion.versionString));
if (!latestVersionInfo) {
return { version: versionString(pkg, /*patch*/ 0), needsPublish: true };
}
}
if (latestVersionInfo.deprecated) {
// https://github.com/DefinitelyTyped/DefinitelyTyped/pull/22306
assert(
pkg.name === "angular-ui-router" || pkg.name === "ui-router-extras",
`Package ${pkg.name} has been deprecated, so we shouldn't have parsed it. Was it re-added?`
);
}
const needsPublish = canPublish && pkg.contentHash !== latestVersionInfo.typesPublisherContentHash;
const patch = needsPublish
? latestVersion!.minor === pkg.minor
? latestVersion!.patch + 1
: 0
: latestVersion!.patch;
return { version: versionString(pkg, patch), needsPublish };
}
function versionString(pkg: TypingsData, patch: number): string {
return new Semver(pkg.major, pkg.minor, patch).versionString;
}
function getHighestVersionForMajor(
versions: ReadonlyMap<string, NpmInfoVersion>,
{ major, minor }: TypingsData
): Semver | undefined {
const patch = latestPatchMatchingMajorAndMinor(versions.keys(), major, minor);
return patch === undefined ? undefined : new Semver(major, minor, patch);
}
/** Finds the version with matching major/minor with the latest patch version. */
function latestPatchMatchingMajorAndMinor(
versions: Iterable<string>,
newMajor: number,
newMinor: number
): number | undefined {
const versionsWithTypings = mapDefined(versions, v => {
const semver = Semver.tryParse(v);
if (!semver) {
return undefined;
}
const { major, minor, patch } = semver;
return major === newMajor && minor === newMinor ? patch : undefined;
});
return best(versionsWithTypings, (a, b) => a > b);
}

View File

@@ -19,7 +19,6 @@
},
"dependencies": {
"@definitelytyped/definitions-parser": "^0.0.30-next.2",
"@definitelytyped/publisher": "^0.0.30-next.2",
"@definitelytyped/utils": "^0.0.30-next.2",
"yargs": "^12.0.5"
},

View File

@@ -5,5 +5,5 @@
"outDir": "dist"
},
"include": ["lib"],
"references": [{ "path": "../definitions-parser" }, { "path": "../publisher" }, { "path": "../utils" }]
"references": [{ "path": "../definitions-parser" }, { "path": "../typescript-versions" }, { "path": "../utils" }]
}