const config = require('config'),
    _ = require('lodash'),
    fs = require('fs'),
    path = require('path');

const SETUPSTATUSFILE = 'setup-status.json';

function isSetupNecessary() {
    let result = true;
    const cfgPath = path.join(process.env.NODE_CONFIG_DIR_HOST, SETUPSTATUSFILE);

    // Does file exist?
    if (fs.existsSync(cfgPath)) {
        try {
            // Does it have the right keys?
            const setupCFG = JSON.parse(fs.readFileSync(cfgPath));
            if (_.get(setupCFG, 'setupComplete', false)) {
                result = false;
            }
        } catch(exc) {
            // Any error accessing the file means that setup is required.
            console.debug(exc.message);
        }
    }

    return result;
}

function run() {
    if (isSetupNecessary()) {
        console.info('Setup IS necessary.');
    } else {
        console.info('Setup is NOT necessary.');
    }
}

exports.run = run;

@@ -108,216 +108,337 @@
 th')
-;%0A%0Aconst SETUPSTATUSFILE = 'setup-status.json';%0A%0Afunction isSetupNecessary() %7B%0A    let result = true;%0A    const cfgPath = path.join(process.env.NODE_CONFIG_DIR_HOST, SETUPSTATUSFILE);%0A%0A    // Does file exist?
+,%0A    util = require('util'),%0A    push = require('couchdb-push');%0A%0Aconst SETUPSTATUSFILE = 'setup-status.json';%0Aconst SETUPSTATUSPATH = path.join(process.env.NODE_CONFIG_DIR_HOST, SETUPSTATUSFILE);%0A%0Afunction isSetupNecessary() %7B%0A    let result = true;%0A%0A    // Does file exist?%0A    console.debug(%60Looking for $%7BSETUPSTATUSPATH%7D%60);
 %0A   
@@ -456,23 +456,31 @@
 stsSync(
-cfgPath
+SETUPSTATUSPATH
 )) %7B%0A   
@@ -594,15 +594,23 @@
 ync(
-cfgPath
+SETUPSTATUSPATH
 ));%0A
@@ -903,16 +903,610 @@
 ion 
-run() %7B%0A
+setupAsync(cfg) %7B%0A    return new Promise((resolve, reject) =%3E %7B%0A        const promisePush = util.promisify(push);%0A%0A        // TODO: use credentials%0A        const dbURL = %60$%7Bcfg.get('couchdbServer')%7D/$%7Bcfg.get('databaseName')%7D%60;%0A        console.debug(dbURL, cfg.defaults.designdoc);%0A%0A        promisePush(dbURL, cfg.defaults.designdoc).then(() =%3E %7B%0A            resolve('!');%0A        %7D).catch((msgData) =%3E %7B%0A            reject(msgData);%0A        %7D);%0A    %7D);%0A%7D%0A%0A/**%0A * Run the setup process.%0A * @return %7BPromise%7D%0A */%0Afunction run(svrConfig) %7B%0A    const promiseSetup = new Promise((resolve, reject) =%3E %7B%0A    
     
@@ -1531,32 +1531,36 @@
 ry()) %7B%0A        
+   
+ 
 console.info('Se
@@ -1576,24 +1576,226 @@
 cessary.');%0A
+            // TODO%0A            // setupAsync(svrConfig).then(() =%3E %7B%0A                resolve('!done');%0A            // %7D).catch((msgData) =%3E %7B%0A            //     reject(msgData);%0A            // %7D);%0A    
     %7D else %7B
@@ -1795,16 +1795,20 @@
  else %7B%0A
+    
         
@@ -1856,28 +1856,140 @@
     
-%7D%0A%7D%0A%0Aexports.run = run
+        resolve('done');%0A        %7D%0A    %7D);%0A%0A    return promiseSetup;%0A%7D%0A%0Aexports.run = run;%0Aexports.isSetupNecessary = isSetupNecessary
 ;%0A

Add setup process, use Promises