forked from commitizen/cz-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommit.js
60 lines (49 loc) · 1.98 KB
/
commit.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import path from 'path';
import {deprecate} from 'util';
import pify from 'pify';
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';
export default commit;
function askUser (inquirer, prompter) {
return new Promise(function (resolve, reject) {
const decoratedInquirer = {
prompt: deprecate(inquirer.prompt, 'Using the supplied copy of inquirer is depreacted, please depend on inquirer directly').bind(inquirer)
}
prompter(decoratedInquirer, function (arg0, arg1) {
// Allow adapters to error out by providing an Error
if (arg0 instanceof Error) {
return reject(arg0);
}
resolve({ template: arg0, overrideOptions: arg1 });
});
});
}
/**
* Asynchronously commits files using commitizen
*/
function commit (inquirer, repoPath, prompter, options) {
var cacheDirectory = cacheDir('commitizen');
var cachePath = path.join(cacheDirectory, 'commitizen.json');
return pify(ensureDir)(cacheDirectory).then(function () {
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);
return gitCommit(repoPath, retryTemplate, { ...retryOptions, ...retryOverrideOptions });
}
// Get user input -- side effect that is hard to test
return askUser(inquirer, prompter).then(function ({ template, overrideOptions }) {
// We don't want to add retries to the cache, only actual commands
cache.setCacheValueSync(cachePath, repoPath, { template, options, overrideOptions });
return gitCommit(repoPath, template, { ...options, ...overrideOptions });
});
});
}