From 27fb1ae8ee48fe8b03f81f43c1210bd7af850e07 Mon Sep 17 00:00:00 2001 From: Mark Lee Date: Thu, 15 Dec 2016 02:37:45 -0800 Subject: [PATCH] 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 --- package.json | 4 +-- src/commitizen/commit.js | 78 ++++++++++++++++++++++------------------ test/tools/clean.js | 7 ++-- 3 files changed, 48 insertions(+), 41 deletions(-) diff --git a/package.json b/package.json index ffbaedb9..2177b0bf 100644 --- a/package.json +++ b/package.json @@ -60,20 +60,20 @@ "nodemon": "1.11.0", "nyc": "6.6.1", "proxyquire": "1.7.10", - "rimraf": "2.5.4", "semantic-release": "4.3.5", "semver": "5.3.0", "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", diff --git a/src/commitizen/commit.js b/src/commitizen/commit.js index 09739bd3..9af65cf0 100644 --- a/src/commitizen/commit.js +++ b/src/commitizen/commit.js @@ -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'; @@ -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); + }); + } + } + }); } diff --git a/test/tools/clean.js b/test/tools/clean.js index e750dabb..43af7037 100644 --- a/test/tools/clean.js +++ b/test/tools/clean.js @@ -1,6 +1,5 @@ import * as path from 'path'; -import fs from 'fs'; -import rimraf from 'rimraf'; +import fs from 'fs-extra'; import uuid from 'node-uuid'; export { @@ -112,7 +111,7 @@ function isNormalNonZeroInteger(str) { function keep(sh, basePath, paths, n) { for (let i=paths.length; i>n; i--) { - rimraf.sync(path.resolve(basePath, paths[i-1])); + fs.removeSync(path.resolve(basePath, paths[i-1])); } } @@ -124,4 +123,4 @@ function getFileCount(path) { function cleanPath(sh, tmpPath) { sh.rm('-rf', tmpPath + '/*'); sh.mkdir(tmpPath); -} \ No newline at end of file +}