hexo-afdian-plugin
Version:
爱发电插件,用于展示爱发电赞助信息
107 lines (93 loc) • 3.29 kB
JavaScript
const fs = require('fs');
const path = require('path');
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
console.log('欢迎使用 Hexo 爱发电插件安装向导!');
console.log('本向导将帮助您配置爱发电插件。\n');
// 检查是否在Hexo博客根目录
if (!fs.existsSync('_config.yml')) {
console.error('错误:请在Hexo博客根目录下运行此安装向导。');
process.exit(1);
}
// 读取现有配置
let hexoConfig = '';
try {
hexoConfig = fs.readFileSync('_config.yml', 'utf8');
} catch (err) {
console.error('错误:无法读取_config.yml文件。', err);
process.exit(1);
}
// 检查是否已有爱发电配置
if (hexoConfig.includes('afdian:')) {
console.log('检测到已有爱发电配置,是否覆盖?(y/n)');
rl.question('> ', (answer) => {
if (answer.toLowerCase() !== 'y') {
console.log('安装已取消。');
rl.close();
return;
}
startConfiguration();
});
} else {
startConfiguration();
}
function startConfiguration() {
console.log('\n请输入您的爱发电用户ID(在 https://afdian.com/dashboard/dev 中获取):');
rl.question('> ', (userId) => {
if (!userId.trim()) {
console.error('错误:用户ID不能为空。');
rl.close();
return;
}
console.log('\n请输入您的爱发电Token(在 https://afdian.com/dashboard/dev 中获取):');
rl.question('> ', (token) => {
if (!token.trim()) {
console.error('错误:Token不能为空。');
rl.close();
return;
}
console.log('\n请输入您的爱发电赞助链接(例如:https://afdian.com/你的用户名):');
rl.question('> ', (sponsorUrl) => {
sponsorUrl = sponsorUrl.trim() || 'https://afdian.com/';
// 生成配置
const config = `
# 爱发电插件配置
afdian:
# 必填项
userId: '${userId}'
token: '${token}'
# 可选项
title: '赞助名单'
description: '感谢赞赏的人,因为你们,让我感受到写博客这件事情能够给你们创造价值。这会让我在这条路上走得更远。'
buttonText: '赞赏作者'
sponsorUrl: '${sponsorUrl}'
sponsorNumber: 66
emptyText: '暂无赞助'
cacheTime: 60
`;
// 更新配置文件
try {
if (hexoConfig.includes('afdian:')) {
// 替换现有配置
const regex = /# 爱发电插件配置\nafdian:[\s\S]*?(?=\n\w|$)/;
hexoConfig = hexoConfig.replace(regex, config.trim());
} else {
// 添加新配置
hexoConfig += config;
}
fs.writeFileSync('_config.yml', hexoConfig);
console.log('\n配置已成功添加到_config.yml文件!');
} catch (err) {
console.error('错误:无法写入_config.yml文件。', err);
}
console.log('\n安装完成!您现在可以通过访问 /afdian/ 路径查看您的爱发电赞助页面。');
console.log('如需更多帮助,请参阅README.md文件。');
rl.close();
});
});
});
}