{"size": 2399, "ext": "js", "max_stars_repo_path": "packages/roc-package-module-dev/src/actions/build.js", "max_stars_repo_name": "schibsted-martin/extensions", "max_stars_repo_stars_event_min_datetime": "2017-10-24T15:42:13.000Z", "max_stars_repo_stars_event_max_datetime": "2018-01-16T18:03:09.000Z", "max_issues_repo_path": "packages/roc-package-module-dev/src/actions/build.js", "max_issues_repo_name": "rocjs/roc-extensions", "max_issues_repo_issues_event_min_datetime": "2017-10-25T10:50:31.000Z", "max_issues_repo_issues_event_max_datetime": "2019-02-26T09:17:19.000Z", "max_forks_repo_path": "packages/roc-package-module-dev/src/actions/build.js", "max_forks_repo_name": "rocjs/roc-extensions", "max_forks_repo_forks_event_min_datetime": "2017-11-28T17:35:22.000Z", "max_forks_repo_forks_event_max_datetime": "2020-01-09T16:24:59.000Z"}
{"max_stars_count": 3, "max_issues_count": 17, "max_forks_count": 11, "avg_line_length": 34.768115942, "max_line_length": 115, "alphanum_fraction": 0.5985827428}
import prettyMs from 'pretty-ms';
import { getAbsolutePath, initLog } from 'roc';
import { getValueFromPotentialObject } from 'roc-abstract-package-base-dev';

import meta from '../config/roc.config.meta';
import { invokeHook } from '../roc/util';

import isSomeTargetValid from './helpers/isSomeTargetValid';
import babel from './helpers/babel';

const log = initLog();

const buildWithBabel = (target, settings) => {
    if (meta.settings.build.targets.validator([target]) === true) {
        const babelConfig = invokeHook('babel-config', target);

        const src = getAbsolutePath(getValueFromPotentialObject(settings.build.input, target));
        const out = getAbsolutePath(getValueFromPotentialObject(settings.build.output, target));

        /* eslint-disable no-console */
        log.small.info(`Building for ${target.toUpperCase()}`);
        /* eslint-enable */

        try {
            babel({
                src,
                out,
                sourceMaps: true,
                copyFiles: true,
            }, babelConfig);
        } catch (err) {
            // eslint-disable-next-line
            if (err._babel && err instanceof SyntaxError) {
                // Display codeFrame if it is an Babel Error
                err.message = `${err.message}\n${err.codeFrame}`;
            }
            log.small.error(`The ${target.toUpperCase()} build failed. ` +
                'All other potential builds will be canceled.', err);
        }

        log.small.success(`Completed ${target.toUpperCase()}\n`);
    }
};

/**
 * Builds source files based on the configuration using Babel.
 *
 * @param {Object} settings - Roc settings object.
 *
 * @returns {Function} - A correct Roc action.
 */
export default ({ context: { config: { settings } } }) => (targets) => {
    // If not at least on of the targets matches the valid ones it will ignore it. Makes it smarter when combining.
    if (isSomeTargetValid(targets)) {
        return () => () => new Promise((resolve) => {
            const startTime = process.hrtime();
            targets.forEach(target => buildWithBabel(target, settings));
            const totalTime = process.hrtime(startTime);

            log.small.success('All module builds completed in ' +
                `${prettyMs((totalTime[0] * 1000) + (totalTime[1] / 1000000))}`);

            resolve();
        });
    }

    return undefined;
};
