JSFiddle - React, Tailwind, and code Playground
by Ace Tributon
JavaScript
const
ftp=new (require('ftp'))(),
fs=require('fs'),
serverFolder='./Path/Of/Server/',
localFolder='./Path/Of/Local/',
file='some.json'
;
//run server if its ready
ftp.on('ready',()=>{
//collect a list of files from the server folder
ftp.list(serverFolder+file,(errList,list)=>{
return errList ||
typeof list === 'object' &&
list.forEach($file=>{
//if the individual file matches, resume to download the file
$file.name===file&&(
ftp.get(serverFolder+file,(errGet,stream)=>{
return errGet||(
console.log('files matched! cdarry onto the operation...'),
stream.pipe(fs.createReadStream(localFolder+file)),
stream.once('close',()=>{
//check if the file has a proper size
fs.stat(localFolder+file,(errStat,stat)=>{
if (errStat || stat.size === 0){(
//will destroy server connection if bytes = 0
ftp.destroy(),
console.log('the file has no value')
)}else{(
//uploads if the file has a size, edits, and ships
editThisFile(),
ftp.put(
fs.createReadStream(localFolder+file),
serverFolder+file,err=>{
return err||(
ftp.end(),
console.log('process is complete!')
)
})
//editThisFile() is a place-holder editor
//edits by path, and object
)}
})
})
...