@tarojs/cli
Version:
cli tool for taro
104 lines • 4.66 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.changeDefaultNameInTemplate = void 0;
const helper_1 = require("@tarojs/helper");
const path = require("path");
const validate_1 = require("./validate");
const UNDERSCORED_DOTFILES = [
'buckconfig',
'eslintrc.js',
'flowconfig',
'gitattributes',
'gitignore',
'prettierrc.js',
'watchmanconfig',
'editorconfig',
'bundle',
'ruby-version',
'env.dev',
'env.test',
'env.prod',
];
const DEFAULT_RN_PROJECT_NAME = 'taroDemo';
function getTemplateName(cwd) {
const result = helper_1.fs.readFileSync(path.join(cwd, './ios/Podfile'), 'utf8').match(/target '(.*)' do/m);
const name = (result === null || result === void 0 ? void 0 : result[1]) || DEFAULT_RN_PROJECT_NAME;
return name;
}
function replaceNameInUTF8File(filePath, projectName, defaultName) {
return __awaiter(this, void 0, void 0, function* () {
const fileContent = yield helper_1.fs.readFile(filePath, 'utf8');
const replacedFileContent = fileContent
.replace(new RegExp(defaultName, 'g'), projectName)
.replace(new RegExp(defaultName.toLowerCase(), 'g'), projectName.toLowerCase());
if (fileContent !== replacedFileContent) {
yield helper_1.fs.writeFile(filePath, replacedFileContent, 'utf8');
}
});
}
function processDotfiles(filePath) {
return __awaiter(this, void 0, void 0, function* () {
const dotfile = UNDERSCORED_DOTFILES.find(e => filePath.includes(`_${e}`));
if (dotfile === undefined) {
return;
}
yield renameFile(filePath, `_${dotfile}`, `.${dotfile}`);
});
}
function walk(current) {
if (!helper_1.fs.lstatSync(current).isDirectory()) {
return [current];
}
const files = helper_1.fs.readdirSync(current).map(child => walk(path.join(current, child)));
const result = [];
return result.concat.apply([current], files);
}
function renameFile(filePath, oldName, newName) {
return __awaiter(this, void 0, void 0, function* () {
const newFileName = path.join(path.dirname(filePath), path.basename(filePath).replace(new RegExp(oldName, 'g'), newName));
yield helper_1.fs.rename(filePath, newFileName);
});
}
function shouldRenameFile(filePath, nameToReplace) {
return path.basename(filePath).includes(nameToReplace);
}
function shouldIgnoreFile(filePath) {
return filePath.match(/node_modules|yarn.lock|package-lock.json/g);
}
function changeDefaultNameInTemplate({ projectName, templatePath, projectPath }) {
return __awaiter(this, void 0, void 0, function* () {
const regex = (0, validate_1.validateProjectName)(projectName);
if (!regex) {
console.log(helper_1.chalk.yellow('因项目名称不符合 java package 包命名规则(只能包含字母、数字和下划线,且必须以字母开头),故项目默认名不做替换!'));
return;
}
const defaultName = getTemplateName(templatePath);
for (const filePath of walk(projectPath).reverse()) {
if (shouldIgnoreFile(filePath)) {
continue;
}
if (!(yield helper_1.fs.stat(filePath)).isDirectory()) {
yield replaceNameInUTF8File(filePath, projectName, defaultName);
}
if (shouldRenameFile(filePath, defaultName)) {
yield renameFile(filePath, defaultName, projectName);
}
if (shouldRenameFile(filePath, defaultName.toLowerCase())) {
yield renameFile(filePath, defaultName.toLowerCase(), projectName.toLowerCase());
}
yield processDotfiles(filePath);
}
console.log(`${helper_1.chalk.green('✔ ')}${helper_1.chalk.grey('项目名更新成功!')}`);
});
}
exports.changeDefaultNameInTemplate = changeDefaultNameInTemplate;
//# sourceMappingURL=editTemplate.js.map