// Log.js - in api/services

"use strict";

const _ = require('lodash');
const fs = require('fs');
const path = require('path');
const {promisify} = require('util');
const readdirPromise = promisify(fs.readdir);
const moment = require('moment');
const readline = require('readline');
const stream = require('stream');
const Convert = require('ansi-to-html');
const baseFolder = path.join('logs');

moment.locale('en');

module.exports = {
    getTasks,
    read
};

function getTasks() {
    return new Promise(async (resolve, reject) => {
        await readdirPromise(baseFolder).then(fileNames => {
            let files = fileNames.filter(name => {
                return name !== '.gitkeep';
            });

            let tasks = [];

            files.map(name => {
                const fileInfo = name.split('_');
                const taskName = fileInfo.shift();
                const date = fileInfo.pop().replace('.log', '');

                const index = tasks.findIndex((task => task.taskName === taskName));
                if (index === -1) {
                    tasks.push({
                        taskName: taskName,
                        dates: [date]
                    });
                } else {
                    tasks[index].dates.push(date);

                    tasks[index].dates = _.orderBy(tasks[index].dates, date => {
                        return new moment(date).format('YYYMMDD');
                    }, ['desc']);
                }
            });

            resolve(tasks);
        }).catch(err => {
            reject(err);
        });
    }).then(tasks => {
        return {
            type: 'success',
            tasks: tasks
        };
    }).catch(err => {
        return {
            type: 'failed',
            message: err
        };
    });
}

function read(req) {
    return new Promise(async (resolve, reject) => {
        const taskName = req.params.taskName;
        const date = req.params.date;

        await readdirPromise(baseFolder).then(async fileNames => {
            let files = fileNames.filter(name => {
                return name !== '.gitkeep';
            });

            files = files.map(name => {
                const fileInfo = name.split('_');
                return {
                    fileName: name,
                    taskName: fileInfo.shift(),
                    date: fileInfo.pop().replace('.log', '')
                };
            });

            files = files.filter(file => {
                return file.taskName === taskName;
            });

            files = _.orderBy(files, file => {
                return new moment(file.date).format('YYYMMDD');
            }, ['desc']);

            if (date) {
                files = files.filter(file => {
                    return file.date === date;
                });
            }
            const file = _.head(files);
            const filePath = path.join(baseFolder, file.fileName);
            const instream = fs.createReadStream(filePath);
            const outstream = new (stream)();
            const rl = readline.createInterface(instream, outstream);
            const output = [];

            rl.on('line', line => {
                const convert = new Convert();
                const lineHtml = convert.toHtml(line)
                    .replace(/style="color:#A00"/g, 'class="red"')
                    .replace(/style="color:#FFF"/g, 'class="white"')
                    .replace(/style="color:#0A0"/g, 'class="green"');

                output.push(lineHtml);
            });

            rl.on('close', () => {
                resolve(output.join('<br>'));
            });
        }).catch(err => {
            reject(err);
        });
    }).then(logs => {
        return {
            type: 'success',
            logs: logs
        };
    }).catch(err => {
        return {
            type: 'failed',
            message: err
        };
    });
}
@@ -759,32 +759,88 @@
 s.map(name =%3E %7B%0A
+                if (name.match(/_%5B0-9%5D%7B8%7D.log/g)) %7B%0A    
                 
@@ -881,32 +881,36 @@
                 
+ 
+   
 const taskName =
@@ -924,24 +924,28 @@
 fo.shift();%0A
+    
             
@@ -1006,32 +1006,36 @@
                 
+ 
+   
 const index = ta
@@ -1099,24 +1099,28 @@
             
+    
 if (index ==
@@ -1143,24 +1143,28 @@
             
+  
+  
 tasks.push(%7B
@@ -1164,16 +1164,20 @@
 .push(%7B%0A
+    
         
@@ -1232,24 +1232,28 @@
             
+  
+  
 dates: %5Bdate
@@ -1266,32 +1266,36 @@
                 
+    
 %7D);%0A            
@@ -1298,16 +1298,20 @@
         
+  
+  
 %7D else %7B
@@ -1311,16 +1311,20 @@
  else %7B%0A
+    
         
@@ -1379,32 +1379,36 @@
                 
+  
+  
 tasks%5Bindex%5D.dat
@@ -1468,32 +1468,36 @@
                 
+    
 return new momen
@@ -1535,32 +1535,36 @@
                 
+  
+  
 %7D, %5B'desc'%5D);%0A  
@@ -1561,16 +1561,38 @@
 esc'%5D);%0A
+                    %7D%0A
         

Add regular expression to test log files