From 9712d335e2efcf9d95bef8a9bd879d031f223cbb Mon Sep 17 00:00:00 2001 From: Martin Konicek Date: Mon, 21 Nov 2016 12:52:36 -0800 Subject: [PATCH] Fix handling of dotfiles in 'react-native init' and 'react-native upgrade' Summary: Followup for CLI rewrite (https://github.com/facebook/react-native/commit/a477aec10d29b4651944cb4292daf06dfa953ea3). See the comment in the code for details. **Test plan (required)** - Published to Sinopia locally ([docs](https://github.com/facebook/react-native/tree/master/react-native-cli)) - Ran 'react-native init MyApp', the correct files were created (no more .npmignore, but have .gitignore): $ cd MyApp $ ls -a . .flowconfig __tests__ ios .. .gitattributes android node_modules .babelrc .gitignore index.android.js package.json .buckconfig .watchmanconfig index.ios.js yarn.lock - Changed .flowconfig, ran 'react-native upgrade'. Saw a prompt "Do you want to overwrite .flowconfig", tried answering first 'n' and then 'y'. When answering 'y' the file was overwritten by the version from the template as expected. Closes https://github.com/facebook/react-native/pull/11051 Differential Revision: D4214831 Pulled By: ericvicenti fbshipit-source-id: 7c6aae4f97c7d45e7241bf017ed2f6527d5d29fe --- .../copyProjectTemplateAndReplace.js | 21 +++++++++++++++++-- .../HelloWorld/{.babelrc => _babelrc} | 0 .../HelloWorld/{.buckconfig => _buckconfig} | 0 .../HelloWorld/{.flowconfig => _flowconfig} | 0 .../HelloWorld/{.gitignore => _gitignore} | 0 .../{.watchmanconfig => _watchmanconfig} | 0 6 files changed, 19 insertions(+), 2 deletions(-) rename local-cli/templates/HelloWorld/{.babelrc => _babelrc} (100%) rename local-cli/templates/HelloWorld/{.buckconfig => _buckconfig} (100%) rename local-cli/templates/HelloWorld/{.flowconfig => _flowconfig} (100%) rename local-cli/templates/HelloWorld/{.gitignore => _gitignore} (100%) rename local-cli/templates/HelloWorld/{.watchmanconfig => _watchmanconfig} (100%) diff --git a/local-cli/generator/copyProjectTemplateAndReplace.js b/local-cli/generator/copyProjectTemplateAndReplace.js index d202434d4..b169736c0 100644 --- a/local-cli/generator/copyProjectTemplateAndReplace.js +++ b/local-cli/generator/copyProjectTemplateAndReplace.js @@ -39,11 +39,10 @@ function copyProjectTemplateAndReplace(srcPath, destPath, newProjectName, option } const relativeFilePath = path.relative(srcPath, absoluteSrcFilePath); - const relativeRenamedPath = relativeFilePath + const relativeRenamedPath = dotFilePath(relativeFilePath) .replace(/HelloWorld/g, newProjectName) .replace(/helloworld/g, newProjectName.toLowerCase()); - let contentChangedCallback = null; if (options && options.upgrade && (!options.force)) { contentChangedCallback = (_, contentChanged) => { @@ -66,6 +65,24 @@ function copyProjectTemplateAndReplace(srcPath, destPath, newProjectName, option }); } +/** + * There are various dotfiles in the templates folder in the RN repo. We want + * these to be ignored by tools when working with React Native itself. + * Example: _babelrc file is ignored by Babel, renamed to .babelrc inside + * a real app folder. + * This is especially important for .gitignore because npm has some special + * behavior of automatically renaming .gitignore to .npmignore. + */ +function dotFilePath(path) { + if (!path) return path; + return path + .replace('_gitignore', '.gitignore') + .replace('_babelrc', '.babelrc') + .replace('_flowconfig', '.flowconfig') + .replace('_buckconfig', '.buckconfig') + .replace('_watchmanconfig', '.watchmanconfig'); +} + function upgradeFileContentChangedCallback( absoluteSrcFilePath, relativeDestPath, diff --git a/local-cli/templates/HelloWorld/.babelrc b/local-cli/templates/HelloWorld/_babelrc similarity index 100% rename from local-cli/templates/HelloWorld/.babelrc rename to local-cli/templates/HelloWorld/_babelrc diff --git a/local-cli/templates/HelloWorld/.buckconfig b/local-cli/templates/HelloWorld/_buckconfig similarity index 100% rename from local-cli/templates/HelloWorld/.buckconfig rename to local-cli/templates/HelloWorld/_buckconfig diff --git a/local-cli/templates/HelloWorld/.flowconfig b/local-cli/templates/HelloWorld/_flowconfig similarity index 100% rename from local-cli/templates/HelloWorld/.flowconfig rename to local-cli/templates/HelloWorld/_flowconfig diff --git a/local-cli/templates/HelloWorld/.gitignore b/local-cli/templates/HelloWorld/_gitignore similarity index 100% rename from local-cli/templates/HelloWorld/.gitignore rename to local-cli/templates/HelloWorld/_gitignore diff --git a/local-cli/templates/HelloWorld/.watchmanconfig b/local-cli/templates/HelloWorld/_watchmanconfig similarity index 100% rename from local-cli/templates/HelloWorld/.watchmanconfig rename to local-cli/templates/HelloWorld/_watchmanconfig