Skip to content

Commit e21f79e

Browse files
committed
feat: use "pino" logger
1 parent 9baa1fa commit e21f79e

File tree

6 files changed

+257
-45
lines changed

6 files changed

+257
-45
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
"git-log-parser": "^1.2.0",
3939
"node-fetch": "2.6.7",
4040
"outvariant": "^1.3.0",
41+
"pino": "^7.10.0",
42+
"pino-pretty": "^7.6.1",
4143
"semver": "^7.3.7",
4244
"yargs": "^17.4.1"
4345
}

src/commands/publish.ts

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { until } from '@open-draft/until'
22
import { invariant } from 'outvariant'
33
import { Command } from '../Command'
4+
import { log } from '../logger'
45
import { createContext } from '../utils/createContext'
56
import { getInfo } from '../utils/git/getInfo'
67
import { getNextReleaseType } from '../utils/getNextReleaseType'
@@ -33,48 +34,54 @@ export class Publish extends Command {
3334
const repo = await getInfo()
3435

3536
const branchName = await getCurrentBranch()
36-
console.log('preparing release from "%s"...', branchName)
37+
38+
log.info(
39+
'preparing release for "%s/%s" from "%s"...',
40+
repo.owner,
41+
repo.name,
42+
branchName
43+
)
3744

3845
// Get the latest release.
3946
const tags = await getTags()
4047
const latestRelease = await getLatestRelease(tags)
4148

4249
if (latestRelease) {
43-
console.log(
44-
'found a previous release "%s" (%s)',
50+
log.info(
51+
'found latest release: %s (%s)',
4552
latestRelease.tag,
4653
latestRelease.hash
4754
)
4855
} else {
49-
console.log('found no previous releases, creating a new one...')
56+
log.info('found no previous releases, creating a new one...')
5057
}
5158

5259
const commits = await getCommits({
5360
after: latestRelease?.hash,
5461
})
5562

5663
if (commits.length === 0) {
57-
console.log('no commits since the latest release, skipping...')
64+
log.warn('no commits since the latest release, skipping...')
5865
return
5966
}
6067

61-
console.log('found %d new commit(s):', commits.length)
68+
log.info('found %d new commit(s):', commits.length)
6269
for (const commit of commits) {
63-
console.log(' - %s %s', commit.commit.short, commit.subject)
70+
log.info('- %s (%s)', commit.subject, commit.hash)
6471
}
6572

6673
// Get the next release type and version number.
6774
const nextReleaseType = getNextReleaseType(commits)
6875
if (!nextReleaseType) {
69-
console.log('committed changes do not bump version, skipping...')
76+
log.warn('committed changes do not bump version, skipping...')
7077
return
7178
}
7279

73-
console.log('release type: %s', nextReleaseType)
80+
log.info('next release type: %s', nextReleaseType)
7481

7582
const prevVersion = latestRelease?.tag || '0.0.0'
7683
const nextVersion = getNextVersion(prevVersion, nextReleaseType)
77-
console.log('next version: %s -> %s', prevVersion, nextVersion)
84+
log.info('next version: %s -> %s', prevVersion, nextVersion)
7885

7986
const context = createContext({
8087
repo,
@@ -89,7 +96,7 @@ export class Publish extends Command {
8996
bumpPackageJson(nextVersion)
9097

9198
// Execute the publishing script.
92-
console.log('executing the publishing script...')
99+
log.info('executing publishing script...')
93100
const publishResult = await until(() => {
94101
return execAsync(this.config.script, {
95102
env: {
@@ -104,9 +111,8 @@ export class Publish extends Command {
104111
publishResult.error
105112
)
106113

107-
console.log('\n')
108-
console.log(publishResult.data)
109-
console.log('published successfully!')
114+
log.info(publishResult.data)
115+
log.info('published successfully!')
110116

111117
const revertQueue: Array<() => Promise<void>> = []
112118

@@ -126,29 +132,30 @@ export class Publish extends Command {
126132
)
127133

128134
revertQueue.push(async () => {
129-
console.log('reverting the release commit...')
135+
log.info('reverting the release commit...')
130136

131137
const hasChanges = await execAsync('git diff')
132138

133139
if (hasChanges) {
134-
console.log('stashing uncommitted changes...')
140+
log.info('stashing uncommitted changes...')
135141
await execAsync('git stash')
136142
}
137143

138144
await execAsync('git reset --hard HEAD~1').finally(async () => {
139145
if (hasChanges) {
140-
console.log('restoring stashed changes...')
146+
log.info('unstashing uncommitted changes...')
141147
await execAsync('git stash pop')
142148
}
143149
})
144150
})
145151

146-
console.log('created a release commit!')
152+
log.info('created release commit!')
147153

148154
// Create a Git tag for the release.
149155
const tagResult = await until(async () => {
150-
await createTag(context.nextRelease.tag)
156+
const tag = await createTag(context.nextRelease.tag)
151157
await execAsync('git push --tags')
158+
return tag
152159
})
153160

154161
invariant(
@@ -158,20 +165,20 @@ export class Publish extends Command {
158165
)
159166

160167
revertQueue.push(async () => {
161-
console.log('reverting release tag...')
168+
log.info('reverting the release tag...')
162169
await execAsync(`git tag -d ${context.nextRelease.tag}`)
163170
await execAsync(`git push --delete origin ${context.nextRelease.tag}`)
164171
})
165172

166-
console.log('created release tag "%s"!', tagResult.data)
173+
log.info('created release tag "%s"!', tagResult.data)
167174

168175
// Generate release notes and create a new release on GitHub.
169176
const releaseNotes = await getReleaseNotes(commits)
170177
const releaseMarkdown = toMarkdown(context, releaseNotes)
171-
console.log('generated release notes:\n\n', releaseMarkdown)
178+
log.info('generated release notes:\n\n', releaseMarkdown)
172179

173180
const releaseUrl = await createRelease(context, releaseMarkdown)
174-
console.log('created release: %s', releaseUrl)
181+
log.info('created release: %s', releaseUrl)
175182

176183
// Push the release commit and tag to the origin.
177184
const pushResult = await until(() => push())
@@ -181,7 +188,7 @@ export class Publish extends Command {
181188
pushResult.error
182189
)
183190

184-
console.log('pushed changes to origin!')
191+
log.info('pushed changes to "%s" (origin)!', repo.remote)
185192
})
186193

187194
if (result.error) {
@@ -191,17 +198,18 @@ export class Publish extends Command {
191198
* so the package has been published at this point, just the Git info
192199
* updates are missing.
193200
*/
194-
console.log('pushing release failed, reverting changes...')
201+
log.warn('pushing release failed, reverting changes...')
195202

196203
// Revert changes in case of errors.
197204
for (const revert of revertQueue) {
198205
await revert()
199206
}
200207

208+
log.error(result.error)
201209
console.error(result.error)
202210
process.exit(1)
203211
}
204212

205-
console.log('release done!')
213+
log.info('release "%s" completed!', context.nextRelease.tag)
206214
}
207215
}

src/logger.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import pino from 'pino'
2+
3+
export const log = pino({
4+
base: null,
5+
transport: {
6+
target: 'pino-pretty',
7+
options: {
8+
colorize: true,
9+
timestampKey: false,
10+
},
11+
},
12+
})

src/utils/git/createRelease.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { log } from '../../../src/logger'
12
import fetch from 'node-fetch'
23
import { format } from 'outvariant'
34
import type { ReleaseContext } from 'utils/createContext'
@@ -16,7 +17,7 @@ export async function createRelease(
1617
): Promise<string> {
1718
const { repo } = context
1819

19-
console.log('creating a new release at "%s/%s"...', repo.owner, repo.name)
20+
log.info('creating a new release at "%s/%s"...', repo.owner, repo.name)
2021

2122
const response = await fetch(
2223
`https://api.github.com/repos/${repo.owner}/${repo.name}/releases`,

test/publish/publish.test.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { createTeardown } from 'fs-teardown'
22
import { Git } from 'node-git-server'
33
import { rest } from 'msw'
44
import { setupServer } from 'msw/node'
5+
import { log } from '../../src/logger'
56
import { createOrigin, initGit, startGitProvider } from '../utils'
67
import { Publish } from '../../src/commands/publish'
78
import { execAsync } from '../../src/utils/execAsync'
@@ -40,8 +41,8 @@ const gitProvider = new Git(fsMock.resolve('git-provider'), {
4041
.on('fetch', (fetch) => fetch.accept())
4142

4243
beforeAll(async () => {
43-
jest.spyOn(console, 'log').mockImplementation()
44-
jest.spyOn(console, 'error')
44+
jest.spyOn(log, 'info').mockImplementation()
45+
jest.spyOn(log, 'error')
4546

4647
execAsync.mockContext({
4748
cwd: fsMock.resolve(),
@@ -87,19 +88,19 @@ module.exports = {
8788
})
8889
await publish.run()
8990

90-
expect(console.error).not.toHaveBeenCalled()
91+
expect(log.error).not.toHaveBeenCalled()
9192

92-
expect(console.log).toHaveBeenCalledWith('found %d new commit(s):', 2)
93+
expect(log.info).toHaveBeenCalledWith('found %d new commit(s):', 2)
9394

9495
// Must notify about the next version.
95-
expect(console.log).toHaveBeenCalledWith(
96+
expect(log.info).toHaveBeenCalledWith(
9697
'next version: %s -> %s',
9798
'0.0.0',
9899
'0.1.0'
99100
)
100101

101102
// The release script is provided with the environmental variables.
102-
expect(console.log).toHaveBeenCalledWith('release script input: 0.1.0\n')
103+
expect(log.info).toHaveBeenCalledWith('release script input: 0.1.0\n')
103104

104105
// Must bump the "version" in package.json.
105106
expect(
@@ -117,5 +118,5 @@ module.exports = {
117118
expect.stringContaining('0.1.0')
118119
)
119120

120-
expect(console.log).toHaveBeenCalledWith('created release: %s', '/releases/1')
121+
expect(log.info).toHaveBeenCalledWith('created release: %s', '/releases/1')
121122
})

0 commit comments

Comments
 (0)