@@ -11,6 +11,7 @@ import {
11
11
} from "@definitelytyped/utils" ;
12
12
import { fetchTypesPackageVersionInfo } from "@definitelytyped/retag" ;
13
13
import * as pacote from "pacote" ;
14
+ import * as semver from "semver" ;
14
15
15
16
if ( ! module . parent ) {
16
17
const log = loggerWithErrors ( ) [ 0 ] ;
@@ -31,9 +32,9 @@ async function computeAndSaveChangedPackages(
31
32
const cp = await computeChangedPackages ( allPackages , log ) ;
32
33
const json : ChangedPackagesJson = {
33
34
changedTypings : cp . changedTypings . map (
34
- ( { pkg : { id } , version, latestVersion } ) : ChangedTypingJson => ( { id , version, latestVersion } )
35
+ ( { pkg : { name } , version, latestVersion } ) : ChangedTypingJson => ( { name , version, latestVersion } )
35
36
) ,
36
- changedNotNeededPackages : cp . changedNotNeededPackages . map ( ( p ) => p . name ) ,
37
+ changedNotNeededPackages : cp . changedNotNeededPackages . map ( ( { pkg : { name } , version } ) => ( { name, version } ) ) ,
37
38
} ;
38
39
await writeDataFile ( versionsFilename , json ) ;
39
40
return cp ;
@@ -66,7 +67,8 @@ async function computeChangedPackages(allPackages: AllPackages, log: LoggerWithE
66
67
} ) ;
67
68
log . info ( "# Computing deprecated packages..." ) ;
68
69
const changedNotNeededPackages = await mapDefinedAsync ( allPackages . allNotNeeded ( ) , async ( pkg ) => {
69
- if ( ! ( await isAlreadyDeprecated ( pkg , log ) ) ) {
70
+ const incipientVersion = await fetchIncipientStubVersion ( pkg , log ) ;
71
+ if ( incipientVersion ) {
70
72
// Assert that dependencies (i.e. the replacement package) exist on npm.
71
73
// Also checked in checkNotNeededPackage().
72
74
await pacote . manifest ( pkg . libraryName , { cache : defaultCacheDir } ) . catch ( ( reason ) => {
@@ -75,20 +77,39 @@ async function computeChangedPackages(allPackages: AllPackages, log: LoggerWithE
75
77
: reason ;
76
78
} ) ;
77
79
log . info ( `To be deprecated: ${ pkg . name } ` ) ;
78
- return pkg ;
80
+ return { pkg, version : incipientVersion } ;
79
81
}
80
82
return undefined ;
81
83
} ) ;
82
84
return { changedTypings, changedNotNeededPackages } ;
83
85
}
84
86
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
+ }
94
115
}
0 commit comments