44
55var CZ_CONFIG_NAME = '.cz-config.js' ;
66var CZ_CONFIG_EXAMPLE_LOCATION = './cz-config-EXAMPLE.js' ;
7- var wrap = require ( 'word-wrap' ) ;
87var findConfig = require ( 'find-config' ) ;
98var log = require ( 'winston' ) ;
109var editor = require ( 'editor' ) ;
1110var temp = require ( 'temp' ) . track ( ) ;
1211var fs = require ( 'fs' ) ;
1312var path = require ( 'path' ) ;
13+ var buildCommit = require ( './buildCommit' ) ;
14+
1415
1516/* istanbul ignore next */
1617function readConfigFile ( ) {
@@ -32,188 +33,25 @@ function readConfigFile() {
3233 var config = findConfig . require ( CZ_CONFIG_NAME , { home : false } ) ;
3334
3435 if ( config ) {
35- console . info ( '>>> Using cz-customizable file ".cz-config.js" ' ) ;
36+ console . info ( '>>> cz-customizable config file has been found. ' ) ;
3637 return config ;
3738 }
3839
3940 log . warn ( 'Unable to find a configuration file. Please refer to documentation to learn how to ser up: https://github.com/leonardoanalista/cz-customizable#steps "' ) ;
40-
41- // if (!config) {
42- // log.warn('Unable to find a config file "' + CZ_CONFIG_NAME + '". Default configuration would be used.');
43- // log.warn('Copy and use it as template by running in a project root directory:\n "cp '
44- // + path.resolve(CZ_CONFIG_EXAMPLE_LOCATION) + ' ' + path.join('.', CZ_CONFIG_NAME) + '"');
45-
46- // config = require(CZ_CONFIG_EXAMPLE_LOCATION);
47- // }
4841}
4942
50- function buildCommit ( answers ) {
51- var maxLineWidth = 100 ;
52-
53- var wrapOptions = {
54- trim : true ,
55- newline : '\n' ,
56- indent :'' ,
57- width : maxLineWidth
58- } ;
59-
60- function addScope ( scope ) {
61- if ( ! scope ) return ': ' ; //it could be type === WIP. So there is no scope
62-
63- return '(' + scope . trim ( ) + '): ' ;
64- }
65-
66- function addSubject ( subject ) {
67- return subject . trim ( ) ;
68- }
69-
70- function escapeSpecialChars ( result ) {
71- var specialChars = [ '\`' ] ;
72-
73- specialChars . map ( function ( item ) {
74- // For some strange reason, we have to pass additional '\' slash to commitizen. Total slashes are 4.
75- // If user types "feat: `string`", the commit preview should show "feat: `\\string\\`".
76- // Don't worry. The git log will be "feat: `string`"
77- result = result . replace ( new RegExp ( item , 'g' ) , '\\\\`' ) ;
78- } ) ;
79- return result ;
80- }
81-
82- // Hard limit this line
83- var head = ( answers . type + addScope ( answers . scope ) + addSubject ( answers . subject ) ) . slice ( 0 , maxLineWidth ) ;
84-
85- // Wrap these lines at 100 characters
86- var body = wrap ( answers . body , wrapOptions ) || '' ;
87- body = body . split ( '|' ) . join ( '\n' ) ;
88-
89- var breaking = wrap ( answers . breaking , wrapOptions ) ;
90- var footer = wrap ( answers . footer , wrapOptions ) ;
91-
92- var result = head ;
93- if ( body ) {
94- result += '\n\n' + body ;
95- }
96- if ( breaking ) {
97- result += '\n\n' + 'BREAKING CHANGE:\n' + breaking ;
98- }
99- if ( footer ) {
100- result += '\n\nISSUES CLOSED: ' + footer ;
101- }
102-
103- return escapeSpecialChars ( result ) ;
104- }
105-
106- var isNotWip = function ( answers ) {
107- return answers . type . toLowerCase ( ) !== 'wip' ;
108- } ;
10943
11044module . exports = {
11145
11246 prompter : function ( cz , commit ) {
11347 var config = readConfigFile ( ) ;
114- config . scopeOverrides = config . scopeOverrides || { } ;
11548
11649 log . info ( '\n\nLine 1 will be cropped at 100 characters. All other lines will be wrapped after 100 characters.\n' ) ;
11750
118- cz . prompt ( [
119- {
120- type : 'list' ,
121- name : 'type' ,
122- message : 'Select the type of change that you\'re committing:' ,
123- choices : config . types
124- } ,
125- {
126- type : 'list' ,
127- name : 'scope' ,
128- message : '\nDenote the SCOPE of this change (optional):' ,
129- choices : function ( answers ) {
130- var scopes = [ ] ;
131- if ( config . scopeOverrides [ answers . type ] ) {
132- scopes = scopes . concat ( config . scopeOverrides [ answers . type ] ) ;
133- } else {
134- scopes = scopes . concat ( config . scopes ) ;
135- }
136- if ( config . allowCustomScopes || scopes . length === 0 ) {
137- scopes = scopes . concat ( [
138- new cz . Separator ( ) ,
139- { name : 'empty' , value : false } ,
140- { name : 'custom' , value : 'custom' }
141- ] ) ;
142- }
143- return scopes ;
144- } ,
145- when : function ( answers ) {
146- var hasScope = false ;
147- if ( config . scopeOverrides [ answers . type ] ) {
148- hasScope = ! ! ( config . scopeOverrides [ answers . type ] . length > 0 ) ;
149- } else {
150- hasScope = ! ! ( config . scopes && ( config . scopes . length > 0 ) ) ;
151- }
152- if ( ! hasScope ) {
153- answers . scope = 'custom' ;
154- return false ;
155- } else {
156- return isNotWip ( answers ) ;
157- }
158- }
159- } ,
160- {
161- type : 'input' ,
162- name : 'scope' ,
163- message : 'Denote the SCOPE of this change:' ,
164- when : function ( answers ) {
165- return answers . scope === 'custom' ;
166- }
167- } ,
168- {
169- type : 'input' ,
170- name : 'subject' ,
171- message : 'Write a SHORT, IMPERATIVE tense description of the change:\n' ,
172- validate : function ( value ) {
173- return ! ! value ;
174- } ,
175- filter : function ( value ) {
176- return value . charAt ( 0 ) . toLowerCase ( ) + value . slice ( 1 ) ;
177- }
178- } ,
179- {
180- type : 'input' ,
181- name : 'body' ,
182- message : 'Provide a LONGER description of the change (optional). Use "|" to break new line:\n'
183- } ,
184- {
185- type : 'input' ,
186- name : 'breaking' ,
187- message : 'List any BREAKING CHANGES (optional):\n' ,
188- when : function ( answers ) {
189- if ( config . allowBreakingChanges ) {
190- return config . allowBreakingChanges . indexOf ( answers . type . toLowerCase ( ) ) >= 0 ;
191- }
51+ var questions = require ( './questions' ) . getQuestions ( config , cz ) ;
52+
53+ cz . prompt ( questions ) . then ( function ( answers ) {
19254
193- return true ;
194- }
195- } ,
196- {
197- type : 'input' ,
198- name : 'footer' ,
199- message : 'List any ISSUES CLOSED by this change (optional). E.g.: #31, #34:\n' ,
200- when : isNotWip
201- } ,
202- {
203- type : 'expand' ,
204- name : 'confirmCommit' ,
205- choices : [
206- { key : 'y' , name : 'Yes' , value : 'yes' } ,
207- { key : 'n' , name : 'Abort commit' , value : 'no' } ,
208- { key : 'e' , name : 'Edit message' , value : 'edit' }
209- ] ,
210- message : function ( answers ) {
211- var SEP = '###--------------------------------------------------------###' ;
212- log . info ( '\n' + SEP + '\n' + buildCommit ( answers ) + '\n' + SEP + '\n' ) ;
213- return 'Are you sure you want to proceed with the commit above?' ;
214- }
215- }
216- ] , function ( answers ) {
21755 if ( answers . confirmCommit === 'edit' ) {
21856 temp . open ( null , function ( err , info ) {
21957 /* istanbul ignore else */
0 commit comments