UNPKG

@trap_stevo/filetide-cli

Version:

Seamless, real-time file transfers between devices with effortless connectivity. A powerful command-line tool built for instant, secure, and cross-platform file sharing, enabling efficient and reliable data transfers in any network environment.

161 lines (160 loc) 4.58 kB
#!/usr/bin/env node "use strict"; const FileTideCLI = require("./FileTideCLI.js"); const os = require("os"); const args = process.argv.slice(2); let activeTransfers = false; let transferringFiles; const commands = { "request": { description: "Request files / directories from the desired client!", command: async (client, clientName, sourcePath, destPath) => { await client.processCommand(`request "${clientName}" "${sourcePath}" "${destPath}"`); transferringFiles = executeAfterInactivity(() => { activeTransfers = false; }, 10000); await waitForTransferToComplete(client); } }, "send": { description: "Sends files / directories to the desired client!", command: async (client, clientName, sourcePath, destPath) => { activeTransfers = true; await client.processCommand(`send "${clientName}" "${sourcePath}" "${destPath}"`); transferringFiles = executeAfterInactivity(() => { activeTransfers = false; }); await waitForTransferToComplete(client); } }, "channel-": { description: "Updates the tide domain (room) of the connection!", command: async (client, channelName) => { await client.processCommand(`channel- "${channelName}"`); } }, "tide-": { description: "Updates the tide (connection URL)!", command: async (client, tideUrl) => { await client.processCommand(`tide- "${tideUrl}"`); } }, "cmd-": { description: "List the current available commands!", command: () => { FileTideCLI.listCommands(); } }, "id-": { description: "Updates your display name!", command: async (client, clientName) => { await client.processCommand(`id- "${clientName}"`); } }, "online-": { description: "See all the connected clients!", command: async client => { client.getOnlineClients(false); await Delay(569); } }, "shrtcs-": { description: "Lists the available path shortcuts for seamless transfers!", command: () => { FileTideCLI.getShortcuts(); } }, "clear-": { description: "Clears the current console.", command: () => { client.clear(); } } }; function executeAfterInactivity(fn, time = 1269) { let timer = null; return function (...args) { if (timer) { clearTimeout(timer); } timer = setTimeout(() => { fn(...args); }, time); }; } ; function waitForTransferToComplete(client) { return new Promise(resolve => { const checkInterval = setInterval(() => { if (!activeTransfers) { clearInterval(checkInterval); resolve(); } }, 1000); }); } ; function Delay(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } ; async function connectAndExecute(args) { let tideUrl = "http://localhost:9269"; let deviceName = os.hostname(); let currentRoom = "file-room"; let commandArgs = args; let commandKey; if (args[0].startsWith("http://") || args[0].startsWith("https://")) { tideUrl = args[0]; commandArgs = args.slice(1); commandKey = commandArgs[0]; commandArgs = commandArgs.slice(1); } else { commandKey = args[0]; commandArgs = args.slice(1); } const noConnectionRequired = ["cmd-", "clear-", "shrtcs-"]; if (noConnectionRequired.includes(commandKey)) { try { if (commands[commandKey]) { await commands[commandKey].command(null, ...commandArgs); } else { console.log("Unknown command. Use 'cmd-' to list available commands."); } } catch (error) { console.error("Error during command execution ~ ", error.message); } finally { process.exit(0); } } else { const client = new FileTideCLI(deviceName, tideUrl, currentRoom, true, true, true, false, {}, {}, {}, {}, true, { onTransferProgress: data => { transferringFiles(); }, onTransferStatus: data => { transferringFiles(); }, onTransferStart: data => { transferringFiles(); } }); try { if (commands[commandKey]) { await commands[commandKey].command(client, ...commandArgs); } else { console.log("Unknown command. Use 'cmd-' to list available commands."); } client.exitFileTideCLI(); } catch (error) { console.error("Error during command execution ~ ", error.message); } finally { process.exit(0); } } } if (args.length > 0) { connectAndExecute(args); } else { console.log("No command provided. Use 'cmd-' to list available commands."); process.exit(0); }