All files / src/platformTools runner.js

21.3% Statements 92/432
4.39% Branches 15/342
28.57% Functions 10/35
35.06% Lines 54/154

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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 4472x   2x 2x 2x 2x   2x                           2x                                     2x             2x 2x 2x 2x 2x       2x 2x             2x 2x   2x             2x                                                                             2x     2x   2x         2x       35x 12x   11x   12x     3x 1x   1x   2x     35x 12x   11x   12x     3x 1x   1x   2x     3x 1x   1x   2x     2x                         2x             2x               2x                     2x           2x                                                                                                                                                             2x                                                 2x                                                       2x                                                                 2x                                                                                                                       2x                    
/* eslint-disable import/no-cycle */
// @todo fix circular
import chalk from 'chalk';
import open from 'react-dev-utils/openBrowser';
import ip from 'ip';
import path from 'path';
 
import {
    isPlatformSupported,
    isBuildSchemeSupported,
    logTask,
    logError,
    checkSdk,
    logErrorPlatform,
    configureIfRequired,
    cleanPlatformIfRequired,
    logDebug,
    writeCleanFile,
    getConfigProp,
    waitForWebpack
} from '../common';
import {
    IOS,
    TVOS,
    ANDROID,
    WEB,
    TIZEN,
    WEBOS,
    ANDROID_TV,
    ANDROID_WEAR,
    MACOS,
    WINDOWS,
    TIZEN_MOBILE,
    TIZEN_WATCH,
    KAIOS,
    FIREFOX_OS,
    FIREFOX_TV,
    WEB_HOSTED_PLATFORMS,
    PLATFORMS
} from '../constants';
import {
    runXcodeProject,
    exportXcodeProject,
    archiveXcodeProject,
    packageBundleForXcode,
    runAppleLog
} from './apple';
import { buildWeb, runWeb, runWebDevServer, deployWeb, exportWeb } from './web';
import { runTizen, buildTizenProject } from './tizen';
import { runWebOS, buildWebOSProject } from './webos';
import { runFirefoxProject, buildFirefoxProject } from './firefox';
import {
    runElectron,
    buildElectron, runElectronDevServer, configureElectronProject, exportElectron
} from './electron';
import PlatformSetup from '../setupTools';
import {
    packageAndroid,
    runAndroid,
    configureGradleProject,
    buildAndroid,
    runAndroidLog
} from './android';
import { copyFolderContentsRecursiveSync } from '../systemTools/fileutils';
import { executeAsync } from '../systemTools/exec';
 
const isRunningOnWindows = process.platform === 'win32';
 
// ##########################################
// PUBLIC API
// ##########################################
 
 
export const rnvStart = async (c) => {
    const { platform } = c;
    const port = c.program.port || c.platformDefaults[platform] ? c.platformDefaults[platform].defaultPort : null;
    const { hosted } = c.program;
 
    logTask(`rnvStart:${platform}:${port}`);
 
    if (_isWebHostEnabled(c, platform) && hosted) {
        const hostIp = isRunningOnWindows ? '127.0.0.1' : '0.0.0.0';
        waitForWebpack(c, port)
            .then(() => open(`http://${hostIp}:${port}/`))
            .catch(logError);
    }
 
    switch (platform) {
    case MACOS:
    case WINDOWS:
        return runElectronDevServer(c, platform, port);
 
    case WEB:
    case TIZEN:
    case WEBOS:
    case TIZEN_MOBILE:
    case TIZEN_WATCH:
        await configureIfRequired(c, platform);
        return runWebDevServer(c, platform, port);
    default:
        if (hosted) {
            return logError('This platform does not support hosted mode', true);
        }
    }
 
    const sourceExts = PLATFORMS[platform] ? PLATFORMS[platform].sourceExts.join(',') : 'mobile.js';
    const defaultPort = PLATFORMS[platform]?.defaultPort || 8081;
    let startCmd = `node ./node_modules/react-native/local-cli/cli.js start --watchFolders src --sourceExts ${sourceExts},js,js,json,ts,tsx --port ${defaultPort} --config=metro.config.js`;
    if (c.program.reset) {
        startCmd += ' --reset-cache';
    }
 
    await executeAsync(c, startCmd, { stdio: 'inherit', silent: true });
};
 
const runWeinre = c => executeAsync(c, 'npx weinre --boundHost -all-');
 
export const rnvDebug = async (c) => {
    logTask(`rnvDebug:${c.platform}`);
 
    await isPlatformSupported(c);
    await isBuildSchemeSupported(c);
    await runWeinre(c);
};
 
 
export const rnvRun = async (c) => {
    logTask(`rnvRun:${c.platform}`);
 
    await isPlatformSupported(c);
    await isBuildSchemeSupported(c);
    await _rnvRunWithPlatform(c);
};
 
export const rnvPackage = async (c) => {
    logTask(`rnvPackage:${c.platform}`);
 
    await isPlatformSupported(c);
    await isBuildSchemeSupported(c);
    await _rnvPackageWithPlatform(c);
};
 
