All files / src/projectTools buildHooks.js

57.95% Statements 51/88
27.27% Branches 6/22
50% Functions 14/28
54.43% Lines 43/79

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 1292x 2x 2x 2x 2x       2x 2x 2x 2x 2x   2x           2x 1x   1x   1x 1x 1x                     2x   53x 53x   53x   53x   53x             53x             53x     2x   56x 56x   56x                                           56x   2x   2x 1x   1x   1x                     1x       2x   2x 1x   1x   1x 1x   2x      
import chalk from 'chalk';
import path from 'path';
import fs from 'fs';
import child_process from 'child_process';
import {
    isPlatformSupportedSync, getConfig,
    getAppFolder, resolveNodeModulePath
} from '../common';
import { logToSummary, logTask, logComplete, logError, logWarning } from '../systemTools/logger';
import { generateOptions } from '../systemTools/prompt';
import { IOS, ANDROID, TVOS, TIZEN, WEBOS, ANDROID_TV, ANDROID_WEAR, KAIOS } from '../constants';
import { executeAsync, execCLI } from '../systemTools/exec';
import { cleanFolder, copyFolderContentsRecursiveSync, copyFolderRecursiveSync, copyFileSync } from '../systemTools/fileutils';
 
const isRunningOnWindows = process.platform === 'win32';
 
// ##########################################
// PUBLIC API
// ##########################################
 
const rnvHooksRun = c => new Promise((resolve, reject) => {
    logTask('rnvHooksRun');
 
    buildHooks(c)
        .then(() => {
      E      if (!c.buildHooks) {
                reject('Build hooks have not been compiled properly!');
                return;
            }
            if (c.buildHooks[c.program?.exeMethod]) {
                c.buildHooks[c.program?.exeMethod](c)
                    .then(() => resolve())
                    .catch(e => reject(e));
            } else {
                reject(`Method name ${chalk.white(c.program.exeMethod)} does not exists in your buildHooks!`);
            }
        })
        .catch(e => reject(e));
});
 
const executePipe = (c, key) => new Promise((resolve, reject) => {
    logTask(`executePipe:${key}`);
 
    buildHooks(c)
        .then(() => {
            const pipe = c.buildPipes ? c.buildPipes[key] : null;
 
      I      if (Array.isArray(pipe)) {
                const chain = pipe
                    .reduce((accumulatorPromise, next) => accumulatorPromise.then(() => next(c)), Promise.resolve())
                    .then(() => resolve())
                    .catch(e => reject(e));
                return;
            }
      I      if (pipe) {
                c.buildPipes[key](c)
                    .then(() => resolve())
                    .catch(e => reject(e));
                return;
            }
 
            resolve();
        })
        .catch(e => reject(e));
});
 
const buildHooks = c => new Promise((resolve, reject) => {
    logTask('buildHooks');
 
    Iif (fs.existsSync(c.paths.buildHooks.index)) {
        if (c.isBuildHooksReady) {
            resolve();
            return;
        }
 
        executeAsync(c, `babel --no-babelrc ${c.paths.buildHooks.dir} -d ${c.paths.buildHooks.dist.dir} --presets=@babel/env`, {
            cwd: c.paths.buildHooks.dir
        })
            .then(() => {
                const h = require(c.paths.buildHooks.dist.index);
                c.buildHooks = h.hooks;
                c.buildPipes = h.pipes;
                c.isBuildHooksReady = true;
                resolve();
            })
            .catch((e) => {
                logWarning(`BUILD_HOOK Failed with error: ${e}`);
                resolve();
            });
    } else {
        // logWarning(`Your buildHook ${chalk.white(c.paths.buildHooks.index)} is missing!. Skipping operation`);
        resolve();
    }
});
 
const rnvHooksList = c => new Promise((resolve, reject) => {
    logTask('rnvHooksList');
 
    buildHooks(c)
        .then(() => {
      I      if (c.buildHooks) {
                const hookOpts = generateOptions(c.buildHooks);
                let hooksAsString = `\n${'Hooks:'}\n${hookOpts.asString}`;
 
                if (c.buildPipes) {
                    const pipeOpts = generateOptions(c.buildPipes);
                    hooksAsString += `\n${'Pipes:'}\n${pipeOpts.asString}`;
                }
                logToSummary(hooksAsString);
                resolve();
            } else {
                reject('Your buildHooks object is empty!');
            }
        })
        .catch(e => reject(e));
});
 
const rnvHooksPipes = c => new Promise((resolve, reject) => {
    logTask('rnvHooksPipes');
 
    buildHooks(c)
        .then(() => {
            const pipeOpts = generateOptions(c.buildPipes);
            console.log(`Pipes:\n${pipeOpts.asString}`);
        }).catch(e => reject(e));
});
 
export { buildHooks, rnvHooksList, rnvHooksRun, executePipe, rnvHooksPipes };