# All files / src/platformTools/tizen index.js

10.34% Statements 37/358
0% Branches 0/146
0% Functions 0/55
16.93% Lines 32/189

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
    3562x
    2x
    2x
    2x
    2x
    2x
    2x

    2x
    2x
    2x

    2x
    2x
    2x

    2x

    2x

    2x

    2x

    2x

    2x

    2x

    2x

    2x

    2x

    2x

    2x

    2x

    2x

    2x

    2x

    2x

    2x

    2x

     /* eslint-disable import/no-cycle */
    import path from 'path';
    import fs from 'fs';
    import chalk from 'chalk';
    import inquirer from 'inquirer';
    import net from 'net';
    import parser from 'xml2json';

    import { execCLI } from '../../systemTools/exec';
    import { RENATIVE_CONFIG_NAME, CLI_TIZEN_EMULATOR, CLI_TIZEN, CLI_SDB_TIZEN } from '../../constants';
    import {
        logTask,
        logError,
        getAppFolder,
        isPlatformActive,
        logWarning,
        logDebug,
        logSuccess,
        writeCleanFile,
        getAppTemplateFolder,
        getConfigProp,
        waitForEmulator
    } from '../../common';
    import { copyAssetsFolder, copyBuildsFolder } from '../../projectTools/projectParser';
    import { copyFolderContentsRecursiveSync } from '../../systemTools/fileutils';
    import { buildWeb } from '../web';

    const CHECK_INTEVAL = 2000;

    const formatXMLObject = obj => ({
        ...obj['model-config'].platform.key.reduce((acc, cur, i) => {
            acc[cur.name] = cur.$t;
            return acc;
        }, {})
    });

    const configureTizenGlobal = c => new Promise((resolve, reject) => {
        logTask('configureTizenGlobal');
        // Check Tizen Cert
        // if (isPlatformActive(c, TIZEN) || isPlatformActive(c, TIZEN_WATCH)) {
        const tizenAuthorCert = path.join(c.paths.workspace.dir, 'tizen_author.p12');
        if (fs.existsSync(tizenAuthorCert)) {
            console.log('tizen_author.p12 file exists!');
            resolve();
        } else {
            console.log('tizen_author.p12 file missing! Creating one for you...');
            createDevelopTizenCertificate(c)
                .then(() => resolve())
                .catch(e => reject(e));
        }
        // }
    });

    function launchTizenSimulator(c, name) {
        logTask(`launchTizenSimulator:${name}`);
        const { maxErrorLength } = c.program;

        if (name) {
            return execCLI(c, CLI_TIZEN_EMULATOR, `launch --name ${name}`, { detached: true, maxErrorLength });
        }
        return Promise.reject('No simulator -t target name specified!');
    }

    const createDevelopTizenCertificate = c => new Promise((resolve, reject) => {
        logTask('createDevelopTizenCertificate');
        const { maxErrorLength } = c.program;

        execCLI(c, CLI_TIZEN, `certificate -- ${c.paths.workspace.dir} -a rnv -f tizen_author -p 1234`)
            .then(() => addDevelopTizenCertificate(c))
            .then(() => resolve())
            .catch((e) => {
                logError(e);
                resolve();
            });
    });

    const addDevelopTizenCertificate = c => new Promise((resolve) => {
        logTask('addDevelopTizenCertificate');
        const { maxErrorLength } = c.program;

        execCLI(c, CLI_TIZEN, `security-profiles add -n RNVanillaCert -a ${path.join(c.paths.workspace.dir, 'tizen_author.p12')} -p 1234`)
            .then(() => resolve())
            .catch((e) => {
                logError(e);
                resolve();
            });
    });

    const getDeviceID = async (c, target) => {
        const { device } = c.program;

        if (device) {
            const connectResponse = await execCLI(c, CLI_SDB_TIZEN, `connect ${target}`);
            if (connectResponse.includes('failed to connect to remote target')) throw new Error(connectResponse);
        }

        const devicesList = await execCLI(c, CLI_SDB_TIZEN, 'devices');
        if (devicesList.includes(target)) {
            const lines = devicesList.trim().split(/\r?\n/);
            const devices = lines.filter(line => line.includes(target));

            if (devices.length > 1) {
                // @todo handle more than one
            }

            const deviceID = devices[0].split('device')[1].trim();
            return deviceID;
        }
        return Promise.reject(`No device matching ${target} could be found.`);
    };

    const getRunningDevices = async (c) => {
        const { platform } = c.program;
        const devicesList = await execCLI(c, CLI_SDB_TIZEN, 'devices');
        const lines = devicesList.trim().split(/\r?\n/).filter(line => !line.includes('List of devices'));
        const devices = [];

        await Promise.all(lines.map(async (line) => {
            const words = line.replace(/\t/g, '').split('    ');
            if (words.length >= 3) {
                const name = words[0].trim();
                const deviceInfoXML = await execCLI(c, CLI_SDB_TIZEN, `-s ${name} shell cat /etc/config/model-config.xml`, { ignoreErrors: true });

                let deviceInfo;
                let deviceType;

                if (deviceInfoXML !== true) {
                    // for some reason the tv does not connect through sdb
                    deviceInfo = formatXMLObject(parser.toJson(deviceInfoXML, { object: true, reversible: false }));
                    deviceType = deviceInfo['tizen.org/feature/profile'];
                }

                if ((platform === 'tizenmobile' && deviceType === 'mobile') || (platform === 'tizenwatch' && deviceType === 'wearable') || (platform === 'tizen' && !deviceType)) {
                    devices.push({
                        name,
                        type: words[1].trim(),
                        id: words[2].trim(),
                        deviceType,
                    });
                }
            }
        }));

        return devices;
    };

    const waitForEmulatorToBeReady = (c, target) => waitForEmulator(c, CLI_SDB_TIZEN, 'devices', (res) => {
        const lines = res.trim().split(/\r?\n/);
        const devices = lines.filter(line => line.includes(target) && line.includes('device'));
        return devices.length > 0;
    });

    const composeDevicesString = devices => devices.map(device => ({ key: device.id, name: device.name, value: device.id }));

    const runTizen = async (c, platform, target) => {
        logTask(`runTizen:${platform}:${target}`);

        const platformConfig = c.buildConfig.platforms[platform];
        const { hosted, debug } = c.program;

        let isHosted = hosted || !getConfigProp(c, platform, 'bundleAssets');
        if (debug) isHosted = false;

        if (!platformConfig) {
            throw new Error(`runTizen: ${chalk.grey(platform)} not defined in your ${chalk.white(c.paths.appConfig.config)}`);
        }
        if (!platformConfig.appName) {
            throw new Error(`runTizen: ${chalk.grey(platform)}.appName not defined in your ${chalk.white(c.paths.appConfig.config)}`);
        }

        const tDir = getAppFolder(c, platform);
        const tBuild = path.join(tDir, 'build');
        const tOut = path.join(tDir, 'output');
        const tId = platformConfig.id;
        const gwt = `${platformConfig.appName}.wgt`;
        const certProfile = platformConfig.certificateProfile;

        let deviceID;

        const askForEmulator = async () => {
            const { startEmulator } = await inquirer.prompt([{
                name: 'startEmulator',
                type: 'confirm',
                message: `Could not find or connect to the specified target (${target}). Would you like to start an emulator?`,
            }]);

            if (startEmulator) {
                const defaultTarget = c.files.workspace.config.defaultTargets[platform];
                try {
                    await launchTizenSimulator(c, defaultTarget);
                    deviceID = defaultTarget;
                    await waitForEmulatorToBeReady(c, defaultTarget);
                    return continueLaunching();
                } catch (e) {
                    logDebug(`askForEmulator:ERRROR: ${e}`);
                    try {
                        await execCLI(c, CLI_TIZEN_EMULATOR, `create -n ${defaultTarget} -p tv-samsung-5.0-x86`);
                        await launchTizenSimulator(c, defaultTarget);
                        deviceID = defaultTarget;
                        await waitForEmulatorToBeReady(c, defaultTarget);
                        return continueLaunching();
                    } catch (err) {
                        logDebug(err);
                        logError(`Could not find the specified target and could not create the emulator automatically. Please create one and then edit the default target from ${c.paths.workspace.dir}/${RENATIVE_CONFIG_NAME}`);
                    }
                }
            }
        };

        const continueLaunching = async () => {
            let hasDevice = false;

            !isHosted && await buildWeb(c, platform);
            await execCLI(c, CLI_TIZEN, `build-web -- ${tDir} -out ${tBuild}`);
            await execCLI(c, CLI_TIZEN, `package -- ${tBuild} -s ${certProfile} -t wgt -o ${tOut}`);

            try {
                const packageID = platform === 'tizenwatch' || platform === 'tizenmobile' ? tId.split('.')[0] : tId;
                await execCLI(c, CLI_TIZEN, `uninstall -p ${packageID} -t ${deviceID}`, { ignoreErrors: true });
                hasDevice = true;
            } catch (e) {
                if (e && e.includes && e.includes('No device matching')) {
                    await launchTizenSimulator(c, target);
                    hasDevice = await waitForEmulatorToBeReady(c, target);
                }
            }
            try {
                await execCLI(c, CLI_TIZEN, `install -- ${tOut} -n ${gwt} -t ${deviceID}`);
                hasDevice = true;
            } catch (err) {
                logError(err);
                logWarning(
                    `Looks like there is no emulator or device connected! Let's try to launch it. "${chalk.white.bold(
                        `rnv target launch -p ${platform} -t ${target}`
                    )}"`
                );

                await launchTizenSimulator(c, target);
                hasDevice = await waitForEmulatorToBeReady(c, target);
            }

            if (platform !== 'tizenwatch' && platform !== 'tizenmobile' && hasDevice) {
                await execCLI(c, CLI_TIZEN, `run -p ${tId} -t ${deviceID}`);
            } else if ((platform === 'tizenwatch' || platform === 'tizenmobile') && hasDevice) {
                const packageID = tId.split('.');
                await execCLI(c, CLI_TIZEN, `run -p ${packageID[0]} -t ${deviceID}`);
            }
            return true;
        };

        // Check if target is present or it's the default one
        const isTargetSpecified = c.program.target;

        // Check for running devices
        const devices = await getRunningDevices(c);

        if (isTargetSpecified) {
            // The user requested a specific target, searching for it in active ones
            if (net.isIP(target)) {
                deviceID = await getDeviceID(c, target);
                return continueLaunching();
            }

            if (devices.length > 0) {
                const targetDevice = devices.find(device => device.id === target || device.name === target);
                if (targetDevice) {
                    deviceID = targetDevice.id;
                    return continueLaunching();
                }
            }
            try {
                // try to launch it, see if it's a simulator that's not started yet
                await launchTizenSimulator(c, target);
                await waitForEmulatorToBeReady(c, target);
                deviceID = target;
                return continueLaunching();
            } catch (e) {
                return askForEmulator();
            }
        } else {
            if (devices.length === 1) {
                deviceID = devices[0].id;
                return continueLaunching();
            } if (devices.length > 1) {
                const choices = composeDevicesString(devices);
                const { chosenEmulator } = await inquirer.prompt([{
                    name: 'chosenEmulator',
                    type: 'list',
                    message: 'On what emulator would you like to run the app?',
                    choices
                }]);
                deviceID = chosenEmulator;
                return continueLaunching();
            }
            return askForEmulator();
        }
    };

    const buildTizenProject = (c, platform) => new Promise((resolve, reject) => {
        logTask(`buildTizenProject:${platform}`);

        const platformConfig = c.buildConfig.platforms[platform];
        const tDir = getAppFolder(c, platform);
        const tOut = path.join(tDir, 'output');
        const tBuild = path.join(tDir, 'build');
        const certProfile = platformConfig.certificateProfile;

        buildWeb(c, platform)
            .then(() => execCLI(c, CLI_TIZEN, `build-web -- ${tDir} -out ${tBuild}`))
            .then(() => execCLI(c, CLI_TIZEN, `package -- ${tBuild} -s ${certProfile} -t wgt -o ${tOut}`))
            .then(() => {
                logSuccess(`Your GWT package is located in ${chalk.white(tOut)} .`);
                return resolve();
            })
            .catch(e => reject(e));
    });

    const configureTizenProject = (c, platform) => new Promise((resolve, reject) => {
        logTask('configureTizenProject');

        if (!isPlatformActive(c, platform, resolve)) return;

        copyAssetsFolder(c, platform)
            .then(() => copyBuildsFolder(c, platform))
            .then(() => configureProject(c, platform))
            .then(() => resolve())
            .catch(e => reject(e));
    });

    const configureProject = (c, platform) => new Promise((resolve) => {
        logTask(`configureProject:${platform}`);

        const appFolder = getAppFolder(c, platform);

        const configFile = 'config.xml';
        const p = c.buildConfig.platforms[platform];
        writeCleanFile(path.join(getAppTemplateFolder(c, platform), configFile), path.join(appFolder, configFile), [
            { pattern: '{{PACKAGE}}', override: p.package },
            { pattern: '{{ID}}', override: p.id },
            { pattern: '{{APP_NAME}}', override: p.appName },
        ]);

        resolve();
    });

    export {
        launchTizenSimulator,
        configureTizenProject,
        createDevelopTizenCertificate,
        addDevelopTizenCertificate,
        runTizen,
        buildTizenProject,
        configureTizenGlobal,
    };