66// ones which have changed.
77
88import * as fs from "fs" ;
9- import { join , dirname } from "path" ;
9+ import { join , dirname , basename } from "path" ;
1010import { fileURLToPath } from "url" ;
11- import fetch from "node-fetch" ;
12- import { spawnSync } from "child_process" ;
11+ import { spawnSync , execSync } from "child_process" ;
1312import { Octokit } from "@octokit/core" ;
1413import printDiff from "print-diff" ;
1514import { generateChangelogFrom } from "../lib/changelog.js" ;
15+ import { packages } from "./createTypesPackages.mjs" ;
1616
1717// eslint-disable-next-line @typescript-eslint/ban-ts-comment
1818// @ts -ignore
@@ -27,6 +27,9 @@ const verify = () => {
2727 ) ;
2828} ;
2929
30+ const gitShowFile = ( commitish , path ) =>
31+ execSync ( `git show "${ commitish } ":${ path } ` , { encoding : "utf-8" } ) ;
32+
3033const go = async ( ) => {
3134 verify ( ) ;
3235
@@ -41,6 +44,10 @@ const go = async () => {
4144 const newTSConfig = fs . readFileSync ( localPackageJSONPath , "utf-8" ) ;
4245 const pkgJSON = JSON . parse ( newTSConfig ) ;
4346
47+ // We'll need to map back from the filename in the npm package to the
48+ // generated file in baselines inside the git tag
49+ const thisPackageMeta = packages . find ( ( p ) => p . name === pkgJSON . name ) ;
50+
4451 const dtsFiles = fs
4552 . readdirSync ( join ( generatedDir , dirName ) )
4653 . filter ( ( f ) => f . endsWith ( ".d.ts" ) ) ;
@@ -52,25 +59,36 @@ const go = async () => {
5259 // determine if anything has changed
5360 let upload = false ;
5461 for ( const file of dtsFiles ) {
62+ const originalFilename = basename (
63+ thisPackageMeta . files . find ( ( f ) => f . to === file ) . from
64+ ) ;
65+
5566 const generatedDTSPath = join ( generatedDir , dirName , file ) ;
5667 const generatedDTSContent = fs . readFileSync ( generatedDTSPath , "utf8" ) ;
57- const unpkgURL = `https://unpkg.com/${ pkgJSON . name } /${ file } ` ;
68+
69+ // This assumes we'll only _ever_ ship patches, which may change in the
70+ // future someday.
71+ const [ maj , min , patch ] = pkgJSON . version . split ( "." ) ;
72+ const olderVersion = `${ maj } .${ min } .${ patch - 1 } ` ;
73+
5874 try {
59- const npmDTSReq = await fetch ( unpkgURL ) ;
60- const npmDTSText = await npmDTSReq . text ( ) ;
61- console . log ( `Comparing ${ file } from unpkg, to generated version:` ) ;
62- printDiff ( npmDTSText , generatedDTSContent ) ;
75+ const oldFile = gitShowFile (
76+ `${ pkgJSON . name } @${ olderVersion } ` ,
77+ `baselines/${ originalFilename } `
78+ ) ;
79+ console . log ( `Comparing ${ file } from ${ olderVersion } , to now:` ) ;
80+ printDiff ( oldFile , generatedDTSContent ) ;
6381
6482 const title = `\n## \`${ file } \`\n` ;
65- const notes = generateChangelogFrom ( npmDTSText , generatedDTSContent ) ;
83+ const notes = generateChangelogFrom ( oldFile , generatedDTSContent ) ;
6684 releaseNotes . push ( title ) ;
6785 releaseNotes . push ( notes . trim ( ) === "" ? "No changes" : notes ) ;
6886
69- upload = upload || npmDTSText !== generatedDTSContent ;
87+ upload = upload || oldFile !== generatedDTSContent ;
7088 } catch ( error ) {
7189 // Could not find a previous build
7290 console . log ( `
73- Could not get the file ${ file } inside the npm package ${ pkgJSON . name } from unpkg at ${ unpkgURL }
91+ Could not get the file ${ file } inside the npm package ${ pkgJSON . name } from tag ${ olderVersion } .
7492Assuming that this means we need to upload this package.` ) ;
7593 upload = true ;
7694 }
0 commit comments