UNPKG

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