6
6
// ones which have changed.
7
7
8
8
import * as fs from "fs" ;
9
- import { join , dirname } from "path" ;
9
+ import { join , dirname , basename } from "path" ;
10
10
import { fileURLToPath } from "url" ;
11
- import fetch from "node-fetch" ;
12
- import { spawnSync } from "child_process" ;
11
+ import { spawnSync , execSync } from "child_process" ;
13
12
import { Octokit } from "@octokit/core" ;
14
13
import printDiff from "print-diff" ;
15
14
import { generateChangelogFrom } from "../lib/changelog.js" ;
15
+ import { packages } from "./createTypesPackages.mjs" ;
16
16
17
17
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
18
18
// @ts -ignore
@@ -27,6 +27,9 @@ const verify = () => {
27
27
) ;
28
28
} ;
29
29
30
+ const gitShowFile = ( commitish , path ) =>
31
+ execSync ( `git show "${ commitish } ":${ path } ` , { encoding : "utf-8" } ) ;
32
+
30
33
const go = async ( ) => {
31
34
verify ( ) ;
32
35
@@ -41,6 +44,10 @@ const go = async () => {
41
44
const newTSConfig = fs . readFileSync ( localPackageJSONPath , "utf-8" ) ;
42
45
const pkgJSON = JSON . parse ( newTSConfig ) ;
43
46
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
+
44
51
const dtsFiles = fs
45
52
. readdirSync ( join ( generatedDir , dirName ) )
46
53
. filter ( ( f ) => f . endsWith ( ".d.ts" ) ) ;
@@ -52,25 +59,36 @@ const go = async () => {
52
59
// determine if anything has changed
53
60
let upload = false ;
54
61
for ( const file of dtsFiles ) {
62
+ const originalFilename = basename (
63
+ thisPackageMeta . files . find ( ( f ) => f . to === file ) . from
64
+ ) ;
65
+
55
66
const generatedDTSPath = join ( generatedDir , dirName , file ) ;
56
67
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
+
58
74
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 ) ;
63
81
64
82
const title = `\n## \`${ file } \`\n` ;
65
- const notes = generateChangelogFrom ( npmDTSText , generatedDTSContent ) ;
83
+ const notes = generateChangelogFrom ( oldFile , generatedDTSContent ) ;
66
84
releaseNotes . push ( title ) ;
67
85
releaseNotes . push ( notes . trim ( ) === "" ? "No changes" : notes ) ;
68
86
69
- upload = upload || npmDTSText !== generatedDTSContent ;
87
+ upload = upload || oldFile !== generatedDTSContent ;
70
88
} catch ( error ) {
71
89
// Could not find a previous build
72
90
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 } .
74
92
Assuming that this means we need to upload this package.` ) ;
75
93
upload = true ;
76
94
}
0 commit comments