diff --git a/fe-app-podkop/.env.example b/fe-app-podkop/.env.example new file mode 100644 index 0000000..b82d0b6 --- /dev/null +++ b/fe-app-podkop/.env.example @@ -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/ diff --git a/fe-app-podkop/watch-upload.js b/fe-app-podkop/watch-upload.js index db0b1ea..b05ea19 100644 --- a/fe-app-podkop/watch-upload.js +++ b/fe-app-podkop/watch-upload.js @@ -16,67 +16,81 @@ const config = { : { password: process.env.SFTP_PASS }), }; -const localDir = path.resolve(process.env.LOCAL_DIR || './dist'); -const remoteDir = process.env.REMOTE_DIR || '/www/luci-static/mypkg'; +const syncDirs = [ + { + 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) { - const relativePath = path.relative(localDir, filePath); - const remotePath = path.posix.join(remoteDir, relativePath); +async function uploadFile(filePath, baseDir, remoteBase) { + const relativePath = path.relative(baseDir, filePath); + const remotePath = path.posix.join(remoteBase, relativePath); - console.log(`Uploading: ${relativePath} -> ${remotePath}`); + console.log(`↑ Uploading: ${relativePath} -> ${remotePath}`); try { await sftp.fastPut(filePath, remotePath); - console.log(`Uploaded: ${relativePath}`); + console.log(`✓ Uploaded: ${relativePath}`); } catch (err) { - console.error(`Failed: ${relativePath}: ${err.message}`); + console.error(`✗ Failed: ${relativePath}: ${err.message}`); } } -async function deleteFile(filePath) { - const relativePath = path.relative(localDir, filePath); - const remotePath = path.posix.join(remoteDir, relativePath); +async function deleteFile(filePath, baseDir, remoteBase) { + const relativePath = path.relative(baseDir, filePath); + const remotePath = path.posix.join(remoteBase, relativePath); - console.log(`Removing: ${relativePath}`); + console.log(`⨯ Removing: ${relativePath}`); try { await sftp.delete(remotePath); - console.log(`Removed: ${relativePath}`); + console.log(`✓ Removed: ${relativePath}`); } catch (err) { - console.warn(`Could not delete ${relativePath}: ${err.message}`); + console.warn(`⚠ Could not delete ${relativePath}: ${err.message}`); } } async function uploadAllFiles() { - console.log('Uploading all files from', localDir); - - const files = await glob(`${localDir}/**/*`, { nodir: true }); - for (const file of files) { - await uploadFile(file); + for (const { local, remote } of syncDirs) { + console.log(`📂 Uploading all from ${local}`); + const files = await glob(`${local}/**/*`, { nodir: true }); + for (const file of files) { + await uploadFile(file, local, remote); + } } - - console.log('Initial upload complete!'); + console.log('✅ Initial upload complete!'); } async function main() { await sftp.connect(config); - console.log(`Connected to ${config.host}`); + console.log(`🔌 Connected to ${config.host}`); await uploadAllFiles(); - chokidar - .watch(localDir, { ignoreInitial: true }) - .on('all', async (event, filePath) => { - if (event === 'add' || event === 'change') { - await uploadFile(filePath); - } else if (event === 'unlink') { - await deleteFile(filePath); - } - }); + for (const { local, remote } of syncDirs) { + chokidar.watch(local, { ignoreInitial: true }).on('all', async (event, filePath) => { + if (event === 'add' || event === 'change') { + await uploadFile(filePath, local, remote); + } else if (event === 'unlink') { + await deleteFile(filePath, local, remote); + } + }); + } process.on('SIGINT', async () => { - console.log('Disconnecting...'); + console.log('👋 Disconnecting...'); await sftp.end(); process.exit(); }); } -main().catch(console.error); +main().catch((err) => { + console.error('💥 Fatal:', err); +});