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.

You may add the following options.

Exemple:

node sync.js -lmylogin -pmypass ftp.example.com -dremotedir -bbackupdir localdir

Download and install

To install the program:

  1. Extract the contents of the archive.
  2. Download and install Node.js if necessary.
  3. You may install jsftp: npm install jsftp (it is already in the archive actually).