Skip to content

Commit

Permalink
Feat: 增加页面模板创建
Browse files Browse the repository at this point in the history
  • Loading branch information
pandaoh committed Nov 1, 2024
1 parent 59a1740 commit 4d76396
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 2 deletions.
11 changes: 10 additions & 1 deletion bin/xcmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @Author: HxB
* @Date: 2022-04-25 16:27:06
* @LastEditors: DoubleAm
* @LastEditTime: 2024-10-31 18:20:12
* @LastEditTime: 2024-11-01 19:10:50
* @Description: 命令处理文件
* @FilePath: \js-xcmd\bin\xcmd.js
*/
Expand Down Expand Up @@ -33,6 +33,7 @@ const {
const { cmd } = require('../utils/cmd');
const { node2es6, sortJSON, mergeObj, versionUpgrade, isValidJson, jsonToExcel } = require('../utils/tools');
const { extractParamsFromFiles } = require('../utils/ast');
const { downloadTpl } = require('../utils/tpl');
const nodeCmd = require('node-cmd');
const readline = require('readline');

Expand Down Expand Up @@ -769,4 +770,12 @@ program
});
});

program
.option('add-umi-page [dir]', 'add-umi-page [dir]')
.command('add-umi-page [dir]')
.description('创建简单页面模板')
.action((dir) => {
downloadTpl('direct:http://cdn.biugle.cn/umi_page.zip', dir || '', ['PageCode', 'Author']);
});

program.parse(process.argv);
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "js-xcmd",
"version": "1.5.12",
"version": "1.5.13",
"description": "XCmd library for node.js.",
"main": "main.js",
"bin": {
Expand Down Expand Up @@ -38,6 +38,7 @@
"commander": "^9.2.0",
"download-git-repo": "^3.0.2",
"fs-extra": "^11.2.0",
"glob": "^11.0.0",
"node-cmd": "^5.0.0",
"rimraf": "^5.0.5",
"xlsx": "^0.18.5"
Expand Down
103 changes: 103 additions & 0 deletions utils/tpl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
const download = require('download-git-repo');
const fs = require('fs');
const path = require('path');
const glob = require('glob');
const readline = require('readline');

/**
* 下载模板仓库并替换其中的占位符
* @param {string} repo - Git 仓库地址
* @param {string} dest - 下载目录
* @param {object} replacements - 用户输入的替换内容
*/
function downloadAndReplace(repo, dest, replacements) {
download(repo, dest, (err) => {
if (err) return console.error('模板下载失败:', err);
console.log(`模板已下载到 ${dest}`);

// 获取下载目录中的所有文件路径并替换其中的占位符
const files = glob.sync(`${dest}/**/*`, { nodir: true });
files.forEach((file) => applyReplacements(file, replacements));

console.log('模板替换已完成');
});
}

/**
* 直接使用正则表达式替换文件中的占位符 [[[ ]]]
* @param {string} filePath - 文件路径
* @param {object} replacements - 要替换的值
*/
function applyReplacements(filePath, replacements) {
let content = fs.readFileSync(filePath, 'utf8');

// 遍历 replacements 对象,将 [[[key]]] 替换为对应的值
Object.keys(replacements).forEach((key) => {
const regex = new RegExp(`\\[\\[\\[${key}\\]\\]\\]`, 'g');
content = content.replace(regex, replacements[key]);
});

fs.writeFileSync(filePath, content, 'utf8');
}

/**
* 提示用户输入以填充模板中的占位符
* @param {Array<string>} questions - 要收集的键名数组
*/
function promptUserInputs(questions) {
if (!Array.isArray(questions) || questions.length === 0) {
console.error('请提供一个有效的占位符名称数组');
return Promise.resolve({});
}

const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});

const answers = {};
let index = 0;

return new Promise((resolve) => {
const askQuestion = () => {
if (index === questions.length) {
rl.close();
resolve(answers);
} else {
const question = questions[index];
rl.question(`请输入 ${question} :\n`, (answer) => {
answers[question] = answer;
index++;
askQuestion();
});
}
};

askQuestion();
});
}

/**
* 主函数,用于运行模板下载和替换
* @param {string} gitRepo - Git 仓库地址
* @param {string} downloadPath - 下载目录路径
* @param {Array<string>} options - 要收集的替换项
*/
async function downloadTpl(gitRepo, downloadPath, options) {
if (typeof gitRepo !== 'string' || typeof downloadPath !== 'string') {
console.error('请提供有效的 Git 仓库地址和下载路径');
return;
}

const answers = await promptUserInputs(options);
if (!answers.PageCode) {
console.error('请提供 PageCode');
return;
}
downloadPath = downloadPath || `./${answers.PageCode}`;
const dest = path.resolve(downloadPath);
console.log({ answers, downloadPath });
downloadAndReplace(gitRepo, dest, answers);
}

module.exports = { downloadTpl };

0 comments on commit 4d76396

Please sign in to comment.