Skip to content

Commit 27fb1ae

Browse files
maleptjimthedev
authored andcommitted
feat(commit): use OS-specific cache dir for commitizen.json instead of home-or-tmp (#400)
* feat(commit): use OS-specific cache dir for commitizen.json instead of home-or-tmp Based on #252. Fixes #240, #252, #339 * refactor(tests): replace rimraf with fs-extra
1 parent 6fa09a2 commit 27fb1ae

File tree

3 files changed

+48
-41
lines changed

3 files changed

+48
-41
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,20 +60,20 @@
6060
"nodemon": "1.11.0",
6161
"nyc": "6.6.1",
6262
"proxyquire": "1.7.10",
63-
"rimraf": "2.5.4",
6463
"semantic-release": "4.3.5",
6564
"semver": "5.3.0",
6665
"sinon": "1.17.6"
6766
},
6867
"dependencies": {
68+
"cachedir": "^1.1.0",
6969
"chalk": "1.1.3",
7070
"cz-conventional-changelog": "1.2.0",
7171
"dedent": "0.6.0",
7272
"detect-indent": "4.0.0",
7373
"find-node-modules": "1.0.3",
7474
"find-root": "1.0.0",
75+
"fs-extra": "^1.0.0",
7576
"glob": "7.1.1",
76-
"home-or-tmp": "2.0.0",
7777
"inquirer": "1.2.2",
7878
"lodash": "4.16.3",
7979
"minimist": "1.2.0",

src/commitizen/commit.js

Lines changed: 43 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import path from 'path';
22

3-
import homeOrTmp from 'home-or-tmp';
43
import dedent from 'dedent';
4+
import cacheDir from 'cachedir';
5+
import {ensureDir} from 'fs-extra';
56
import {commit as gitCommit, log} from '../git';
67
import * as cache from './cache';
78

@@ -21,41 +22,48 @@ function dispatchGitCommit(sh, repoPath, template, options, overrideOptions, don
2122
* Asynchronously commits files using commitizen
2223
*/
2324
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');
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
}

test/tools/clean.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import * as path from 'path';
2-
import fs from 'fs';
3-
import rimraf from 'rimraf';
2+
import fs from 'fs-extra';
43
import uuid from 'node-uuid';
54

65
export {
@@ -112,7 +111,7 @@ function isNormalNonZeroInteger(str) {
112111
function keep(sh, basePath, paths, n) {
113112

114113
for (let i=paths.length; i>n; i--) {
115-
rimraf.sync(path.resolve(basePath, paths[i-1]));
114+
fs.removeSync(path.resolve(basePath, paths[i-1]));
116115
}
117116
}
118117

@@ -124,4 +123,4 @@ function getFileCount(path) {
124123
function cleanPath(sh, tmpPath) {
125124
sh.rm('-rf', tmpPath + '/*');
126125
sh.mkdir(tmpPath);
127-
}
126+
}

0 commit comments

Comments
 (0)