1
1
import path from 'path' ;
2
2
3
- import homeOrTmp from 'home-or-tmp' ;
4
3
import dedent from 'dedent' ;
4
+ import cacheDir from 'cachedir' ;
5
+ import { ensureDir } from 'fs-extra' ;
5
6
import { commit as gitCommit , log } from '../git' ;
6
7
import * as cache from './cache' ;
7
8
@@ -21,41 +22,48 @@ function dispatchGitCommit(sh, repoPath, template, options, overrideOptions, don
21
22
* Asynchronously commits files using commitizen
22
23
*/
23
24
function 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' ) ;
50
27
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 ) {
54
34
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
+ } ) ;
60
68
61
69
}
0 commit comments