export const rnvBuild = async (c) => {
    logTask(`rnvBuild:${c.platform}`);
 
    await isPlatformSupported(c);
    await isBuildSchemeSupported(c);
    await _rnvBuildWithPlatform(c);
};
 
export const rnvExport = async (c) => {
    logTask(`rnvExport:${c.platform}`);
 
    await isPlatformSupported(c);
    await isBuildSchemeSupported(c);
    await _rnvExportWithPlatform(c);
};
 
export const rnvDeploy = async (c) => {
    logTask(`rnvDeploy:${c.platform}`);
 
    await isPlatformSupported(c);
    await isBuildSchemeSupported(c);
    await _rnvDeployWithPlatform(c);
};
 
export const rnvLog = async (c) => {
    switch (c.platform) {
    case ANDROID:
    case ANDROID_TV:
    case ANDROID_WEAR:
        await runAndroidLog(c);
        return;
    case IOS:
    case TVOS:
        await runAppleLog(c);
        return;
    }
 
    logErrorPlatform(c, c.platform);
};
 
// ##########################################
// PRIVATE
// ##########################################
 
const _isWebHostEnabled = (c, platform) => {
    const { hosted, debug } = c.program;
    if (debug) return false;
    const bundleAssets = getConfigProp(c, platform, 'bundleAssets');
    return (hosted || !bundleAssets) && WEB_HOSTED_PLATFORMS.includes(platform);
};
 
 
const _configureHostedIfRequired = async (c, platform) => {
    if (_isWebHostEnabled(c, platform)) {
        logDebug('Running hosted build');
        const { project, rnv } = c.paths;
        copyFolderContentsRecursiveSync(path.join(rnv.dir, 'supportFiles', 'appShell'), path.join(project.dir, 'platformBuilds', `${c.runtime.appId}_${platform}`, 'public'));
        writeCleanFile(path.join(rnv.dir, 'supportFiles', 'appShell', 'index.html'), path.join(project.dir, 'platformBuilds', `${c.runtime.appId}_${platform}`, 'public', 'index.html'), [
            { pattern: '{{DEV_SERVER}}', override: `http://${ip.address()}:${c.platformDefaults[platform].defaultPort}` },
        ]);
    }
};
 
const _startHostedServerIfRequired = (c, platform) => {
    if (_isWebHostEnabled(c, platform)) {
        return rnvStart(c);
    }
};
 
const _rnvRunWithPlatform = async (c) => {
    logTask(`_rnvRunWithPlatform:${c.platform}`);
    const { platform } = c;
    const port = c.program.port || c.platformDefaults[platform].defaultPort;
    const target = c.program.target || c.files.workspace.config.defaultTargets[platform];
    const { hosted } = c.program;
 
    logTask(`_rnvRunWithPlatform:${platform}:${port}:${target}`, chalk.grey);
 
    const throwErr = (err) => {
        throw err;
    };
 
    if (_isWebHostEnabled(c, platform) && hosted) {
        return rnvStart(c);
        // logWarning(`Platform ${platform} does not support --hosted mode. Ignoring`);
    }
 
    switch (platform) {
    case IOS:
    case TVOS:
        await cleanPlatformIfRequired(c, platform);
        await configureIfRequired(c, platform);
        return runXcodeProject(c, platform, target);
    case ANDROID:
    case ANDROID_TV:
    case ANDROID_WEAR:
        if (!checkSdk(c, platform, logError)) {
            const setupInstance = PlatformSetup(c);
            await setupInstance.askToInstallSDK('android');
        }
 
        await cleanPlatformIfRequired(c, platform);
        await configureIfRequired(c, platform);
        return _runAndroid(c, platform, target, platform === ANDROID_WEAR);
    case MACOS:
    case WINDOWS:
        await cleanPlatformIfRequired(c, platform);
        await configureIfRequired(c, platform);
        return runElectron(c, platform, port);
    case WEB:
        await cleanPlatformIfRequired(c, platform);
        await configureIfRequired(c, platform);
        return runWeb(c, platform, port);
    case TIZEN:
    case TIZEN_MOBILE:
    case TIZEN_WATCH:
        if (!checkSdk(c, platform, logError)) {
            const setupInstance = PlatformSetup(c);
            await setupInstance.askToInstallSDK('tizen');
        }
        await cleanPlatformIfRequired(c, platform);
        await configureIfRequired(c, platform);
        await _configureHostedIfRequired(c, platform);
        await runTizen(c, platform, target);
        return _startHostedServerIfRequired(c, platform);
    case WEBOS:
        if (!checkSdk(c, platform, logError)) {
            const setupInstance = PlatformSetup(c);
            await setupInstance.askToInstallSDK('webos');
        }
        await cleanPlatformIfRequired(c, platform);
        await configureIfRequired(c, platform);
        await _configureHostedIfRequired(c, platform);
        await runWebOS(c, platform, target);
        return _startHostedServerIfRequired(c, platform);
    case KAIOS:
    case FIREFOX_OS:
    case FIREFOX_TV:
        if (platform === KAIOS && !checkSdk(c, platform, throwErr)) return;
 
        await cleanPlatformIfRequired(c, platform);
        await configureIfRequired(c, platform);
        return runFirefoxProject(c, platform);
    }
 
    return logErrorPlatform(c, platform);
};
 
