oak-cli/lib/rename.js

68 lines
3.2 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.renameProject = renameProject;
exports.rename = rename;
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');
// 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)}`)}`);
}
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);
}