"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.rename = exports.renameProject = void 0; const path_1 = require("path"); const fs_1 = require("fs"); const editTemplate_1 = require("@react-native-community/cli/build/commands/init/editTemplate"); const tip_style_1 = require("./tip-style"); const process_1 = require("process"); async function renameProject(dir, name, title, placeholderName, placeholderTitle) { // replace package.json中的name const packageJsonFilePath = (0, path_1.join)(dir, 'package.json'); const packageJsonJson = require(packageJsonFilePath); packageJsonJson.name = name; const newPackageJsonContent = JSON.stringify(packageJsonJson, undefined, 4); (0, fs_1.writeFileSync)(packageJsonFilePath, newPackageJsonContent); // replace web下html的title // todo,这个替换方法不是很优雅,以后再改 const htmlFilePath = (0, path_1.join)(dir, 'web', 'public', 'index.html'); const htmlContent = (0, fs_1.readFileSync)(htmlFilePath, 'utf-8'); const newHtmlContent = htmlContent.replace(new RegExp(placeholderTitle, 'g'), title).replace(new RegExp(placeholderTitle.toLowerCase(), 'g'), title.toLowerCase()); (0, fs_1.writeFileSync)(htmlFilePath, newHtmlContent, 'utf-8'); // index.tsx下的title const indexTsxPath = (0, path_1.join)(dir, 'web', 'src/index.tsx'); const tsxContent = (0, fs_1.readFileSync)(indexTsxPath, 'utf-8'); const newTsxlContent = tsxContent.replace(new RegExp(placeholderName, 'g'), name).replace(new RegExp(placeholderName.toLowerCase(), 'g'), name.toLowerCase()); (0, fs_1.writeFileSync)(indexTsxPath, newTsxlContent, 'utf-8'); // replace wechatMp下project.config.json中的projectname // todo,现在这个是在wechatMp/src目录下的,可能是搞错了,待修正 const pcjFilePath = (0, path_1.join)(dir, 'wechatMp', 'src', 'project.config.json'); const pcjJson = require(pcjFilePath); pcjJson.projectname = title; const newPcjContent = JSON.stringify(pcjJson, undefined, 4); (0, fs_1.writeFileSync)(pcjFilePath, newPcjContent); // src/locales/common/zh_CN.json中的name const commonLocalePath = (0, path_1.join)(dir, 'src', 'locales', 'common', 'zh_CN.json'); const clpJson = require(commonLocalePath); clpJson.name = title; const newClpJson = JSON.stringify(clpJson, undefined, 4); (0, fs_1.writeFileSync)(commonLocalePath, newClpJson); // replace native下的相关数据 const cwd = process.cwd(); process.chdir((0, path_1.join)(dir, 'native')); await (0, editTemplate_1.changePlaceholderInTemplate)({ projectName: name, projectTitle: title, placeholderName, placeholderTitle, }); process.chdir(cwd); (0, tip_style_1.Success)(`${(0, tip_style_1.success)(`Change project name to ${(0, tip_style_1.primary)(name)}, project title to ${(0, tip_style_1.primary)(title)}`)}`); } exports.renameProject = renameProject; async function rename(cmd) { const { oldName, newName, oldTitle, newTitle } = cmd; if (!oldName) { (0, tip_style_1.Error)((0, tip_style_1.error)('oldName must not be empty')); (0, process_1.exit)(-1); } if (!newName) { (0, tip_style_1.Error)((0, tip_style_1.error)('newName must not be empty')); (0, process_1.exit)(-1); } if (!oldTitle) { (0, tip_style_1.Error)((0, tip_style_1.error)('oldTitle must not be empty')); (0, process_1.exit)(-1); } if (!newTitle) { (0, tip_style_1.Error)((0, tip_style_1.error)('newTitle must not be empty')); (0, process_1.exit)(-1); } renameProject(process.cwd(), newName, newTitle, oldName, oldTitle); } exports.rename = rename;