All files / src sticky.js

83.87% Statements 26/31
50% Branches 9/18
100% Functions 6/6
92.3% Lines 24/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 5333x 33x 33x   33x 33x 33x                   33x 33x 33x 33x       33x   7x 7x   7x 7x 7x 7x 7x         7x 7x 7x 7x 7x       7x              
const os = require('os'),
    childProcess = require('child_process'),
    platform = os.platform(),
    // Basso,Blow,Bottle,Frog,Funk,Glass,Hero,Morse,Ping,Pop,Purr,Sosumi,Submarine,Tink
    sounds = { success: 'Tink', failure: 'Blow' },
    icons = { success: '✅', failure: '❌' },
    tools = {
        mac: [
            'osascript',
            '-e \'display notification "{icon} {message}" with title "{title}"{sound}\''
        ],
        linux: [
            'notify-send',
            '-t 1000 "{title}" "{message}"'
        ]
    },
    currentOs = (function () {
        Iif (/^win32/.test(platform)) return 'win';
        Iif (/^linux/.test(platform)) return 'linux';
        Eif (/^darwin/.test(platform)) return 'mac';
        return false;
    })();
 
module.exports = function (title, message, errs, testcb) {
    'use strict';
    Eif (typeof testcb === 'undefined') {
        testcb = function () { return null; };
    }
    Iif (!(currentOs in tools)) return;
    const exeData = tools[currentOs],
        hasErrors = typeof errs !== 'undefined',
        exec = exeData[0],
        params = exeData[1]
            .replace(/\{title\}/, title)
            .replace(/\{message\}/, message)
            .replace(/\{icon\}/, icons[hasErrors ? 'failure' : 'success'])
            .replace(/\{sound\}/, hasErrors ? `sound name "${sounds.failure}"` : '');
    setImmediate(() => {
        childProcess.exec(`which ${exec}`, error => {
            Eif (error === null) {
                childProcess.exec(`${exec} ${params}`, function (error) {
                    Iif (error) {
                        // eslint-disable-next-line no-console
                        console.log(error);
                    } else {
                        testcb(`${title}___${message}`);
                    }
                });
            }
        });
    });
};