11#!/usr/bin/env node
22
33import chalk from 'chalk' ;
4+ import commitCount from 'git-commit-count' ;
45import execa from 'execa' ;
5- import yargs from 'yargs' ;
66import inquirer from 'inquirer' ;
7- import isGit from 'is-git-repository' ;
87import isAdded from 'is-git-added' ;
8+ import isGit from 'is-git-repository' ;
99import updateNotifier from 'update-notifier' ;
10+ import yargs from 'yargs' ;
1011
11- import pkg from '../package.json' ;
1212import getConfig from './getConfig' ;
13- import questions from './questions' ;
1413import combineTypeScope from './helpers/combineTypeScope' ;
14+ import pkg from '../package.json' ;
15+ import questions , {
16+ initMessage ,
17+ initQuestion ,
18+ } from './questions' ;
1519
1620const config = getConfig ( ) ;
1721const questionsList = questions ( config ) ;
22+ const question = initQuestion ( config ) ;
23+
24+ const gitCommitExeca = message => (
25+ execa ( 'git' , [ 'commit' , '-m' , message ] , { stdio : 'inherit' } )
26+ . catch ( ( ) => {
27+ console . error ( chalk . red ( '\nAn error occured. Try to resolve the previous error and run following commit message again:' ) ) ;
28+ console . error ( chalk . green ( `git commit -m "${ message } "` ) ) ;
29+ } )
30+ ) ;
31+
1832const argv = yargs
1933 . usage ( 'Usage: $0' )
2034 . alias ( 'v' , 'version' )
@@ -31,15 +45,19 @@ if (argv.v) {
3145 console . error ( 'fatal: Not a git repository (or any of the parent directories): .git' ) ;
3246} else if ( ! isAdded ( ) ) {
3347 console . error ( chalk . red ( 'Please' , chalk . bold ( 'git add' ) , 'some files first before you commit.' ) ) ;
48+ } else if ( commitCount ( ) === 0 &&
49+ typeof config [ 'initial-commit' ] === 'object' &&
50+ config [ 'initial-commit' ] . isEnabled ) {
51+ const message = initMessage ( config ) ;
52+
53+ inquirer . prompt ( question ) . then ( answers => (
54+ answers . initCommit ? gitCommitExeca ( message ) : undefined
55+ ) ) ;
3456} else {
3557 inquirer . prompt ( questionsList ) . then ( ( answers ) => {
3658 const typeScope = combineTypeScope ( answers . type , answers . scope ) ;
3759 const message = answers . body ? `${ answers . editor } ` : `${ typeScope } ${ answers . description } ` ;
3860
39- return execa ( 'git' , [ 'commit' , '-m' , message ] , { stdio : 'inherit' } )
40- . catch ( ( ) => {
41- console . error ( chalk . red ( '\nAn error occured. Try to resolve the previous error and run following commit message again:' ) ) ;
42- console . error ( chalk . green ( `git commit -m "${ message } "` ) ) ;
43- } ) ;
61+ return gitCommitExeca ( message ) ;
4462 } ) ;
4563}
0 commit comments