JS FTP Synchronizer - JavaScript Static Site Generator
Works under Node.js, runs a local image of the website.
A PHP version of the same program is also available: PHP FTP Synchronizer.
The program compares the local file with the duplicate, also local, in the backup folder. If the size is different or the content comparison shows a difference, the file is updated at your site.
The file is sent to the remote site in the duplicate folder, or if it is different, this backup directory becomes the exact image of the remote directory.
The cloned directory solution is fast and secure, it also allows you to have a copy, and it is advisable to place this backup folder on a removable module.
This JavaScript program uses the asynchronous JSFtp framework to send pages to a remote site. Performing a sequence of such operations in asynchronous mode is a real call. It is especially difficult to create folders when they are not present, and then place files in them, all in a separate order, since the server performs operations when appropriate, and not on demand.
The simplest solution was found with the following algorithm:
function ftpSend(src, rmt, rdir) {
var connection = new JSFtp(OPTIONS)
connection.put(src, rmt, function(err) {
if (err) {
connection.raw.mkd(rdir, function(err, data) {
connection.put(src, rmt, function(err) {
if(err) {
return console.log("Error, file not uploaded.")
}
connection.raw.quit(function() {})
return
});
});
return
}
connection.raw.quit(function() {})
});
}
We send the file anyway, if an error occurs, we assume that the folder does not exist, so we create it. Then send the file again. If the error persists, only then will it be indicated that the file cannot be downloaded.
In addition, async/awais is used to obtain the sequence whenever possible.
Operation manual
The site is updated with the following command, which is more convenient to place in a batch file:
node sync.js [options/paramètres] répsource
A source is a local folder that contains files to be placed online. Other required parameters:
- -furl: URL in ftp format.
- -ddirectory: Directory on remote site where files will be stored .
- -backup: path to the local backup directory that will be the remote image directory.
- -login: ftp username .
- -passe: ftp user password .
You can add the following options:
- -q: (quiet) silent mode, displayed error messages only.
- -v: (verbose) displays all operation details . -t: (test)
- shows the operations that will be performed, so sent files do not perform them .
Example:
node sync.js -lmonlogin -pmonmotdepasse ftp.example.com -drepdistant -bbackuplocal replocal
Download and Install
To install the program:
- Extract content from archive.
- Download and install Node.js if necessary.
- You can install jsftp: npm install jsftp (it is already in the archive).