@@ -3,14 +3,19 @@ import nodePath from "path";
33import assembleReleasePlan from "@changesets/assemble-release-plan" ;
44import { parse as parseConfig } from "@changesets/config" ;
55import parseChangeset from "@changesets/parse" ;
6- import { PreState , NewChangeset } from "@changesets/types" ;
7- import { Packages , Tool } from "@manypkg/get-packages" ;
6+ import type {
7+ NewChangeset ,
8+ PreState ,
9+ WrittenConfig ,
10+ PackageJSON as ChangesetPackageJSON ,
11+ } from "@changesets/types" ;
12+ import type { Packages , Tool } from "@manypkg/get-packages" ;
813import { safeLoad } from "js-yaml" ;
914import micromatch from "micromatch" ;
1015import fetch from "node-fetch" ;
11- import { ProbotOctokit } from "probot" ;
16+ import type { ProbotOctokit } from "probot" ;
1217
13- export let getChangedPackages = async ( {
18+ export const getChangedPackages = async ( {
1419 owner,
1520 repo,
1621 ref,
@@ -21,12 +26,12 @@ export let getChangedPackages = async ({
2126 owner : string ;
2227 repo : string ;
2328 ref : string ;
24- changedFiles : string [ ] | Promise < string [ ] > ;
29+ changedFiles : Array < string > | Promise < Array < string > > ;
2530 octokit : InstanceType < typeof ProbotOctokit > ;
2631 installationToken : string ;
2732} ) => {
2833 let hasErrored = false ;
29- let encodedCredentials = Buffer . from ( `x-access-token:${ installationToken } ` ) . toString ( "base64" ) ;
34+ const encodedCredentials = Buffer . from ( `x-access-token:${ installationToken } ` ) . toString ( "base64" ) ;
3035
3136 function fetchFile ( path : string ) {
3237 return fetch ( `https://raw.githubusercontent.com/${ owner } /${ repo } /${ ref } /${ path } ` , {
@@ -36,54 +41,63 @@ export let getChangedPackages = async ({
3641 } ) ;
3742 }
3843
39- function fetchJsonFile ( path : string ) {
40- return fetchFile ( path )
41- . then ( ( x ) => x . json ( ) )
42- . catch ( ( err ) => {
43- hasErrored = true ;
44- console . error ( err ) ;
45- return { } ;
46- } ) ;
44+ async function fetchJsonFile < T > ( path : string ) : Promise < T > {
45+ try {
46+ const x = await fetchFile ( path ) ;
47+ return x . json ( ) as Promise < T > ;
48+ } catch ( error ) {
49+ hasErrored = true ;
50+ console . error ( error ) ;
51+ return { } as Promise < T > ;
52+ }
53+ }
54+
55+ async function fetchTextFile ( path : string ) : Promise < string > {
56+ try {
57+ const x = await fetchFile ( path ) ;
58+ return x . text ( ) ;
59+ } catch ( err ) {
60+ hasErrored = true ;
61+ console . error ( err ) ;
62+ return "" ;
63+ }
4764 }
4865
49- function fetchTextFile ( path : string ) {
50- return fetchFile ( path )
51- . then ( ( x ) => x . text ( ) )
52- . catch ( ( err ) => {
53- hasErrored = true ;
54- console . error ( err ) ;
55- return "" ;
56- } ) ;
66+ interface PackageJSON extends ChangesetPackageJSON {
67+ workspaces ?: Array < string > | { packages : Array < string > } ;
68+ bolt ?: { workspaces : Array < string > } ;
5769 }
5870
59- async function getPackage ( pkgPath : string ) {
60- let jsonContent = await fetchJsonFile ( pkgPath + "/package.json" ) ;
71+ async function getPackage ( pkgPath : string ) : Promise < { dir : string ; packageJson : PackageJSON } > {
72+ const jsonContent = await fetchJsonFile ( pkgPath + "/package.json" ) ;
6173 return {
62- packageJson : jsonContent ,
6374 dir : pkgPath ,
75+ packageJson : jsonContent as PackageJSON ,
6476 } ;
6577 }
6678
67- let rootPackageJsonContentsPromise = fetchJsonFile ( "package.json" ) ;
68- let configPromise : Promise < any > = fetchJsonFile ( ".changeset/config.json" ) ;
79+ const rootPackageJsonContentsPromise : Promise < PackageJSON > = fetchJsonFile ( "package.json" ) ;
80+ const rawConfigPromise : Promise < WrittenConfig > = fetchJsonFile ( ".changeset/config.json" ) ;
6981
70- let tree = await octokit . git . getTree ( {
82+ const tree = await octokit . git . getTree ( {
7183 owner,
7284 repo,
7385 recursive : "1" ,
7486 tree_sha : ref ,
7587 } ) ;
7688
7789 let preStatePromise : Promise < PreState > | undefined ;
78- let changesetPromises : Promise < NewChangeset > [ ] = [ ] ;
79- let potentialWorkspaceDirectories : string [ ] = [ ] ;
90+ const changesetPromises : Array < Promise < NewChangeset > > = [ ] ;
91+ const potentialWorkspaceDirectories : Array < string > = [ ] ;
8092 let isPnpm = false ;
81- let changedFiles = await changedFilesPromise ;
93+ const changedFiles = await changedFilesPromise ;
8294
83- for ( let item of tree . data . tree ) {
84- if ( ! item . path ) continue ;
95+ for ( const item of tree . data . tree ) {
96+ if ( ! item . path ) {
97+ continue ;
98+ }
8599 if ( item . path . endsWith ( "/package.json" ) ) {
86- let dirPath = nodePath . dirname ( item . path ) ;
100+ const dirPath = nodePath . dirname ( item . path ) ;
87101 potentialWorkspaceDirectories . push ( dirPath ) ;
88102 } else if ( item . path === "pnpm-workspace.yaml" ) {
89103 isPnpm = true ;
@@ -95,56 +109,62 @@ export let getChangedPackages = async ({
95109 item . path . endsWith ( ".md" ) &&
96110 changedFiles . includes ( item . path )
97111 ) {
98- let res = / \. c h a n g e s e t \/ ( [ ^ \ .] + ) \. m d / . exec ( item . path ) ;
112+ const res = / \. c h a n g e s e t \/ ( [ ^ . ] + ) \. m d / . exec ( item . path ) ;
99113 if ( ! res ) {
100114 throw new Error ( "could not get name from changeset filename" ) ;
101115 }
102- let id = res [ 1 ] ;
116+ const id = res [ 1 ] ;
117+
103118 changesetPromises . push (
104- fetchTextFile ( item . path ) . then ( ( text ) => {
105- return { ...parseChangeset ( text ) , id } ;
106- } ) ,
119+ fetchTextFile ( item . path ) . then ( ( text ) => ( { ...parseChangeset ( text ) , id } ) ) ,
107120 ) ;
108121 }
109122 }
110123 let tool :
111124 | {
112125 tool : Tool ;
113- globs : string [ ] ;
126+ globs : Array < string > ;
114127 }
115128 | undefined ;
116129
117130 if ( isPnpm ) {
131+ interface PnpmWorkspace {
132+ packages : Array < string > ;
133+ }
134+
135+ const pnpmWorkspaceContent = await fetchTextFile ( "pnpm-workspace.yaml" ) ;
136+ const pnpmWorkspace = safeLoad ( pnpmWorkspaceContent ) as PnpmWorkspace ;
137+
118138 tool = {
139+ globs : pnpmWorkspace . packages ,
119140 tool : "pnpm" ,
120- globs : safeLoad ( await fetchTextFile ( "pnpm-workspace.yaml" ) ) . packages ,
121141 } ;
122142 } else {
123- let rootPackageJsonContent = await rootPackageJsonContentsPromise ;
143+ const rootPackageJsonContent = await rootPackageJsonContentsPromise ;
124144
125145 if ( rootPackageJsonContent . workspaces ) {
126- if ( ! Array . isArray ( rootPackageJsonContent . workspaces ) ) {
146+ if ( Array . isArray ( rootPackageJsonContent . workspaces ) ) {
127147 tool = {
148+ globs : rootPackageJsonContent . workspaces ,
128149 tool : "yarn" ,
129- globs : rootPackageJsonContent . workspaces . packages ,
130150 } ;
131151 } else {
132152 tool = {
153+ globs : rootPackageJsonContent . workspaces . packages ,
133154 tool : "yarn" ,
134- globs : rootPackageJsonContent . workspaces ,
135155 } ;
136156 }
137157 } else if ( rootPackageJsonContent . bolt && rootPackageJsonContent . bolt . workspaces ) {
138158 tool = {
139- tool : "bolt" ,
140159 globs : rootPackageJsonContent . bolt . workspaces ,
160+ tool : "bolt" ,
141161 } ;
142162 }
143163 }
144164
145- let rootPackageJsonContent = await rootPackageJsonContentsPromise ;
165+ const rootPackageJsonContent = await rootPackageJsonContentsPromise ;
146166
147- let packages : Packages = {
167+ const packages : Packages = {
148168 root : {
149169 dir : "/" ,
150170 packageJson : rootPackageJsonContent ,
@@ -157,7 +177,7 @@ export let getChangedPackages = async ({
157177 if ( ! Array . isArray ( tool . globs ) || ! tool . globs . every ( ( x ) => typeof x === "string" ) ) {
158178 throw new Error ( "globs are not valid: " + JSON . stringify ( tool . globs ) ) ;
159179 }
160- let matches = micromatch ( potentialWorkspaceDirectories , tool . globs ) ;
180+ const matches = micromatch ( potentialWorkspaceDirectories , tool . globs ) ;
161181
162182 packages . packages = await Promise . all ( matches . map ( ( dir ) => getPackage ( dir ) ) ) ;
163183 } else {
@@ -167,10 +187,12 @@ export let getChangedPackages = async ({
167187 throw new Error ( "an error occurred when fetching files" ) ;
168188 }
169189
190+ const rawConfig = await rawConfigPromise ;
191+
170192 const releasePlan = assembleReleasePlan (
171193 await Promise . all ( changesetPromises ) ,
172194 packages ,
173- await configPromise . then ( ( rawConfig ) => parseConfig ( rawConfig , packages ) ) ,
195+ parseConfig ( rawConfig , packages ) ,
174196 await preStatePromise ,
175197 ) ;
176198
0 commit comments