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
| require('shelljs/global'); const path = require('path'); try { hexo.on('deployAfter', function() { const commitMessageIndex = process.argv.indexOf('-m'); const commitMessage = commitMessageIndex !== -1 ? process.argv.slice(commitMessageIndex + 1).join(' ') : "blog auto backup script's commit"; run(commitMessage); });
} catch (e) { console.log("产生了一个错误啊<( ̄3 ̄)> !,错误详情为:" + e.toString()); } function run(commitMessage) { if (!which('git')) { echo('Sorry, this script requires git'); exit(1); } else { echo("======================自动备份源码开始======================"); echo('当前项目目录:', path.join(__dirname, '..')); echo('本次提交信息:', commitMessage); cd(path.join(__dirname, '..')); if (exec('git add --all').code !== 0) { echo('Error: Git add failed'); exit(1); } if (exec('git commit -am "' + commitMessage + '"').code !== 0) { echo('Error: Git commit failed'); exit(1); } if (exec('git push origin master').code !== 0) { echo('Error: Git push failed'); exit(1); } echo("======================自动备份源码结束======================") } }
|