All files / skeleton/modules/autoupdate isDesktopInjector.js

64.71% Statements 11/17
75% Branches 6/8
100% Functions 2/2
64.71% Lines 11/17
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68                      64x   64x                             34x 34x 34x     34x 34x     34x                                 34x       34x               1x  
'use strict';
/**
 * Until we would have a `web.desktop` arch in Meteor we need to provide a way to distinguish
 * the desktop specific code. The easiest approach is to have a Meteor.isDesktop. Since we do not
 * want the `Meteor.isCordova` to be true we just replace it with `isDesktop`.
 * Also we need to change the faulty version check procedure to fire on desktop architecture.
 */
 
class IsDesktopInjector {
 
    constructor() {
        this.startupDidCompleteRegEx =
            new RegExp('\\((\\w+.)(isCordova)\\)([\\S\\s]*?)(startupDidComplete)', 'gm');
        this.startupDidCompleteProductionRegEx =
            new RegExp('(\\w+.)(isCordova)(&&\\w*\\.)(startupDidComplete)', 'gm');
    }
    /**
     * Searches for and replaces two places in Meteor app:
     *  - where `isCordova` is set to true
     *  - where `startupDidComplete` is fired
     *
     * @param {string} contents
     * @returns {{fileContents: *, injectedStartupDidComplete: boolean, injected: boolean}}
     */
    processFileContents(contents) {
        // This searches for the place where `startupDidComplete` is fired. We need that now to be
        // fired when `isDesktop` is set.
 
        let injectedStartupDidComplete = false;
        let injected = false;
        let fileContents = contents;
 
        // This changes the place where `isCordova` is set to true.
        fileContents = fileContents.replace('.isCordova=!0', '.isDesktop=!0');
        fileContents = fileContents.replace('.isCordova = true', '.isDesktop = true');
 
 
        Iif (this.startupDidCompleteRegEx.test(fileContents) ||
            this.startupDidCompleteProductionRegEx.test(fileContents)
        ) {
            this.startupDidCompleteProductionRegEx.lastIndex = 0;
            this.startupDidCompleteRegEx.lastIndex = 0;
 
            fileContents = fileContents.replace(
                this.startupDidCompleteRegEx,
                '($1isDesktop)$3$4');
 
            fileContents = fileContents.replace(
                this.startupDidCompleteProductionRegEx,
                '$1isDesktop$3$4');
 
            injectedStartupDidComplete = true;
        }
 
        Iif (~fileContents.indexOf('.isDesktop=!0') ||
            ~fileContents.indexOf('.isDesktop = true')) {
            injected = true;
        }
        return {
            fileContents,
            injectedStartupDidComplete,
            injected
        };
    }
}
 
module.exports = IsDesktopInjector;