@@ -11,6 +11,7 @@ import {
1111} from "@definitelytyped/utils" ;
1212import { fetchTypesPackageVersionInfo } from "@definitelytyped/retag" ;
1313import * as pacote from "pacote" ;
14+ import * as semver from "semver" ;
1415
1516if ( ! module . parent ) {
1617 const log = loggerWithErrors ( ) [ 0 ] ;
@@ -31,9 +32,9 @@ async function computeAndSaveChangedPackages(
3132 const cp = await computeChangedPackages ( allPackages , log ) ;
3233 const json : ChangedPackagesJson = {
3334 changedTypings : cp . changedTypings . map (
34- ( { pkg : { id } , version, latestVersion } ) : ChangedTypingJson => ( { id , version, latestVersion } )
35+ ( { pkg : { name } , version, latestVersion } ) : ChangedTypingJson => ( { name , version, latestVersion } )
3536 ) ,
36- changedNotNeededPackages : cp . changedNotNeededPackages . map ( ( p ) => p . name ) ,
37+ changedNotNeededPackages : cp . changedNotNeededPackages . map ( ( { pkg : { name } , version } ) => ( { name, version } ) ) ,
3738 } ;
3839 await writeDataFile ( versionsFilename , json ) ;
3940 return cp ;
@@ -66,7 +67,8 @@ async function computeChangedPackages(allPackages: AllPackages, log: LoggerWithE
6667 } ) ;
6768 log . info ( "# Computing deprecated packages..." ) ;
6869 const changedNotNeededPackages = await mapDefinedAsync ( allPackages . allNotNeeded ( ) , async ( pkg ) => {
69- if ( ! ( await isAlreadyDeprecated ( pkg , log ) ) ) {
70+ const incipientVersion = await fetchIncipientStubVersion ( pkg , log ) ;
71+ if ( incipientVersion ) {
7072 // Assert that dependencies (i.e. the replacement package) exist on npm.
7173 // Also checked in checkNotNeededPackage().
7274 await pacote . manifest ( pkg . libraryName , { cache : defaultCacheDir } ) . catch ( ( reason ) => {
@@ -75,20 +77,39 @@ async function computeChangedPackages(allPackages: AllPackages, log: LoggerWithE
7577 : reason ;
7678 } ) ;
7779 log . info ( `To be deprecated: ${ pkg . name } ` ) ;
78- return pkg ;
80+ return { pkg, version : incipientVersion } ;
7981 }
8082 return undefined ;
8183 } ) ;
8284 return { changedTypings, changedNotNeededPackages } ;
8385}
8486
85- async function isAlreadyDeprecated ( pkg : NotNeededPackage , log : LoggerWithErrors ) : Promise < unknown > {
86- const offline = await pacote . manifest ( pkg . fullNpmName , { cache : defaultCacheDir , offline : true } ) . catch ( ( reason ) => {
87- if ( reason . code !== "ENOTCACHED" ) throw reason ;
88- return undefined ;
89- } ) ;
90- if ( offline ?. deprecated ) return offline . deprecated ;
91- log . info ( `Version info not cached for deprecated package ${ pkg . desc } ` ) ;
92- const online = await pacote . manifest ( pkg . fullNpmName , { cache : defaultCacheDir , preferOnline : true } ) ;
93- return online . deprecated ;
87+ /**
88+ * Return the version of the stub @types package we're about to publish and deprecate, if we haven't already deprecated that package.
89+ * It's the max of the not-needed version and the max published version + 1, in case we already published a not-needed-versioned stub but failed to deprecate it, for whatever reason.
90+ */
91+ export async function fetchIncipientStubVersion ( pkg : NotNeededPackage , log : LoggerWithErrors ) : Promise < string | false > {
92+ const packument = await revalidate ( ) ;
93+ return (
94+ ! packument . versions [ packument [ "dist-tags" ] . latest ] . deprecated &&
95+ String (
96+ semver . maxSatisfying (
97+ [ pkg . version , semver . inc ( semver . maxSatisfying ( Object . keys ( packument . versions ) , "*" ) ! , "patch" ) ! ] ,
98+ "*"
99+ )
100+ )
101+ ) ;
102+
103+ async function revalidate ( ) {
104+ const offline = await pacote
105+ . packument ( pkg . fullNpmName , { cache : defaultCacheDir , offline : true } )
106+ . catch ( ( reason ) => {
107+ if ( reason . code !== "ENOTCACHED" ) throw reason ;
108+ return undefined ;
109+ } ) ;
110+ if ( offline ?. versions [ offline [ "dist-tags" ] . latest ] . deprecated ) return offline ;
111+ log . info ( `Version info not cached for deprecated package ${ pkg . desc } ` ) ;
112+ const online = await pacote . packument ( pkg . fullNpmName , { cache : defaultCacheDir , preferOnline : true } ) ;
113+ return online ;
114+ }
94115}
0 commit comments