editTemplate.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.changePlaceholderInTemplate = changePlaceholderInTemplate;
  6. function _fs() {
  7. const data = _interopRequireDefault(require("fs"));
  8. _fs = function () {
  9. return data;
  10. };
  11. return data;
  12. }
  13. function _path() {
  14. const data = _interopRequireDefault(require("path"));
  15. _path = function () {
  16. return data;
  17. };
  18. return data;
  19. }
  20. function _cliTools() {
  21. const data = require("@react-native-community/cli-tools");
  22. _cliTools = function () {
  23. return data;
  24. };
  25. return data;
  26. }
  27. var _walk = _interopRequireDefault(require("../../tools/walk"));
  28. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  29. /**
  30. TODO: This is a default placeholder for title in react-native template.
  31. We should get rid of this once custom templates adapt `placeholderTitle` in their configurations.
  32. */
  33. const DEFAULT_TITLE_PLACEHOLDER = 'Hello App Display Name';
  34. function replaceNameInUTF8File(filePath, projectName, templateName) {
  35. _cliTools().logger.debug(`Replacing in ${filePath}`);
  36. const fileContent = _fs().default.readFileSync(filePath, 'utf8');
  37. const replacedFileContent = fileContent.replace(new RegExp(templateName, 'g'), projectName).replace(new RegExp(templateName.toLowerCase(), 'g'), projectName.toLowerCase());
  38. if (fileContent !== replacedFileContent) {
  39. _fs().default.writeFileSync(filePath, replacedFileContent, 'utf8');
  40. }
  41. }
  42. function renameFile(filePath, oldName, newName) {
  43. const newFileName = _path().default.join(_path().default.dirname(filePath), _path().default.basename(filePath).replace(new RegExp(oldName, 'g'), newName));
  44. _cliTools().logger.debug(`Renaming ${filePath} -> file:${newFileName}`);
  45. _fs().default.renameSync(filePath, newFileName);
  46. }
  47. function shouldRenameFile(filePath, nameToReplace) {
  48. return _path().default.basename(filePath).includes(nameToReplace);
  49. }
  50. function shouldIgnoreFile(filePath) {
  51. return filePath.match(/node_modules|yarn.lock|package-lock.json/g);
  52. }
  53. const UNDERSCORED_DOTFILES = ['buckconfig', 'eslintrc.js', 'flowconfig', 'gitattributes', 'gitignore', 'prettierrc.js', 'watchmanconfig'];
  54. function processDotfiles(filePath) {
  55. const dotfile = UNDERSCORED_DOTFILES.find(e => filePath.includes(`_${e}`));
  56. if (dotfile === undefined) {
  57. return;
  58. }
  59. renameFile(filePath, `_${dotfile}`, `.${dotfile}`);
  60. }
  61. function changePlaceholderInTemplate({
  62. projectName,
  63. placeholderName,
  64. placeholderTitle = DEFAULT_TITLE_PLACEHOLDER,
  65. projectTitle = projectName
  66. }) {
  67. _cliTools().logger.debug(`Changing ${placeholderName} for ${projectName} in template`);
  68. (0, _walk.default)(process.cwd()).reverse().forEach(filePath => {
  69. if (shouldIgnoreFile(filePath)) {
  70. return;
  71. }
  72. if (!_fs().default.statSync(filePath).isDirectory()) {
  73. replaceNameInUTF8File(filePath, projectName, placeholderName);
  74. replaceNameInUTF8File(filePath, projectTitle, placeholderTitle);
  75. }
  76. if (shouldRenameFile(filePath, placeholderName)) {
  77. renameFile(filePath, placeholderName, projectName);
  78. }
  79. if (shouldRenameFile(filePath, placeholderName.toLowerCase())) {
  80. renameFile(filePath, placeholderName.toLowerCase(), projectName.toLowerCase());
  81. }
  82. processDotfiles(filePath);
  83. });
  84. }
  85. //# sourceMappingURL=editTemplate.js.map