const _rnvPackageWithPlatform = async (c) => {
    logTask(`_rnvPackageWithPlatform:${c.platform}`);
    const { platform } = c;
 
    const target = c.program.target || c.files.workspace.config.defaultTargets[platform];
 
    switch (platform) {
    case IOS:
    case TVOS:
        await cleanPlatformIfRequired(c, platform);
        await configureIfRequired(c, platform);
        return packageBundleForXcode(c, platform);
    case ANDROID:
    case ANDROID_TV:
    case ANDROID_WEAR:
        checkSdk(c, platform);
        await cleanPlatformIfRequired(c, platform);
        await configureIfRequired(c, platform);
        await configureGradleProject(c, platform);
        return packageAndroid(c, platform, target, platform === ANDROID_WEAR);
    }
 
    logErrorPlatform(c, platform);
};
 
const _rnvExportWithPlatform = async (c) => {
    logTask(`_rnvExportWithPlatform:${c.platform}`);
    const { platform } = c;
    switch (platform) {
    case WEB:
        if (!c.program.only) {
            await rnvBuild(c);
        }
        return exportWeb(c, platform);
    case IOS:
    case TVOS:
        if (!c.program.only) {
            await _rnvBuildWithPlatform(c, platform);
        }
        return exportXcodeProject(c, platform);
    case MACOS:
    case WINDOWS:
 
        await cleanPlatformIfRequired(c, platform);
        await configureIfRequired(c, platform);
        await configureElectronProject(c, platform);
        await buildElectron(c, platform);
        return exportElectron(c, platform);
    }
 
    logErrorPlatform(c, platform);
};
 
const _rnvDeployWithPlatform = async (c) => {
    logTask(`_rnvDeployWithPlatform:${c.platform}`);
    const { platform } = c;
 
    switch (platform) {
    case WEB:
        if (!c.program.only) {
            await rnvBuild(c);
        }
        return deployWeb(c, platform);
    case TVOS:
    case IOS:
        if (!c.program.only) {
            return _rnvExportWithPlatform(c);
        }
        return;
    // case WEBOS:
    case TIZEN:
        if (!c.program.only) {
            await rnvBuild(c);
        }
        return;
    case ANDROID_TV:
    case ANDROID:
        if (!c.program.only) {
            return _rnvBuildWithPlatform(c);
        }
        return;
    default:
        logErrorPlatform(c, platform);
    }
};
 
const _rnvBuildWithPlatform = async (c) => {
    logTask(`_rnvBuildWithPlatform:${c.platform}`);
    const { platform } = c;
 
    switch (platform) {
    case ANDROID:
    case ANDROID_TV:
    case ANDROID_WEAR:
        await cleanPlatformIfRequired(c, platform);
        await configureIfRequired(c, platform);
        await configureGradleProject(c, platform);
        await packageAndroid(c, platform);
        await buildAndroid(c, platform);
        return;
    case MACOS:
    case WINDOWS:
        await cleanPlatformIfRequired(c, platform);
        await configureIfRequired(c, platform);
        await configureElectronProject(c, platform);
        await buildElectron(c, platform);
        return;
    case IOS:
    case TVOS:
        if (!c.program.only) {
            await _rnvPackageWithPlatform(c, platform);
        }
        await archiveXcodeProject(c, platform);
        return;
    case WEB:
        await cleanPlatformIfRequired(c, platform);
        await configureIfRequired(c, platform);
        await buildWeb(c, platform);
        return;
    case KAIOS:
    case FIREFOX_OS:
    case FIREFOX_TV:
        await cleanPlatformIfRequired(c, platform);
        await configureIfRequired(c, platform);
        await buildFirefoxProject(c, platform);
        return;
    case TIZEN:
    case TIZEN_MOBILE:
    case TIZEN_WATCH:
        checkSdk(c, platform);
        await cleanPlatformIfRequired(c, platform);
        await configureIfRequired(c, platform);
        await buildTizenProject(c, platform);
        return;
    case WEBOS:
        checkSdk(c, platform);
        await cleanPlatformIfRequired(c, platform);
        await configureIfRequired(c, platform);
        await buildWebOSProject(c, platform);
        return;
    }
 
    logErrorPlatform(c, platform);
};
 
 
const _runAndroid = async (c, platform, target, forcePackage) => {
    logTask(`_runAndroid:${platform}`);
 
    if (c.buildConfig.platforms[platform].runScheme === 'Release' || forcePackage) {
        await packageAndroid(c, platform);
        await runAndroid(c, platform, target);
    } else {
        await runAndroid(c, platform, target);
    }
};