Files
js-scripts/scripts/sync.js
2025-04-05 16:57:26 +08:00

54 lines
2.1 KiB
JavaScript

#! /usr/bin/env runjs
var main = () => {
if ($ARGS && ($ARGS.length > 0)) {
var userHome = $$.prop('user.home');
if ($STR($ARGS[0]) == 'md') {
if (__.stringutil.isEmpty($$.shell().commands('which', 'markdowndocs.js').start()[0])) {
xprintln('[ERROR] Command markdowndocs.js not found.');
return;
}
$$.shell().inheritIO().commands('markdowndocs.js').run();
}
if ($STR($ARGS[0]) == 'ms') {
if (__.stringutil.isEmpty($$.shell().commands('which', 'markdownslide.js').start()[0])) {
xprintln('[ERROR] Command markdownslide.js not found.');
return;
}
$$.shell().inheritIO().commands('markdownslide.js').run();
}
}
println('[INFO] 1. Git Add All');
$$.shell().inheritIO().commands('git', 'add', '.').run();
println('[INFO] 2. Git Commit All');
$$.shell().inheritIO().commands('git', 'commit', '-a', '-m', "'auto sync'").run();
println('[INFO] 3. Git Push');
var gitPushError = false;
$$.shell().commands('git', 'push').run((out, err) => {
if (err && (err.string().contains('Connection closed by remote host')) || err.string().contains('make sure you have the correct access rights')) {
xprintln('[ERROR] Git Push FAILED!');
gitPushError = true;
}
});
if (gitPushError) { return; }
println('[INFO] 4. Sync Server Repo');
var path = $$.file('.').getAbsolutePath();
var indexOfPage = path.indexOf('/page');
if (indexOfPage > 0) {
path = path.substring(0, indexOfPage);
} else {
path = path.substring(0, path.length - 2);
}
path = path.substring(path.lastIndexOf('/') + 1);
var updatePageUrl = 'https://playsecurity.org/update_pages?' + path;
try {
println($$.httpRequest().connTimeout(600).readTimeout(10000).url(updatePageUrl).get());
} catch (e) {
xprintln('[WARN] Update pages failed, try cURL mode.');
$$.shell().inheritIO().ignoreError(true).commands('curl', updatePageUrl).run();
}
};
main();