#!/usr/bin/env node
'use strict';
/**
* 一键发布脚本
*
* 流程:检查工作区干净 → syndicate → git commit → git push
*
* 适用于 git push 会自动触发部署的项目(如 VPS 上的 Git hook、
* GitHub Pages、Netlify 等)。
*
* 用法:npx hexo-mastodon-publish
*/
const { execSync } = require('child_process');
const path = require('path');
const BLOG_ROOT = process.cwd();
function run(cmd) {
return execSync(cmd, { cwd: BLOG_ROOT, encoding: 'utf8' }).trim();
}
async function main() {
// 1. 检查工作区是否有未提交的改动
const status = run('git status --porcelain');
if (status) {
console.error('工作区有未提交的改动,请先 commit:');
console.error(status);
process.exit(1);
}
// 2. 运行 syndicate
const { syndicate } = require('./syndicate');
const { syndicatedCount, modifiedFiles } = await syndicate();
if (syndicatedCount === 0 || modifiedFiles.length === 0) {
console.log('没有需要提交的改动。');
return;
}
// 3. 自动 commit
console.log('\n正在提交 syndication 改动...');
for (const file of modifiedFiles) {
run(`git add "${file}"`);
}
const titles = modifiedFiles.map(f => path.basename(f, '.md')).join(', ');
run(`git commit -m "更新 syndication URL: ${titles}"`);
console.log('✓ 已创建 commit');
// 4. Push 到当前分支的上游
console.log('\n正在推送到远程...');
run('git push');
console.log('✓ 已推送');
}
main().catch(e => {
console.error(`错误: ${e.message}`);
process.exit(1);
});