Skip to content

Commit 96d2cfd

Browse files
blakefBlake Friedmannoomorph
committed
fix(init) exits on error before erasing package.json (wix#1693)
Co-authored-by: Blake Friedman <[email protected]> Co-authored-by: Yaroslav Serhieiev <[email protected]>
1 parent aa25025 commit 96d2cfd

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

detox/local-cli/init.js

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ const mochaTemplates = require('./templates/mocha');
55
const jestTemplates = require('./templates/jest');
66
const log = require('../src/utils/logger').child({ __filename });
77

8+
let exitCode = 0;
9+
810
module.exports.command = 'init';
911
module.exports.desc = 'Scaffold initial E2E test folder structure for a specified test runner';
1012
module.exports.builder = {
@@ -42,17 +44,19 @@ module.exports.handler = async function init(argv) {
4244
'HINT: Try running one of the commands above, look what it does, and take similar steps for your use case.',
4345
].join('\n'));
4446
}
47+
48+
process.exit(exitCode); // eslint-disable-line
4549
};
4650

4751
function createFolder(dir, files) {
4852
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)}`);
5054
}
5155

5256
try {
5357
fs.mkdirSync(dir);
5458
} 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:`);
5660
}
5761

5862
for (const entry of Object.entries(files)) {
@@ -65,9 +69,8 @@ function createFile(filename, content) {
6569
try {
6670
fs.writeFileSync(filename, content);
6771
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}`);
7174
}
7275
}
7376

@@ -91,7 +94,7 @@ function parsePackageJson(filepath) {
9194
try {
9295
return require(filepath);
9396
} 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}`);
9598
}
9699
}
97100

@@ -106,7 +109,7 @@ function savePackageJson(filepath, json) {
106109
try {
107110
fs.writeFileSync(filepath, JSON.stringify(json, null, 2) + '\n');
108111
} 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}`);
110113
}
111114
}
112115

@@ -117,10 +120,16 @@ function patchDetoxConfigInPackageJSON({ runner }) {
117120
log.info(`Patching package.json at path: ${packageJsonPath}`);
118121

119122
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+
}
123127
} else {
124-
log.error(`Failed to find package.json at path: ${packageJsonPath}`);
128+
reportError(`Failed to find package.json at path: ${packageJsonPath}`);
125129
}
126130
}
131+
132+
function reportError(...args) {
133+
log.error(...args);
134+
exitCode = 1;
135+
}

0 commit comments

Comments
 (0)