@@ -5,6 +5,8 @@ const mochaTemplates = require('./templates/mocha');
5
5
const jestTemplates = require ( './templates/jest' ) ;
6
6
const log = require ( '../src/utils/logger' ) . child ( { __filename } ) ;
7
7
8
+ let exitCode = 0 ;
9
+
8
10
module . exports . command = 'init' ;
9
11
module . exports . desc = 'Scaffold initial E2E test folder structure for a specified test runner' ;
10
12
module . exports . builder = {
@@ -42,17 +44,19 @@ module.exports.handler = async function init(argv) {
42
44
'HINT: Try running one of the commands above, look what it does, and take similar steps for your use case.' ,
43
45
] . join ( '\n' ) ) ;
44
46
}
47
+
48
+ process . exit ( exitCode ) ; // eslint-disable-line
45
49
} ;
46
50
47
51
function createFolder ( dir , files ) {
48
52
if ( fs . existsSync ( dir ) ) {
49
- return log . error ( `Failed to create ${ dir } folder, because it already exists at path: ${ path . resolve ( dir ) } ` ) ;
53
+ return reportError ( `Failed to create ${ dir } folder, because it already exists at path: ${ path . resolve ( dir ) } ` ) ;
50
54
}
51
55
52
56
try {
53
57
fs . mkdirSync ( dir ) ;
54
58
} catch ( err ) {
55
- return log . error ( { err } , `Failed to create ${ dir } folder due to an error:` ) ;
59
+ return reportError ( { err } , `Failed to create ${ dir } folder due to an error:` ) ;
56
60
}
57
61
58
62
for ( const entry of Object . entries ( files ) ) {
@@ -65,9 +69,8 @@ function createFile(filename, content) {
65
69
try {
66
70
fs . writeFileSync ( filename , content ) ;
67
71
log . info ( `Created a file at path: ${ filename } ` ) ;
68
- } catch ( e ) {
69
- log . error ( `Failed to create a file at path: ${ filename } ` ) ;
70
- log . error ( e ) ;
72
+ } catch ( err ) {
73
+ reportError ( { err } , `Failed to create a file at path: ${ filename } ` ) ;
71
74
}
72
75
}
73
76
@@ -91,7 +94,7 @@ function parsePackageJson(filepath) {
91
94
try {
92
95
return require ( filepath ) ;
93
96
} catch ( err ) {
94
- log . error ( `Failed to parse package.json due to an error:\n${ err . message } ` ) ;
97
+ reportError ( `Failed to parse package.json due to an error:\n${ err . message } ` ) ;
95
98
}
96
99
}
97
100
@@ -106,7 +109,7 @@ function savePackageJson(filepath, json) {
106
109
try {
107
110
fs . writeFileSync ( filepath , JSON . stringify ( json , null , 2 ) + '\n' ) ;
108
111
} catch ( err ) {
109
- log . error ( `Failed to write changes back into package.json due to an error:\n${ err . message } ` ) ;
112
+ reportError ( `Failed to write changes back into package.json due to an error:\n${ err . message } ` ) ;
110
113
}
111
114
}
112
115
@@ -117,10 +120,16 @@ function patchDetoxConfigInPackageJSON({ runner }) {
117
120
log . info ( `Patching package.json at path: ${ packageJsonPath } ` ) ;
118
121
119
122
const packageJson = parsePackageJson ( packageJsonPath ) ;
120
- loggedSet ( packageJson , [ 'detox' , 'test-runner' ] , runner ) ;
121
-
122
- savePackageJson ( packageJsonPath , packageJson ) ;
123
+ if ( packageJson ) {
124
+ loggedSet ( packageJson , [ 'detox' , 'test-runner' ] , runner ) ;
125
+ savePackageJson ( packageJsonPath , packageJson ) ;
126
+ }
123
127
} else {
124
- log . error ( `Failed to find package.json at path: ${ packageJsonPath } ` ) ;
128
+ reportError ( `Failed to find package.json at path: ${ packageJsonPath } ` ) ;
125
129
}
126
130
}
131
+
132
+ function reportError ( ...args ) {
133
+ log . error ( ...args ) ;
134
+ exitCode = 1 ;
135
+ }
0 commit comments