@@ -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 ;
@@ -64,37 +65,55 @@ async function computeChangedPackages(allPackages: AllPackages, log: LoggerWithE
6465 } ) ;
6566 log . info ( "# Computing deprecated packages..." ) ;
6667 const changedNotNeededPackages = await mapDefinedAsync ( allPackages . allNotNeeded ( ) , async ( pkg ) => {
67- if ( ! ( await isAlreadyDeprecated ( pkg , log ) ) ) {
68+ const incipientVersion = await fetchIncipientStubVersion ( pkg , log ) ;
69+ if ( incipientVersion ) {
6870 await pacote . manifest ( pkg . libraryName , { cache : defaultCacheDir } ) . catch ( ( reason ) => {
6971 throw reason . code === "E404"
7072 ? new Error ( `To deprecate '@types/${ pkg . name } ', '${ pkg . libraryName } ' must exist on npm.` , { cause : reason } )
7173 : reason ;
7274 } ) ;
7375 log . info ( `To be deprecated: ${ pkg . name } ` ) ;
74- return pkg ;
76+ return { pkg, version : incipientVersion } ;
7577 }
7678 return undefined ;
7779 } ) ;
7880 return { changedTypings, changedNotNeededPackages } ;
7981}
8082
81- async function isAlreadyDeprecated ( pkg : NotNeededPackage , log : LoggerWithErrors ) : Promise < unknown > {
82- const offline = await pacote
83- . manifest ( pkg . fullNpmName , {
83+ /**
84+ * Return the version of the stub @types package we're about to publish and deprecate, if we haven't already deprecated that package.
85+ * 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.
86+ */
87+ export async function fetchIncipientStubVersion ( pkg : NotNeededPackage , log : LoggerWithErrors ) : Promise < string | false > {
88+ const packument = await revalidate ( ) ;
89+ return (
90+ ! packument . deprecated &&
91+ String (
92+ semver . maxSatisfying (
93+ [ pkg . version , semver . inc ( semver . maxSatisfying ( Object . keys ( packument . versions ) , "*" ) ! , "patch" ) ! ] ,
94+ "*"
95+ )
96+ )
97+ ) ;
98+
99+ async function revalidate ( ) {
100+ const offline = await pacote
101+ . packument ( pkg . fullNpmName , {
102+ cache : defaultCacheDir ,
103+ fullMetadata : true ,
104+ offline : true ,
105+ } )
106+ . catch ( ( reason ) => {
107+ if ( reason . code !== "ENOTCACHED" ) throw reason ;
108+ return undefined ;
109+ } ) ;
110+ if ( offline ?. deprecated ) return offline ;
111+ log . info ( `Version info not cached for deprecated package ${ pkg . desc } ` ) ;
112+ const online = await pacote . packument ( pkg . fullNpmName , {
84113 cache : defaultCacheDir ,
85114 fullMetadata : true ,
86- offline : true ,
87- } )
88- . catch ( ( reason ) => {
89- if ( reason . code !== "ENOTCACHED" ) throw reason ;
90- return undefined ;
115+ preferOnline : true ,
91116 } ) ;
92- if ( offline ?. deprecated ) return offline . deprecated ;
93- log . info ( `Version info not cached for deprecated package ${ pkg . desc } ` ) ;
94- const online = await pacote . manifest ( pkg . fullNpmName , {
95- cache : defaultCacheDir ,
96- fullMetadata : true ,
97- preferOnline : true ,
98- } ) ;
99- return online . deprecated ;
117+ return online ;
118+ }
100119}
0 commit comments