UNPKG

3.59 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 isPackageJson = _path().default.basename(filePath) === 'package.json';
52
53 const fileContent = _fs().default.readFileSync(filePath, 'utf8');
54
55 const replacedFileContent = fileContent.replace(new RegExp(templateName, 'g'), projectName).replace(new RegExp(templateName.toLowerCase(), 'g'), projectName.toLowerCase());
56
57 if (fileContent !== replacedFileContent) {
58 _fs().default.writeFileSync(filePath, replacedFileContent, 'utf8');
59 }
60
61 if (isPackageJson) {
62 _fs().default.writeFileSync(filePath, fileContent.replace(templateName, projectName.toLowerCase()), 'utf8');
63 }
64}
65
66function renameFile(filePath, oldName, newName) {
67 const newFileName = _path().default.join(_path().default.dirname(filePath), _path().default.basename(filePath).replace(new RegExp(oldName, 'g'), newName));
68
69 _cliTools().logger.debug(`Renaming ${filePath} -> file:${newFileName}`);
70
71 _fs().default.renameSync(filePath, newFileName);
72}
73
74function shouldRenameFile(filePath, nameToReplace) {
75 return _path().default.basename(filePath).includes(nameToReplace);
76}
77
78function shouldIgnoreFile(filePath) {
79 return filePath.match(/node_modules|yarn.lock|package-lock.json/g);
80}
81
82const UNDERSCORED_DOTFILES = ['buckconfig', 'eslintrc.js', 'flowconfig', 'gitattributes', 'gitignore', 'prettierrc.js', 'watchmanconfig', 'editorconfig'];
83
84function processDotfiles(filePath) {
85 const dotfile = UNDERSCORED_DOTFILES.find(e => filePath.includes(`_${e}`));
86
87 if (dotfile === undefined) {
88 return;
89 }
90
91 renameFile(filePath, `_${dotfile}`, `.${dotfile}`);
92}
93
94function changePlaceholderInTemplate({
95 projectName,
96 placeholderName,
97 placeholderTitle = DEFAULT_TITLE_PLACEHOLDER,
98 projectTitle = projectName
99}) {
100 _cliTools().logger.debug(`Changing ${placeholderName} for ${projectName} in template`);
101
102 (0, _walk.default)(process.cwd()).reverse().forEach(filePath => {
103 if (shouldIgnoreFile(filePath)) {
104 return;
105 }
106
107 if (!_fs().default.statSync(filePath).isDirectory()) {
108 replaceNameInUTF8File(filePath, projectName, placeholderName);
109 replaceNameInUTF8File(filePath, projectTitle, placeholderTitle);
110 }
111
112 if (shouldRenameFile(filePath, placeholderName)) {
113 renameFile(filePath, placeholderName, projectName);
114 }
115
116 if (shouldRenameFile(filePath, placeholderName.toLowerCase())) {
117 renameFile(filePath, placeholderName.toLowerCase(), projectName.toLowerCase());
118 }
119
120 processDotfiles(filePath);
121 });
122}
123
124//# sourceMappingURL=editTemplate.js.map
\No newline at end of file