Text to vertical video in 100+ styles: AI codes each scene (HTML/SVG/WebGL), renders a 9:16 MP4 with music and English/Vietnamese voice. MCP, CLI and REST for AI agents.
#!/usr/bin/env node
// Kiểm bản mới trên GitHub trước khi chạy app (npm start gọi qua "prestart").
// node scripts/update.mjs : kiểm, có bản mới thì hỏi cập nhật (mặc định Có)
// node scripts/update.mjs --json : chỉ kiểm, in JSON (server dùng để hiện thông báo trong giao diện)
// node scripts/update.mjs --yes : có bản mới thì cập nhật luôn, không hỏi
// Bỏ qua: LUMREELS_SKIP_UPDATE=1. Máy không có mạng / không phải bản clone git thì bỏ qua êm, app vẫn chạy.
import fs from 'node:fs';
import path from 'node:path';
import readline from 'node:readline';
import { spawnSync } from 'node:child_process';
import { fileURLToPath } from 'node:url';
const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
const git = (args, timeout = 15000) => spawnSync('git', args, { cwd: ROOT, encoding: 'utf8', timeout, windowsHide: true });
export function checkUpdate({ fetch = true } = {}) {
if (!fs.existsSync(path.join(ROOT, '.git'))) return { ok: false, reason: 'Không phải bản clone git (tải zip?), không tự kiểm được bản mới' };
if (git(['--version'], 5000).status !== 0) return { ok: false, reason: 'Máy chưa có git' };
const up = git(['rev-parse', '--abbrev-ref', '--symbolic-full-name', '@{u}'], 5000);
if (up.status !== 0) return { ok: false, reason: 'Nhánh hiện tại không theo dõi nhánh nào trên GitHub' };
if (fetch) {
const f = git(['fetch', '--quiet', '--no-tags'], 20000);
if (f.status !== 0) return { ok: false, reason: 'Không kết nối được GitHub (mất mạng?)' };
}
const behind = Number(git(['rev-list', '--count', 'HEAD..@{u}']).stdout.trim()) || 0;
const ahead = Number(git(['rev-list', '--count', '@{u}..HEAD']).stdout.trim()) || 0;
const dirty = git(['status', '--porcelain', '--untracked-files=no']).stdout.trim().length > 0;
const changes = behind ? git(['log', '--format=%s', '-n', '8', 'HEAD..@{u}']).stdout.trim().split(/\r?\n/).filter(Boolean) : [];
return { ok: true, behind, ahead, dirty, upstream: up.stdout.trim(), changes, checkedAt: new Date().toISOString() };
}
export function applyUpdate(log = console.log) {
const before = git(['rev-parse', 'HEAD']).stdout.trim();
const pull = git(['pull', '--ff-only', '--quiet'], 120000);
if (pull.status !== 0) return { ok: false, error: (pull.stderr || pull.stdout || '').trim().slice(-400) };
const changed = git(['diff', '--name-only', before, 'HEAD']).stdout.split(/\r?\n/);
if (changed.some((f) => f === 'package.json' || f === 'package-lock.json')) {
log('Thư viện thay đổi, đang chạy npm install…');
const npm = spawnSync(process.platform === 'win32' ? 'npm.cmd' : 'npm', ['install', '--no-audit', '--no-fund'], { cwd: ROOT, stdio: 'inherit', shell: process.platform === 'win32', timeout: 15 * 60 * 1000 });
if (npm.status !== 0) return { ok: false, error: 'npm install lỗi, chạy tay: npm install' };
}
return { ok: true, files: changed.filter(Boolean).length };
}
function ask(q) {
return new Promise((resolve) => {
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
const t = setTimeout(() => { rl.close(); console.log('(không trả lời sau 30 giây, chạy bản hiện tại)'); resolve(false); }, 30000);
rl.question(q, (a) => { clearTimeout(t); rl.close(); resolve(!/^\s*(n|no|k|không|khong)\s*$/i.test(a)); });
});
}
async function main() {
const argv = process.argv.slice(2);
if (argv.includes('--json')) { process.stdout.write(JSON.stringify(checkUpdate())); return; }
if (process.env.LUMREELS_SKIP_UPDATE === '1') return;
console.log('LumReels: đang kiểm bản mới trên GitHub…');
const r = checkUpdate();
if (!r.ok) { console.log(` Bỏ qua kiểm bản mới: ${r.reason}.`); return; }
if (!r.behind) { console.log(' Đang dùng bản mới nhất.'); return; }
console.log(`\n ★ Có ${r.behind} cập nhật mới:`);
for (const c of r.changes) console.log(' • ' + c);
if (r.behind > r.changes.length) console.log(` • … và ${r.behind - r.changes.length} thay đổi khác`);
if (r.dirty || r.ahead) {
console.log(`\n Máy có ${r.dirty ? 'file đã sửa tay' : ''}${r.dirty && r.ahead ? ' và ' : ''}${r.ahead ? 'commit riêng chưa có trên GitHub' : ''}, nên không tự cập nhật.`);
console.log(' Tự gộp bằng: git stash && git pull && git stash pop (rồi npm install nếu cần)\n');
return;
}
const yes = argv.includes('--yes') || process.env.LUMREELS_AUTO_UPDATE === '1' || (process.stdin.isTTY ? await ask('\n Cập nhật ngay trước khi chạy? [Y/n] ') : false);
if (!yes) { console.log(' Chạy bản hiện tại. Cập nhật sau bằng: git pull && npm install\n'); return; }
const u = applyUpdate();
if (u.ok) console.log(` Đã cập nhật (${u.files} file). Đang khởi động bản mới…\n`);
else console.log(` Cập nhật lỗi: ${u.error}\n Chạy bản hiện tại. Cập nhật tay: git pull && npm install\n`);
}
if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
main().catch((e) => { console.log(' Bỏ qua kiểm bản mới: ' + e.message); });
}