JS FTP Synchronizer - Static Site Generator in JavaScript
Work on Node.js, put online a local image of a website.
A PHP version of the same program is also available : PHP FTP Synchronizer.
The program compares this file with a double, also local, in a backup directory. If the size is different or if the comparison shows a difference of content, the file is updated on the site. In all cases the file is sent if it does not exist or is modified since the last update.
The solution to use a clone directory is fast and safe, and provides a backup always updated. It is better to put this clone folder on a removable unit.
This program uses the asynchronous JavaScript framework JSFtp to send pages to the remote site. Performing a series of such operations asynchronously is a real challenge. In particular, it is difficult to create folders when they do not exist, then move files, all this piecemeal since the server performs the operations when it suits him and not when asked.
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() {})
});
}
The file is sent in all cases, if an error occurs, it is assumed that the directory does not exist, therefore it is created. Then file is sent again. If the error persists, then only it is shown that the file is not downloaded.
The program also use async/away to have a synchronous sequence when possible.
How to use
The site is updated with the following command, it is more convenient to put in a batch file:
node sync.js [options/parameters] sourcedir
The source is the local directory where the files to upload are stored. The other required parameters are as follows.
- -furl: a URL in the FTP format.
- -ddirectory: The directory on the remote site where the files will be stored.
- -bbackup: the path of a local backup directory which will be an image of the remote directory.
- -llogin: the name of the FTP user.
- -ppassword: the password of the FTP user.
You may add the following options.
- -q: (quiet) only error messages are displayed.
- -v: (verbose) displays all the details of operations.
- -t: (test) shows the operations to be undertaken, so uploaded files, without doing it really.
Exemple:
node sync.js -lmylogin -pmypass ftp.example.com -dremotedir -bbackupdir localdir
Download and install
To install the program:
- Extract the contents of the archive.
- Download and install Node.js if necessary.
- You may install jsftp: npm install jsftp (it is already in the archive actually).