11import { until } from '@open-draft/until'
22import { invariant } from 'outvariant'
33import { Command } from '../Command'
4+ import { log } from '../logger'
45import { createContext } from '../utils/createContext'
56import { getInfo } from '../utils/git/getInfo'
67import { 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}
0 commit comments