11import path from 'path' ;
22
3- import homeOrTmp from 'home-or-tmp' ;
43import dedent from 'dedent' ;
4+ import cacheDir from 'cachedir' ;
5+ import { ensureDir } from 'fs-extra' ;
56import { commit as gitCommit , log } from '../git' ;
67import * as cache from './cache' ;
78
@@ -21,41 +22,48 @@ function dispatchGitCommit(sh, repoPath, template, options, overrideOptions, don
2122 * Asynchronously commits files using commitizen
2223 */
2324function commit ( sh , inquirer , repoPath , prompter , options , done ) {
24-
25- var cachePath = path . join ( homeOrTmp , 'commitizen.json' ) ;
26-
27- if ( options . retryLastCommit ) {
28-
29- console . log ( 'Retrying last commit attempt.' ) ;
30-
31- // We want to use the last commit instead of the current commit,
32- // so lets override some options using the values from cache.
33- let {
34- options : retryOptions ,
35- overrideOptions : retryOverrideOptions ,
36- template : retryTemplate
37- } = cache . getCacheValueSync ( cachePath , repoPath ) ;
38- dispatchGitCommit ( sh , repoPath , retryTemplate , retryOptions , retryOverrideOptions , done ) ;
39-
40- } else {
41- // Get user input -- side effect that is hard to test
42- prompter ( inquirer , function ( error , template , overrideOptions ) {
43- // Allow adapters to error out
44- // (error: Error?, template: String, overrideOptions: Object)
45- if ( ! ( error instanceof Error ) ) {
46- overrideOptions = template ;
47- template = error ;
48- error = null ;
49- }
25+ var cacheDirectory = cacheDir ( 'commitizen' ) ;
26+ var cachePath = path . join ( cacheDirectory , 'commitizen.json' ) ;
5027
51- if ( error ) {
52- return done ( error ) ;
53- }
28+ ensureDir ( cacheDirectory , function ( error ) {
29+ if ( error ) {
30+ console . error ( "Couldn't create commitizen cache directory: " , error ) ;
31+ // TODO: properly handle error?
32+ } else {
33+ if ( options . retryLastCommit ) {
5434
55- // We don't want to add retries to the cache, only actual commands
56- cache . setCacheValueSync ( cachePath , repoPath , { template, options, overrideOptions } ) ;
57- dispatchGitCommit ( sh , repoPath , template , options , overrideOptions , done ) ;
58- } ) ;
59- }
35+ console . log ( 'Retrying last commit attempt.' ) ;
36+
37+ // We want to use the last commit instead of the current commit,
38+ // so lets override some options using the values from cache.
39+ let {
40+ options : retryOptions ,
41+ overrideOptions : retryOverrideOptions ,
42+ template : retryTemplate
43+ } = cache . getCacheValueSync ( cachePath , repoPath ) ;
44+ dispatchGitCommit ( sh , repoPath , retryTemplate , retryOptions , retryOverrideOptions , done ) ;
45+
46+ } else {
47+ // Get user input -- side effect that is hard to test
48+ prompter ( inquirer , function ( error , template , overrideOptions ) {
49+ // Allow adapters to error out
50+ // (error: Error?, template: String, overrideOptions: Object)
51+ if ( ! ( error instanceof Error ) ) {
52+ overrideOptions = template ;
53+ template = error ;
54+ error = null ;
55+ }
56+
57+ if ( error ) {
58+ return done ( error ) ;
59+ }
60+
61+ // We don't want to add retries to the cache, only actual commands
62+ cache . setCacheValueSync ( cachePath , repoPath , { template, options, overrideOptions } ) ;
63+ dispatchGitCommit ( sh , repoPath , template , options , overrideOptions , done ) ;
64+ } ) ;
65+ }
66+ }
67+ } ) ;
6068
6169}
0 commit comments