function controller() {

    var arrShirts = [];
    var dir = 'data';
    var curDate = moment().format('YYYY-MM-DD');

    var configMain = {
        repeatItemGroup: '.nav > li',
        dataFormat: {
            URL: {
                selector: 'li > a',
                type: 'attr:href'
            }
        }
    };

    getPage(baseURL, configMain).then( function(data) {
        for (var i = 0; i < data.length; i++) {
            if (data[i].URL.search('shirt') !== -1) {
                var url = baseURL + '/' + data[i].URL;
                return url;
            }
        }
    }).then( function(url) {
        var configShirts = {
            repeatItemGroup: '.products > li',
            dataFormat: {
                ImageURL: {
                    selector: 'li > a > img',
                    type: 'attr:src'
                },
                URL: {
                    selector: 'li > a',
                    type: 'attr:href'
                },
                Time: moment().format('LTS')
            }
        };
        return getPage(url, configShirts);
    }).then( function(data) {
        arrShirts = data;
        var configDetails = {
            repeatItemGroup: '.shirt-details',
            dataFormat: {
                Title: {
                    selector: '.shirt-details > h1',
                    type: 'text'
                },
                Price: {
                    selector: '.price',
                    type: 'text'
                }
            }
        };
        return (getDetailPage(data, configDetails));
    }).then( function(data) {

        /** Combine two objects into one and remove price from title string and  */
        for( var i = 0; i < arrShirts.length; i++) {
            /** data is an array of objects, where each object is stored as an array. Odd, I know. */
            arrShirts[i] = (merge(data[i][0], arrShirts[i]));
            arrShirts[i].Title = arrShirts[i].Title.replace(/\$\d+/g, '').trim();
        }

        /** convert JSON to csv to prepare for writing to file */
        var csv = baby.unparse(arrShirts);

        /** Create data directory if one does not exist */
        if (!fs.existsSync(dir)) {
            fs.mkdirSync(dir);
        }

        /** Write to file */
        var file = dir + '/' + curDate + '.csv';
        fs.writeFile(file, csv, (err) => {
            if (err) throw err;
            var msg = 'Data saved to file: ' + file + '.';
            logger.log(msg);
        });

    }).catch(function(e) {
        var msg = '';
        /** Create message to append to error log */ 
        if (e.message.startsWith('getaddrinfo ENOTFOUND')) {
            msg = '--  Page couldn\'t be accessed: ';
        } else {
            msg = '--  Something went wrong: ';
        }
        msg = moment().format('dddd, MMM Do, YYYY, h:mm:ss a') + msg + e.message + '\n';
        fs.appendFile('scraper.error.log', msg, (err) => {
            if (err) throw err;
            logger.log(msg);
        });
    });
}
/**------------------------------------------------------------------------------------
 * FUNCTION controller() 
 *
 * Main function that drives the content scrapping and data manipulation,
 * file and error handling.
 */
