feat: add bulk watch for fe/bin/lib directories

This commit is contained in:
divocat
2025-10-10 20:39:28 +03:00
parent 7bfb673b49
commit b90f520c68
2 changed files with 63 additions and 33 deletions

View File

@@ -0,0 +1,16 @@
SFTP_HOST=192.168.160.129
SFTP_PORT=22
SFTP_USER=root
SFTP_PASS=
# you can use key if needed
# SFTP_PRIVATE_KEY=~/.ssh/id_rsa
LOCAL_DIR_FE=../luci-app-podkop/htdocs/luci-static/resources/view/podkop
REMOTE_DIR_FE=/www/luci-static/resources/view/podkop
LOCAL_DIR_BIN=../podkop/files/usr/bin/
REMOTE_DIR_BIN=/usr/bin/
LOCAL_DIR_LIB=../podkop/files/usr/lib/
REMOTE_DIR_LIB=/usr/lib/podkop/

View File

@@ -16,67 +16,81 @@ const config = {
: { password: process.env.SFTP_PASS }), : { password: process.env.SFTP_PASS }),
}; };
const localDir = path.resolve(process.env.LOCAL_DIR || './dist'); const syncDirs = [
const remoteDir = process.env.REMOTE_DIR || '/www/luci-static/mypkg'; {
local: path.resolve(process.env.LOCAL_DIR_FE ?? '../luci-app-podkop/htdocs/luci-static/resources/view/podkop'),
remote: process.env.REMOTE_DIR_FE ?? '/www/luci-static/resources/view/podkop',
},
{
local: path.resolve(process.env.LOCAL_DIR_BIN ?? '../podkop/files/usr/bin/'),
remote: process.env.REMOTE_DIR_BIN ?? '/usr/bin/',
},
{
local: path.resolve(process.env.LOCAL_DIR_LIB ?? '../podkop/files/usr/lib/'),
remote: process.env.REMOTE_DIR_LIB ?? '/usr/lib/podkop/',
},
];
async function uploadFile(filePath) { async function uploadFile(filePath, baseDir, remoteBase) {
const relativePath = path.relative(localDir, filePath); const relativePath = path.relative(baseDir, filePath);
const remotePath = path.posix.join(remoteDir, relativePath); const remotePath = path.posix.join(remoteBase, relativePath);
console.log(`Uploading: ${relativePath} -> ${remotePath}`); console.log(`Uploading: ${relativePath} -> ${remotePath}`);
try { try {
await sftp.fastPut(filePath, remotePath); await sftp.fastPut(filePath, remotePath);
console.log(`Uploaded: ${relativePath}`); console.log(`Uploaded: ${relativePath}`);
} catch (err) { } catch (err) {
console.error(`Failed: ${relativePath}: ${err.message}`); console.error(`Failed: ${relativePath}: ${err.message}`);
} }
} }
async function deleteFile(filePath) { async function deleteFile(filePath, baseDir, remoteBase) {
const relativePath = path.relative(localDir, filePath); const relativePath = path.relative(baseDir, filePath);
const remotePath = path.posix.join(remoteDir, relativePath); const remotePath = path.posix.join(remoteBase, relativePath);
console.log(`Removing: ${relativePath}`); console.log(` Removing: ${relativePath}`);
try { try {
await sftp.delete(remotePath); await sftp.delete(remotePath);
console.log(`Removed: ${relativePath}`); console.log(`Removed: ${relativePath}`);
} catch (err) { } catch (err) {
console.warn(`Could not delete ${relativePath}: ${err.message}`); console.warn(`Could not delete ${relativePath}: ${err.message}`);
} }
} }
async function uploadAllFiles() { async function uploadAllFiles() {
console.log('Uploading all files from', localDir); for (const { local, remote } of syncDirs) {
console.log(`📂 Uploading all from ${local}`);
const files = await glob(`${localDir}/**/*`, { nodir: true }); const files = await glob(`${local}/**/*`, { nodir: true });
for (const file of files) { for (const file of files) {
await uploadFile(file); await uploadFile(file, local, remote);
}
} }
console.log('✅ Initial upload complete!');
console.log('Initial upload complete!');
} }
async function main() { async function main() {
await sftp.connect(config); await sftp.connect(config);
console.log(`Connected to ${config.host}`); console.log(`🔌 Connected to ${config.host}`);
await uploadAllFiles(); await uploadAllFiles();
chokidar for (const { local, remote } of syncDirs) {
.watch(localDir, { ignoreInitial: true }) chokidar.watch(local, { ignoreInitial: true }).on('all', async (event, filePath) => {
.on('all', async (event, filePath) => { if (event === 'add' || event === 'change') {
if (event === 'add' || event === 'change') { await uploadFile(filePath, local, remote);
await uploadFile(filePath); } else if (event === 'unlink') {
} else if (event === 'unlink') { await deleteFile(filePath, local, remote);
await deleteFile(filePath); }
} });
}); }
process.on('SIGINT', async () => { process.on('SIGINT', async () => {
console.log('Disconnecting...'); console.log('👋 Disconnecting...');
await sftp.end(); await sftp.end();
process.exit(); process.exit();
}); });
} }
main().catch(console.error); main().catch((err) => {
console.error('💥 Fatal:', err);
});