All files / src/projectTools buildHooks.js

51.82% Statements 57/110
42.31% Branches 11/26
24.24% Functions 8/33
43.21% Lines 35/81

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 1293x 3x 3x 3x 3x       3x 3x 3x 3x 3x 3x 3x 3x 102x   3x   3x           3x                           3x   3x 2x   2x   2x   2x             2x             2x     3x   3x 2x   2x                                       2x   3x   3x                                       3x   3x               3x      
import chalk from 'chalk';
import path from 'path';
import fs from 'fs';
import child_process from 'child_process';
import {
    isPlatformSupportedSync, getConfig, logTask, logComplete, logError,
    getAppFolder, logWarning, resolveNodeModulePath
} from '../common';
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';
import { PIPES as RUNNER_PIPES } from '../cli/runner';
import { PIPES as PLATFORM_PIPES } from '../cli/platform';
import { PIPES as PLUGIN_PIPES } from '../cli/plugin';
import { PIPES as TARGET_PIPES } from '../cli/target';
import { PIPES as APP_PIPES } from '../cli/app';EEE
 
const isRunningOnWindows = process.platform === 'win32';
 
const PIPES = { ...RUNNER_PIPES, ...PLATFORM_PIPES, ...PLUGIN_PIPES, ...TARGET_PIPES, ...APP_PIPES };
 
// ##########################################
// PUBLIC API
// ##########################################
 
const executeHook = c => new Promise((resolve, reject) => {
    logTask('executeHook');
 
    buildHooks(c)
        .then(() => {
            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`)
            .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 listHooks = c => new Promise((resolve, reject) => {
    logTask('listHooks');
 
    buildHooks(c)
        .then(() => {
            if (c.buildHooks) {
                const hookOpts = generateOptions(c.buildHooks);
                let hooksAsString = `\n${chalk.blue('Hooks:')}\n${hookOpts.asString}`;
 
                if (c.buildPipes) {
                    const pipeOpts = generateOptions(c.buildPipes);
                    hooksAsString += `\n${chalk.blue('Pipes:')}\n${pipeOpts.asString}`;
                }
                console.log(hooksAsString);
                resolve();
            } else {
                reject('Your buildHooks object is empty!');
            }
        })
        .catch(e => reject(e));
});
 
const listPipes = c => new Promise((resolve, reject) => {
    logTask('listPipes');
 
    buildHooks(c)
        .then(() => {
            const pipeOpts = generateOptions(c.buildPipes);
            console.log(`Pipes:\n${pipeOpts.asString}`);
        }).catch(e => reject(e));
});
 
export { buildHooks, listHooks, executeHook, executePipe, listPipes };