FUNCTION controller()
Main function that drives the content scrapping and data manipulation,
file and error handling.
FUNCTION controller () Main function that drives the content scrapping and data manipulation file and error handling .
function controller() {
    var arrShirts = [];
    var dir = 'data';
    var curDate = moment().format('YYYY-MM-DD');
    var configMain = {
        repeatItemGroup: '.nav > li',
        dataFormat: {
            URL: {
                selector: 'li > a',
                type: 'attr:href'
            }
        }
    };
    getPage(baseURL, configMain).then( function(data) {
        for (var i = 0; i < data.length; i++) {
            if (data[i].URL.search('shirt') !== -1) {
                var url = baseURL + '/' + data[i].URL;
                return url;
            }
        }
    }).then( function(url) {
        var configShirts = {
            repeatItemGroup: '.products > li',
            dataFormat: {
                ImageURL: {
                    selector: 'li > a > img',
                    type: 'attr:src'
                },
                URL: {
                    selector: 'li > a',
                    type: 'attr:href'
                },
                Time: moment().format('LTS')
            }
        };
        return getPage(url, configShirts);
    }).then( function(data) {
        arrShirts = data;
        var configDetails = {
            repeatItemGroup: '.shirt-details',
            dataFormat: {
                Title: {
                    selector: '.shirt-details > h1',
                    type: 'text'
                },
                Price: {
                    selector: '.price',
                    type: 'text'
                }
            }
        };
        return (getDetailPage(data, configDetails));
    }).then( function(data) {
        for( var i = 0; i < arrShirts.length; i++) {
            arrShirts[i] = (merge(data[i][0], arrShirts[i]));
            arrShirts[i].Title = arrShirts[i].Title.replace(/\$\d+/g, '').trim();
        }
        var csv = baby.unparse(arrShirts);
        if (!fs.existsSync(dir)) {
            fs.mkdirSync(dir);
        }
        var file = dir + '/' + curDate + '.csv';
        fs.writeFile(file, csv, (err) => {
            if (err) throw err;
            var msg = 'Data saved to file: ' + file + '.';
            logger.log(msg);
        });
    }).catch(function(e) {
        var msg = '';
        if (e.message.startsWith('getaddrinfo ENOTFOUND')) {
            msg = '--  Page couldn\'t be accessed: ';
        } else {
            msg = '--  Something went wrong: ';
        }
        msg = moment().format('dddd, MMM Do, YYYY, h:mm:ss a') + msg + e.message + '\n';
        fs.appendFile('scraper.error.log', msg, (err) => {
            if (err) throw err;
            logger.log(msg);
        });
    });
}
function controller ( ) { var arrShirts = [ ] ; var dir = 'data' ; var curDate = moment ( ) . format ( 'YYYY-MM-DD' ) ; var configMain = { repeatItemGroup : '.nav > li' , dataFormat : { URL : { selector : 'li > a' , type : 'attr:href' } } } ; getPage ( baseURL , configMain ) . then ( function ( data ) { for ( var i = 0 ; i < data . length ; i ++ ) { if ( data [ i ] . URL . search ( 'shirt' ) !== - 1 ) { var url = baseURL + '/' + data [ i ] . URL ; return url ; } } } ) . then ( function ( url ) { var configShirts = { repeatItemGroup : '.products > li' , dataFormat : { ImageURL : { selector : 'li > a > img' , type : 'attr:src' } , URL : { selector : 'li > a' , type : 'attr:href' } , Time : moment ( ) . format ( 'LTS' ) } } ; return getPage ( url , configShirts ) ; } ) . then ( function ( data ) { arrShirts = data ; var configDetails = { repeatItemGroup : '.shirt-details' , dataFormat : { Title : { selector : '.shirt-details > h1' , type : 'text' } , Price : { selector : '.price' , type : 'text' } } } ; return ( getDetailPage ( data , configDetails ) ) ; } ) . then ( function ( data ) { for ( var i = 0 ; i < arrShirts . length ; i ++ ) { arrShirts [ i ] = ( merge ( data [ i ] [ 0 ] , arrShirts [ i ] ) ) ; arrShirts [ i ] . Title = arrShirts [ i ] . Title . replace ( / \$\d+ / g , '' ) . trim ( ) ; } var csv = baby . unparse ( arrShirts ) ; if ( ! fs . existsSync ( dir ) ) { fs . mkdirSync ( dir ) ; } var file = dir + '/' + curDate + '.csv' ; fs . writeFile ( file , csv , ( err ) => { if ( err ) throw err ; var msg = 'Data saved to file: ' + file + '.' ; logger . log ( msg ) ; } ) ; } ) . catch ( function ( e ) { var msg = '' ; if ( e . message . startsWith ( 'getaddrinfo ENOTFOUND' ) ) { msg = '--  Page couldn\'t be accessed: ' ; } else { msg = '--  Something went wrong: ' ; } msg = moment ( ) . format ( 'dddd, MMM Do, YYYY, h:mm:ss a' ) + msg + e . message + '\n' ; fs . appendFile ( 'scraper.error.log' , msg , ( err ) => { if ( err ) throw err ; logger . log ( msg ) ; } ) ; } ) ; }
FUNCTION controller()
 Main function that drives the content scrapping and data manipulation,
 file and error handling.
FUNCTION controller () Main function that drives the content scrapping and data manipulation file and error handling .
/** Combine two objects into one and remove price from title string and  */ /** data is an array of objects, where each object is stored as an array. Odd, I know. */ /** convert JSON to csv to prepare for writing to file */ /** Create data directory if one does not exist */ /** Write to file */ /** Create message to append to error log */