From 90c97e0f3817ed5fe6a9e7f42282eb1cdedad16d Mon Sep 17 00:00:00 2001 From: BrianMuenzenmeyer Date: Mon, 25 Nov 2019 13:10:16 -0600 Subject: [PATCH] fix(react-scripts): proactively append to .gitignore during generation --- packages/react-scripts/scripts/init.js | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/packages/react-scripts/scripts/init.js b/packages/react-scripts/scripts/init.js index aa035d15345..74d607b105d 100644 --- a/packages/react-scripts/scripts/init.js +++ b/packages/react-scripts/scripts/init.js @@ -190,23 +190,20 @@ module.exports = function( } } - // Rename gitignore after the fact to prevent npm from renaming it to .npmignore - // See: https://github.com/npm/npm/issues/1862 - try { + const gitignoreExists = fs.existsSync(path.join(appPath, '.gitignore')); + if (gitignoreExists) { + // Append if there's already a `.gitignore` file there + const data = fs.readFileSync(path.join(appPath, 'gitignore')); + fs.appendFileSync(path.join(appPath, '.gitignore'), data); + fs.unlinkSync(path.join(appPath, 'gitignore')); + } else { + // Rename gitignore after the fact to prevent npm from renaming it to .npmignore + // See: https://github.com/npm/npm/issues/1862 fs.moveSync( path.join(appPath, 'gitignore'), path.join(appPath, '.gitignore'), [] ); - } catch (err) { - // Append if there's already a `.gitignore` file there - if (err.code === 'EEXIST') { - const data = fs.readFileSync(path.join(appPath, 'gitignore')); - fs.appendFileSync(path.join(appPath, '.gitignore'), data); - fs.unlinkSync(path.join(appPath, 'gitignore')); - } else { - throw err; - } } let command;