Skip to content

Commit 9572e39

Browse files
authored
Access json output file instead of parsing CLI (#6)
1 parent 714363a commit 9572e39

File tree

10 files changed

+113
-23
lines changed

10 files changed

+113
-23
lines changed

.changeset/brown-tips-roll.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'mock-workspace': patch
3+
---
4+
5+
fake entry

.changeset/fast-pigs-trade.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
---

__tests__/main.test.ts

+15
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,21 @@ describe('action', () => {
5353

5454
it('completes when there is a changeset entry for every package', async () => {
5555
// Arrange
56+
jest.spyOn(utils, 'getChangesets').mockImplementation(
57+
async (): Promise<utils.Changesets> =>
58+
Promise.resolve({
59+
releases: [
60+
{
61+
name: '@owner/pkg1',
62+
type: 'patch'
63+
},
64+
{
65+
name: '@owner/pkgB',
66+
type: 'patch'
67+
}
68+
]
69+
})
70+
)
5671
Object.defineProperty(github, 'context', {
5772
value: {
5873
...originalContext,

__tests__/utils.test.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
import { getBaseAndHead } from '../src/utils'
1+
import { getBaseAndHead, getChangesets } from '../src/utils'
22
import { Context } from '@actions/github/lib/context'
33

4+
import fs from 'fs/promises'
5+
jest.mock('fs/promises')
6+
47
const contextData: Context = {
58
payload: {},
69
eventName: '',
@@ -137,4 +140,13 @@ describe('utils', () => {
137140
expect(getBaseAndHead(context)).toEqual([])
138141
})
139142
})
143+
describe('getChangesets', () => {
144+
it('returns the parsed changesets file', async () => {
145+
fs.readFile = jest
146+
.fn()
147+
.mockResolvedValue(JSON.stringify({ releases: [] }))
148+
const changesets = await getChangesets('changesets.json')
149+
expect(changesets).toEqual({ releases: [] })
150+
})
151+
})
140152
})

dist/index.js

+46-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mock-workspace/fakechange

Whitespace-only changes.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
"devDependencies": {
7373
"@changesets/cli": "^2.26.2",
7474
"@types/jest": "^29.5.5",
75-
"@types/node": "^20.6.3",
75+
"@types/node": "^20.6.5",
7676
"@typescript-eslint/eslint-plugin": "^6.7.2",
7777
"@typescript-eslint/parser": "^6.7.2",
7878
"@vercel/ncc": "^0.38.0",

src/main.ts

+8-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as core from '@actions/core'
22
import * as exec from '@actions/exec'
33
import * as github from '@actions/github'
4-
import { getBaseAndHead } from './utils'
4+
import { getBaseAndHead, getChangesets } from './utils'
55

66
/**
77
* The main function for the action.
@@ -77,9 +77,10 @@ export async function run(): Promise<void> {
7777

7878
// right now, the only way to access JSON output is to create a file,
7979
// so we are just going to work with the pretty-printed output
80+
const filePath = `${github.context.runId}.json`
8081
await exec.exec(`yarn add @changesets/cli@latest -W`)
8182
const changesetResult = await exec.getExecOutput(
82-
`yarn changeset status --since origin/${base}`,
83+
`yarn changeset status --since origin/${base} --output ${filePath}`,
8384
undefined,
8485
{ ignoreReturnCode: true }
8586
)
@@ -91,14 +92,11 @@ export async function run(): Promise<void> {
9192
}
9293

9394
// parse out the package names from the pretty-printed changeset output
94-
const changesetEntries =
95-
changesetResult.exitCode === 1
96-
? []
97-
: changesetResult.stdout
98-
.split('\n')
99-
.map((line: string) => line.trim())
100-
.filter((line: string) => line.startsWith('🦋 - '))
101-
.map((line: string) => line.replace('🦋 - ', ''))
95+
let changesetEntries: string[] = []
96+
if (changesetResult.exitCode !== 1) {
97+
const changesets = await getChangesets(filePath)
98+
changesetEntries = changesets.releases.map(release => release.name)
99+
}
102100

103101
const changesetEntriesNeeded = packageNamesArray.filter(
104102
packageName => !changesetEntries.includes(packageName)

src/utils.ts

+17
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Context } from '@actions/github/lib/context'
2+
import * as fs from 'fs/promises'
23

34
export const getBaseAndHead = (context: Context): string[] => {
45
switch (context.eventName) {
@@ -13,3 +14,19 @@ export const getBaseAndHead = (context: Context): string[] => {
1314
}
1415
return []
1516
}
17+
18+
type Release = {
19+
name: string
20+
type: 'major' | 'minor' | 'patch'
21+
oldVersion?: string
22+
changeSets?: string[]
23+
}
24+
25+
export type Changesets = {
26+
releases: Release[]
27+
}
28+
29+
export const getChangesets = async (path: string): Promise<Changesets> => {
30+
const changesetsFile = await fs.readFile(path, 'utf8')
31+
return JSON.parse(changesetsFile) as Changesets
32+
}

yarn.lock

+6-1
Original file line numberDiff line numberDiff line change
@@ -1104,7 +1104,7 @@
11041104
resolved "https://registry.yarnpkg.com/@types/minimist/-/minimist-1.2.2.tgz#ee771e2ba4b3dc5b372935d549fd9617bf345b8c"
11051105
integrity sha512-jhuKLIRrhvCPLqwPcx6INqmKeiA5EWrsCOPhrlFSrbrmU4ZMPjj5Ul/oLCMDO98XRUIwVm78xICz4EPCektzeQ==
11061106

1107-
"@types/node@*", "@types/node@^20.6.3":
1107+
"@types/node@*":
11081108
version "20.6.3"
11091109
resolved "https://registry.yarnpkg.com/@types/node/-/node-20.6.3.tgz#5b763b321cd3b80f6b8dde7a37e1a77ff9358dd9"
11101110
integrity sha512-HksnYH4Ljr4VQgEy2lTStbCKv/P590tmPe5HqOnv9Gprffgv5WXAY+Y5Gqniu0GGqeTCUdBnzC3QSrzPkBkAMA==
@@ -1114,6 +1114,11 @@
11141114
resolved "https://registry.yarnpkg.com/@types/node/-/node-12.20.55.tgz#c329cbd434c42164f846b909bd6f85b5537f6240"
11151115
integrity sha512-J8xLz7q2OFulZ2cyGTLE1TbbZcjpno7FaN6zdJNrgAdrJ+DZzh/uFR6YrTb4C+nXakvud8Q4+rbhoIWlYQbUFQ==
11161116

1117+
"@types/node@^20.6.5":
1118+
version "20.6.5"
1119+
resolved "https://registry.yarnpkg.com/@types/node/-/node-20.6.5.tgz#4c6a79adf59a8e8193ac87a0e522605b16587258"
1120+
integrity sha512-2qGq5LAOTh9izcc0+F+dToFigBWiK1phKPt7rNhOqJSr35y8rlIBjDwGtFSgAI6MGIhjwOVNSQZVdJsZJ2uR1w==
1121+
11171122
"@types/normalize-package-data@^2.4.0":
11181123
version "2.4.1"
11191124
resolved "https://registry.yarnpkg.com/@types/normalize-package-data/-/normalize-package-data-2.4.1.tgz#d3357479a0fdfdd5907fe67e17e0a85c906e1301"

0 commit comments

Comments
 (0)