{"size": 697, "ext": "js", "max_stars_repo_path": "lib/plugin.js", "max_stars_repo_name": "gemini-testing/gemini-imageoptim", "max_stars_repo_stars_event_min_datetime": "2017-04-25T06:15:44.000Z", "max_stars_repo_stars_event_max_datetime": "2018-01-29T08:31:57.000Z", "max_issues_repo_path": "lib/plugin.js", "max_issues_repo_name": "gemini-testing/gemini-optipng", "max_issues_repo_issues_event_min_datetime": "2016-08-23T15:26:18.000Z", "max_issues_repo_issues_event_max_datetime": "2018-11-20T22:45:04.000Z", "max_forks_repo_path": "lib/plugin.js", "max_forks_repo_name": "gemini-testing/gemini-imageoptim", "max_forks_repo_forks_event_min_datetime": "2017-11-06T12:52:12.000Z", "max_forks_repo_forks_event_max_datetime": "2022-03-19T07:54:03.000Z"}
{"max_stars_count": 2, "max_issues_count": 6, "max_forks_count": 2, "avg_line_length": 23.2333333333, "max_line_length": 104, "alphanum_fraction": 0.5868005739}
'use strict';

const chalk = require('chalk');
const _ = require('lodash');

const defaults = require('./defaults');
const Optimizer = require('./optimizer');

const validateOpts = (opts) => {
    if (opts.level < 0 || !Number.isInteger(opts.level)) {
        throw new Error(
            `${chalk.red('gemini-optipng')}: Level option must be a positive integer number from 0 to 7`
        );
    }
};

module.exports = (gemini, opts) => {
    if (!_.isObject(opts)) {
        return;
    }

    opts = defaults(opts);

    validateOpts(opts);

    gemini.on('startRunner', (runner) => {
        runner.on('updateResult', (data) => data.updated && Optimizer.create(opts, data).exec());
    });
};
