From e2bf90ea965d63966b9783e8b10735ce04b19827 Mon Sep 17 00:00:00 2001 From: Nicolas Cuillery Date: Fri, 29 Dec 2017 17:48:51 -0500 Subject: [PATCH 1/2] Keep the ignored files during the upgrade process When the user adds new files to the .gitignore file, those files are deleted during the upgrade process because it starts by creating a fresh .gitignore file without user's modification. This commit adds a step to move the excluded file at the repository level so that they are kept ignored during the upgrade process. --- react-native-git-upgrade/cliEntry.js | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/react-native-git-upgrade/cliEntry.js b/react-native-git-upgrade/cliEntry.js index b65c23733ddf3e..4b3d69c8de3085 100644 --- a/react-native-git-upgrade/cliEntry.js +++ b/react-native-git-upgrade/cliEntry.js @@ -147,6 +147,28 @@ function configureGitEnv(tmpDir) { process.env.GIT_WORK_TREE = '.'; } +function copyCurrentGitIgnoreFile(tmpDir) { + /* + * The user may have added new files or directories in the .gitignore file. + * We need to keep those files ignored during the process, otherwise they + * will be deleted. + * See https://github.com/facebook/react-native/issues/12237 + */ + try { + const gitignorePath = path.resolve(process.cwd(), '.gitignore'); + const repoExcludePath = path.resolve(tmpDir, process.env.GIT_DIR, 'info/exclude'); + const content = fs.readFileSync(gitignorePath, 'utf8'); + fs.appendFileSync(repoExcludePath, content); + } catch (err) { + if (err.code === 'ENOENT') { + log.info('No .gitignore file found, this step is a no-op'); + return; + } + + throw err; + } +} + function generateTemplates(generatorDir, appName, verbose) { try { const yeomanGeneratorEntryPoint = path.resolve(generatorDir, 'index.js'); @@ -277,9 +299,12 @@ async function run(requestedVersion, cliArgs) { log.info('Configure Git environment'); configureGitEnv(tmpDir); - log.info('Init Git repository'); + log.info('Init temporary Git repository'); await exec('git init', verbose); + log.info('Save current .gitignore file'); + await copyCurrentGitIgnoreFile(tmpDir); + log.info('Add all files to commit'); await exec('git add .', verbose); From ffdfcebdc7ceacd522801b7fe42ad6a8089e2c8b Mon Sep 17 00:00:00 2001 From: Janic Duplessis Date: Mon, 1 Jan 2018 13:26:42 -0500 Subject: [PATCH 2/2] Remove await --- react-native-git-upgrade/cliEntry.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/react-native-git-upgrade/cliEntry.js b/react-native-git-upgrade/cliEntry.js index 4b3d69c8de3085..1bb36cc63d5224 100644 --- a/react-native-git-upgrade/cliEntry.js +++ b/react-native-git-upgrade/cliEntry.js @@ -303,7 +303,7 @@ async function run(requestedVersion, cliArgs) { await exec('git init', verbose); log.info('Save current .gitignore file'); - await copyCurrentGitIgnoreFile(tmpDir); + copyCurrentGitIgnoreFile(tmpDir); log.info('Add all files to commit'); await exec('git add .', verbose);