Skip to content

Commit

Permalink
feat(commit): use OS-specific cache dir for commitizen.json instead o…
Browse files Browse the repository at this point in the history
…f home-or-tmp

Based on commitizen#252.

Fixes commitizen#240, commitizen#252, commitizen#339
  • Loading branch information
malept committed Dec 15, 2016
1 parent 6fa09a2 commit 9155e7d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 36 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,15 @@
"sinon": "1.17.6"
},
"dependencies": {
"cachedir": "^1.1.0",
"chalk": "1.1.3",
"cz-conventional-changelog": "1.2.0",
"dedent": "0.6.0",
"detect-indent": "4.0.0",
"find-node-modules": "1.0.3",
"find-root": "1.0.0",
"fs-extra": "^1.0.0",
"glob": "7.1.1",
"home-or-tmp": "2.0.0",
"inquirer": "1.2.2",
"lodash": "4.16.3",
"minimist": "1.2.0",
Expand Down
78 changes: 43 additions & 35 deletions src/commitizen/commit.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import path from 'path';

import homeOrTmp from 'home-or-tmp';
import dedent from 'dedent';
import cacheDir from 'cachedir';
import {ensureDir} from 'fs-extra';
import {commit as gitCommit, log} from '../git';
import * as cache from './cache';

Expand All @@ -21,41 +22,48 @@ function dispatchGitCommit(sh, repoPath, template, options, overrideOptions, don
* Asynchronously commits files using commitizen
*/
function commit(sh, inquirer, repoPath, prompter, options, done) {

var cachePath = path.join(homeOrTmp, 'commitizen.json');

if(options.retryLastCommit) {

console.log('Retrying last commit attempt.');

// We want to use the last commit instead of the current commit,
// so lets override some options using the values from cache.
let {
options: retryOptions,
overrideOptions: retryOverrideOptions,
template: retryTemplate
} = cache.getCacheValueSync(cachePath, repoPath);
dispatchGitCommit(sh, repoPath, retryTemplate, retryOptions, retryOverrideOptions, done);

} else {
// Get user input -- side effect that is hard to test
prompter(inquirer, function(error, template, overrideOptions) {
// Allow adapters to error out
// (error: Error?, template: String, overrideOptions: Object)
if (!(error instanceof Error)) {
overrideOptions = template;
template = error;
error = null;
}
var cacheDirectory = cacheDir('commitizen');
var cachePath = path.join(cacheDirectory, 'commitizen.json');

if (error) {
return done(error);
}
ensureDir(cacheDirectory, function(error) {
if (error) {
console.error("Couldn't create commitizen cache directory: ", error);
// TODO: properly handle error?
} else {
if(options.retryLastCommit) {

// We don't want to add retries to the cache, only actual commands
cache.setCacheValueSync(cachePath, repoPath, { template, options, overrideOptions });
dispatchGitCommit(sh, repoPath, template, options, overrideOptions, done);
});
}
console.log('Retrying last commit attempt.');

// We want to use the last commit instead of the current commit,
// so lets override some options using the values from cache.
let {
options: retryOptions,
overrideOptions: retryOverrideOptions,
template: retryTemplate
} = cache.getCacheValueSync(cachePath, repoPath);
dispatchGitCommit(sh, repoPath, retryTemplate, retryOptions, retryOverrideOptions, done);

} else {
// Get user input -- side effect that is hard to test
prompter(inquirer, function(error, template, overrideOptions) {
// Allow adapters to error out
// (error: Error?, template: String, overrideOptions: Object)
if (!(error instanceof Error)) {
overrideOptions = template;
template = error;
error = null;
}

if (error) {
return done(error);
}

// We don't want to add retries to the cache, only actual commands
cache.setCacheValueSync(cachePath, repoPath, { template, options, overrideOptions });
dispatchGitCommit(sh, repoPath, template, options, overrideOptions, done);
});
}
}
});

}

0 comments on commit 9155e7d

Please sign in to comment.