11const fs = require ( "fs" ) ;
22const yargs = require ( "yargs" ) ;
33const CURRENT_VERSION = require ( "./package.json" ) . version ;
4+ const { info, error, debug } = require ( "./src/log" ) ;
45
56const {
67 init,
@@ -12,12 +13,13 @@ const {
1213
1314const commitCommand = "commit" ;
1415const branchCommand = "branch"
16+ const knownCommands = [ commitCommand , branchCommand ] ;
1517
1618const appendLineToFile = ( filename , line ) => {
1719 try {
1820 fs . appendFileSync ( filename , `${ line } \n` ) ;
1921 } catch ( e ) {
20- console . error ( `Error appending line to file ${ filename } : ${ e . message } ` ) ;
22+ error ( `Error appending line to file ${ filename } : ${ e . message } ` ) ;
2123 throw e ;
2224 }
2325} ;
9597 commitMessage,
9698 commitDescription,
9799 } = argv ;
98-
99- init ( ) ;
100+ debug ( "Passed args:" , JSON . stringify ( argv , null , 2 ) ) ;
100101 createCommitOnBranch (
101102 owner ,
102103 repo ,
@@ -107,7 +108,7 @@ yargs
107108 commitDescription
108109 )
109110 . then ( ( response ) => {
110- console . log ( `Commit created: ${ response . commitUrl } ` ) ;
111+ info ( `Commit created: ${ response . commitUrl } ` ) ;
111112 writeResultToGithubOutputFile ( [
112113 {
113114 label : "command" ,
@@ -119,8 +120,9 @@ yargs
119120 } ,
120121 ] ) ;
121122 } )
122- . catch ( ( error ) => {
123- console . error ( "Failed to create commit:" , error . message ) ;
123+ . catch ( ( err ) => {
124+ error ( "Failed to create commit:" , err . message ) ;
125+ process . exit ( 1 ) ;
124126 } ) ;
125127 }
126128 )
@@ -150,11 +152,11 @@ yargs
150152 } ,
151153 ( argv ) => {
152154 const { owner, repo, branch } = argv ;
153- init ( ) ;
155+ debug ( "Passed args:" , JSON . stringify ( argv , null , 2 ) ) ;
154156 checkIfBranchExists ( owner , repo , branch )
155157 . then ( ( response ) => {
156158 const n = response ? "a" : "no" ;
157- console . log (
159+ info (
158160 `Repository ${ owner } /${ repo } has ${ n } branch named '${ branch } '`
159161 ) ;
160162 writeResultToGithubOutputFile ( [
@@ -168,15 +170,26 @@ yargs
168170 } ,
169171 ] ) ;
170172 } )
171- . catch ( ( error ) => {
172- console . error ( "Failed to check if branch exists:" , error . message ) ;
173+ . catch ( ( err ) => {
174+ error ( "Failed to check if branch exists:" , err . message ) ;
175+ process . exit ( 1 ) ;
173176 } ) ;
174177 }
175178 )
176179 . demandCommand ( )
177180 . version ( CURRENT_VERSION )
178181 . alias ( {
179182 h : "help" ,
180- v : "version"
183+ v : "version" ,
184+ } )
185+ . check ( ( argv ) => {
186+ const cmd = argv . _ [ 0 ] ;
187+ if ( ! knownCommands . includes ( cmd ) ) {
188+ throw new Error ( `Unknown command: ${ cmd } ` ) ;
189+ }
190+ return true ;
191+ } )
192+ . check ( ( ) => {
193+ return init ( ) ;
181194 } )
182195 . help ( ) . argv ;
0 commit comments