~cytrogen/hexo-mastodon-syndicate

ref: b0f22fc23c9d9438f5b2346ec02961dae1d4596c hexo-mastodon-syndicate/publish.js -rw-r--r-- 1.6 KiB
b0f22fc2 — Cytrogen 初始提交:hexo-mastodon-syndicate a month ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/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);
});