AppDelegate.prototype.addInitCode = function()

in appcenter-link-scripts/src/ios/AppDelegate.js [31:55]


AppDelegate.prototype.addInitCode = function (code, oldCodeRegExp) {
    const oldCodeMatches = oldCodeRegExp ? this.appDelegateContents.match(oldCodeRegExp) : null;
    if (oldCodeMatches && oldCodeMatches.length > 1) {
        for (let i = 1; i < oldCodeMatches.length; i++) {
            this.appDelegateContents = this.appDelegateContents.replace(oldCodeMatches[i], '');
            debug(`Removed duplicate ${oldCodeRegExp} lines from AppDelegate.m`);
        }
    }
    if (this.appDelegateContents.indexOf(code) === -1) {
        /* If old code not found, add new content. */
        if (!oldCodeMatches) {
            /* Find the beginning of the didFinishLaunchingWithOptions method. */
            const match = this.appDelegateContents.match(/[^\n]*didFinishLaunchingWithOptions[^{]*{[\s]*/);
            if (match === null) {
                throw Error('Could not find the start of the didFinishLaunchingWithOptions method in the AppDelegate.m file.');
            }

            const existingLine = match[0];
            this.appDelegateContents = this.appDelegateContents.replace(existingLine, `${existingLine}${code}\n  `);
            debug('Added code', code, 'to file', this.appDelegatePath);
        }
    } else {
        debug(`Looks like ${code} is already added to AppDelegate.m`);
    }
};