Fix handling of dotfiles in 'react-native init' and 'react-native upgrade'

Summary:
Followup for CLI rewrite (a477aec10d). 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
This commit is contained in:
Martin Konicek 2016-11-21 12:52:36 -08:00 committed by Facebook Github Bot
parent 6526548b50
commit 9712d335e2
6 changed files with 19 additions and 2 deletions

View File

@ -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,