1- const escapeStringRegexp = require ( 'escape-string-regexp' ) ;
2- const fs = require ( 'fs' ) ;
3- const Gherkin = require ( '@cucumber/gherkin' ) ;
4- const Messages = require ( '@cucumber/messages' ) ;
5- const glob = require ( 'glob' ) ;
6- const fsPath = require ( 'path' ) ;
1+ const escapeStringRegexp = require ( 'escape-string-regexp' )
2+ const fs = require ( 'fs' )
3+ const Gherkin = require ( '@cucumber/gherkin' )
4+ const Messages = require ( '@cucumber/messages' )
5+ const { globSync } = require ( 'glob' )
6+ const fsPath = require ( 'path' )
77
8- const { getConfig, getTestRoot } = require ( '../utils' ) ;
9- const Codecept = require ( '../../codecept' ) ;
10- const output = require ( '../../output' ) ;
11- const { matchStep } = require ( '../../mocha/bdd' ) ;
8+ const { getConfig, getTestRoot } = require ( '../utils' )
9+ const Codecept = require ( '../../codecept' )
10+ const output = require ( '../../output' )
11+ const { matchStep } = require ( '../../mocha/bdd' )
1212
13- const uuidFn = Messages . IdGenerator . uuid ( ) ;
14- const builder = new Gherkin . AstBuilder ( uuidFn ) ;
15- const matcher = new Gherkin . GherkinClassicTokenMatcher ( ) ;
16- const parser = new Gherkin . Parser ( builder , matcher ) ;
17- parser . stopAtFirstError = false ;
13+ const uuidFn = Messages . IdGenerator . uuid ( )
14+ const builder = new Gherkin . AstBuilder ( uuidFn )
15+ const matcher = new Gherkin . GherkinClassicTokenMatcher ( )
16+ const parser = new Gherkin . Parser ( builder , matcher )
17+ parser . stopAtFirstError = false
1818
1919module . exports = function ( genPath , options ) {
20- const configFile = options . config || genPath ;
21- const testsPath = getTestRoot ( configFile ) ;
22- const config = getConfig ( configFile ) ;
23- if ( ! config ) return ;
20+ const configFile = options . config || genPath
21+ const testsPath = getTestRoot ( configFile )
22+ const config = getConfig ( configFile )
23+ if ( ! config ) return
2424
25- const codecept = new Codecept ( config , { } ) ;
26- codecept . init ( testsPath ) ;
25+ const codecept = new Codecept ( config , { } )
26+ codecept . init ( testsPath )
2727
2828 if ( ! config . gherkin ) {
29- output . error ( 'Gherkin is not enabled in config. Run `codecept gherkin:init` to enable it' ) ;
30- process . exit ( 1 ) ;
29+ output . error ( 'Gherkin is not enabled in config. Run `codecept gherkin:init` to enable it' )
30+ process . exit ( 1 )
3131 }
3232 if ( ! config . gherkin . steps || ! config . gherkin . steps [ 0 ] ) {
33- output . error ( 'No gherkin steps defined in config. Exiting' ) ;
34- process . exit ( 1 ) ;
33+ output . error ( 'No gherkin steps defined in config. Exiting' )
34+ process . exit ( 1 )
3535 }
3636 if ( ! options . feature && ! config . gherkin . features ) {
37- output . error ( 'No gherkin features defined in config. Exiting' ) ;
38- process . exit ( 1 ) ;
37+ output . error ( 'No gherkin features defined in config. Exiting' )
38+ process . exit ( 1 )
3939 }
4040 if ( options . path && ! config . gherkin . steps . includes ( options . path ) ) {
41- output . error ( `You must include ${ options . path } to the gherkin steps in your config file` ) ;
42- process . exit ( 1 ) ;
41+ output . error ( `You must include ${ options . path } to the gherkin steps in your config file` )
42+ process . exit ( 1 )
4343 }
4444
45- const files = [ ] ;
46- glob . sync ( options . feature || config . gherkin . features , { cwd : options . feature ? '.' : global . codecept_dir } ) . forEach ( file => {
45+ const files = [ ]
46+ globSync ( options . feature || config . gherkin . features , { cwd : options . feature ? '.' : global . codecept_dir } ) . forEach ( file => {
4747 if ( ! fsPath . isAbsolute ( file ) ) {
48- file = fsPath . join ( global . codecept_dir , file ) ;
48+ file = fsPath . join ( global . codecept_dir , file )
4949 }
50- files . push ( fsPath . resolve ( file ) ) ;
51- } ) ;
52- output . print ( `Loaded ${ files . length } files` ) ;
50+ files . push ( fsPath . resolve ( file ) )
51+ } )
52+ output . print ( `Loaded ${ files . length } files` )
5353
54- const newSteps = new Map ( ) ;
54+ const newSteps = new Map ( )
5555
5656 const parseSteps = steps => {
57- const newSteps = [ ] ;
58- let currentKeyword = '' ;
57+ const newSteps = [ ]
58+ let currentKeyword = ''
5959 for ( const step of steps ) {
6060 if ( step . keyword . trim ( ) === 'And' ) {
61- if ( ! currentKeyword ) throw new Error ( `There is no active keyword for step '${ step . text } '` ) ;
62- step . keyword = currentKeyword ;
61+ if ( ! currentKeyword ) throw new Error ( `There is no active keyword for step '${ step . text } '` )
62+ step . keyword = currentKeyword
6363 }
64- currentKeyword = step . keyword ;
64+ currentKeyword = step . keyword
6565 try {
66- matchStep ( step . text ) ;
66+ matchStep ( step . text )
6767 } catch ( err ) {
68- let stepLine ;
68+ let stepLine
6969 if ( / [ { } ( ) / ] / . test ( step . text ) ) {
7070 stepLine = escapeStringRegexp ( step . text )
7171 . replace ( / \/ / g, '\\/' )
7272 . replace ( / \" ( .* ?) \" / g, '"(.*?)"' )
7373 . replace ( / ( \d + \\ \. \d + ) / , '(\\d+\\.\\d+)' )
74- . replace ( / ( \d + ) / , ' (\\d+) ' ) ;
75- stepLine = Object . assign ( stepLine , { type : step . keyword . trim ( ) , location : step . location , regexp : true } ) ;
74+ . replace ( / ( \d + ) / , ' (\\d+) ' )
75+ stepLine = Object . assign ( stepLine , { type : step . keyword . trim ( ) , location : step . location , regexp : true } )
7676 } else {
7777 stepLine = step . text
7878 . replace ( / \" ( .* ?) \" / g, '{string}' )
7979 . replace ( / ( \d + \. \d + ) / , '{float}' )
80- . replace ( / ( \d + ) / , ' {int} ' ) ;
81- stepLine = Object . assign ( stepLine , { type : step . keyword . trim ( ) , location : step . location , regexp : false } ) ;
80+ . replace ( / ( \d + ) / , ' {int} ' )
81+ stepLine = Object . assign ( stepLine , { type : step . keyword . trim ( ) , location : step . location , regexp : false } )
8282 }
83- newSteps . push ( stepLine ) ;
83+ newSteps . push ( stepLine )
8484 }
8585 }
86- return newSteps ;
87- } ;
86+ return newSteps
87+ }
8888
8989 const parseFile = file => {
90- const ast = parser . parse ( fs . readFileSync ( file ) . toString ( ) ) ;
90+ const ast = parser . parse ( fs . readFileSync ( file ) . toString ( ) )
9191 for ( const child of ast . feature . children ) {
92- if ( child . scenario . keyword === 'Scenario Outline' ) continue ; // skip scenario outline
92+ if ( child . scenario . keyword === 'Scenario Outline' ) continue // skip scenario outline
9393 parseSteps ( child . scenario . steps )
9494 . map ( step => {
95- return Object . assign ( step , { file : file . replace ( global . codecept_dir , '' ) . slice ( 1 ) } ) ;
95+ return Object . assign ( step , { file : file . replace ( global . codecept_dir , '' ) . slice ( 1 ) } )
9696 } )
97- . map ( step => newSteps . set ( `${ step . type } (${ step } )` , step ) ) ;
97+ . map ( step => newSteps . set ( `${ step . type } (${ step } )` , step ) )
9898 }
99- } ;
99+ }
100100
101- files . forEach ( file => parseFile ( file ) ) ;
101+ files . forEach ( file => parseFile ( file ) )
102102
103- let stepFile = options . path || config . gherkin . steps [ 0 ] ;
103+ let stepFile = options . path || config . gherkin . steps [ 0 ]
104104 if ( ! fs . existsSync ( stepFile ) ) {
105- output . error ( `Please enter a valid step file path ${ stepFile } ` ) ;
106- process . exit ( 1 ) ;
105+ output . error ( `Please enter a valid step file path ${ stepFile } ` )
106+ process . exit ( 1 )
107107 }
108108
109109 if ( ! fsPath . isAbsolute ( stepFile ) ) {
110- stepFile = fsPath . join ( global . codecept_dir , stepFile ) ;
110+ stepFile = fsPath . join ( global . codecept_dir , stepFile )
111111 }
112112
113113 const snippets = [ ...newSteps . values ( ) ]
@@ -117,18 +117,18 @@ module.exports = function (genPath, options) {
117117${ step . type } (${ step . regexp ? '/^' : "'" } ${ step } ${ step . regexp ? '$/' : "'" } , () => {
118118 // From "${ step . file } " ${ JSON . stringify ( step . location ) }
119119 throw new Error('Not implemented yet');
120- });` ;
121- } ) ;
120+ });`
121+ } )
122122
123123 if ( ! snippets . length ) {
124- output . print ( 'No new snippets found' ) ;
125- return ;
124+ output . print ( 'No new snippets found' )
125+ return
126126 }
127- output . success ( `Snippets generated: ${ snippets . length } ` ) ;
128- output . print ( snippets . join ( '\n' ) ) ;
127+ output . success ( `Snippets generated: ${ snippets . length } ` )
128+ output . print ( snippets . join ( '\n' ) )
129129
130130 if ( ! options . dryRun ) {
131- output . success ( `Snippets added to ${ output . colors . bold ( stepFile ) } ` ) ;
132- fs . writeFileSync ( stepFile , fs . readFileSync ( stepFile ) . toString ( ) + snippets . join ( '\n' ) + '\n' ) ;
131+ output . success ( `Snippets added to ${ output . colors . bold ( stepFile ) } ` )
132+ fs . writeFileSync ( stepFile , fs . readFileSync ( stepFile ) . toString ( ) + snippets . join ( '\n' ) + '\n' )
133133 }
134- } ;
134+ }
0 commit comments