101 lines
2.4 KiB
JavaScript
101 lines
2.4 KiB
JavaScript
'use strict';
|
|
|
|
const path = require('node:path');
|
|
const {
|
|
ensureCommandAvailable,
|
|
loadDeployConfig,
|
|
quoteForBash,
|
|
runSsh,
|
|
} = require('./deploy-lib.cjs');
|
|
|
|
function parseArgs(argv) {
|
|
const options = {
|
|
history: 0,
|
|
};
|
|
|
|
for (const item of argv) {
|
|
if (!item) continue;
|
|
if (item.startsWith('--history=')) {
|
|
const value = Number(item.slice('--history='.length));
|
|
if (Number.isFinite(value) && value > 0) {
|
|
options.history = Math.floor(value);
|
|
}
|
|
}
|
|
}
|
|
|
|
return options;
|
|
}
|
|
|
|
function queryCurrentRelease(config) {
|
|
const releaseFile = path.posix.join(config.stateDir, 'current-release.json');
|
|
const remoteScript = [
|
|
'set -euo pipefail',
|
|
`FILE=${quoteForBash(releaseFile)}`,
|
|
'if [ -f "$FILE" ]; then',
|
|
' cat "$FILE"',
|
|
'else',
|
|
' echo ONLINE_VERSION_NOT_FOUND',
|
|
'fi',
|
|
].join('\n');
|
|
|
|
const output = runSsh(config, remoteScript, { stdio: 'pipe' }).trim();
|
|
if (!output || output === 'ONLINE_VERSION_NOT_FOUND') {
|
|
console.log('ONLINE_VERSION_NOT_FOUND');
|
|
return;
|
|
}
|
|
|
|
try {
|
|
const parsed = JSON.parse(output);
|
|
console.log(JSON.stringify(parsed, null, 2));
|
|
} catch (error) {
|
|
console.log(output);
|
|
}
|
|
}
|
|
|
|
function queryReleaseHistory(config, limit) {
|
|
const historyFile = path.posix.join(config.stateDir, 'release-history.jsonl');
|
|
const remoteScript = [
|
|
'set -euo pipefail',
|
|
`FILE=${quoteForBash(historyFile)}`,
|
|
`LIMIT=${Number(limit)}`,
|
|
'if [ -f "$FILE" ]; then',
|
|
' tail -n "$LIMIT" "$FILE"',
|
|
'else',
|
|
' echo ONLINE_VERSION_HISTORY_EMPTY',
|
|
'fi',
|
|
].join('\n');
|
|
|
|
const output = runSsh(config, remoteScript, { stdio: 'pipe' }).trim();
|
|
if (!output || output === 'ONLINE_VERSION_HISTORY_EMPTY') {
|
|
console.log('ONLINE_VERSION_HISTORY_EMPTY');
|
|
return;
|
|
}
|
|
|
|
const lines = output.split(/\r?\n/).map(item => item.trim()).filter(Boolean);
|
|
const records = [];
|
|
for (const line of lines) {
|
|
try {
|
|
records.push(JSON.parse(line));
|
|
} catch (error) {
|
|
records.push({ raw: line });
|
|
}
|
|
}
|
|
console.log(JSON.stringify(records, null, 2));
|
|
}
|
|
|
|
function main() {
|
|
const envName = process.argv[2] || 'production';
|
|
const options = parseArgs(process.argv.slice(3));
|
|
|
|
ensureCommandAvailable('ssh', '请先安装 OpenSSH Client 并确保 ssh 在 PATH 中。');
|
|
|
|
const config = loadDeployConfig(envName);
|
|
if (options.history > 0) {
|
|
queryReleaseHistory(config, options.history);
|
|
return;
|
|
}
|
|
|
|
queryCurrentRelease(config);
|
|
}
|
|
|
|
main(); |