<commit_before>import postcss from 'postcss';
import path from 'path';
import valueParser from 'postcss-value-parser';
import url from 'url';
import crypto from 'crypto';
import micromatch from 'micromatch';
import copy from './lib/copy';

const tags = [
    'path',
    'name',
    'hash',
    'ext'
];

/**
 * Helper function to ignore files
 *
 * @param  {string} filename
 * @param  {string} extra
 * @param  {Object} opts plugin options
 * @return {boolean}
 */
function ignore(fileMeta, opts) {
    if (typeof opts.ignore === 'function') {
        return opts.ignore(fileMeta, opts);
    }

    if (typeof opts.ignore === 'string' || Array.isArray(opts.ignore)) {
        return micromatch.any(fileMeta.filename + fileMeta.extra, opts.ignore);
    }

    return false;
}

/**
 * Helper function that reads the file ang get some helpful information
 * to the copy process.
 *
 * @param  {string} dirname path of the read file css
 * @param  {string} value url
 * @param  {Object} opts plugin options
 * @return {Promise} resolve => fileMeta | reject => error message
 */
function getFileMeta(dirname, value, opts) {
    const parsedUrl = url.parse(value, true);
    const filename = parsedUrl.pathname;
    const pathname = path.resolve(dirname, filename);
    const extra = (parsedUrl.search || '') + (parsedUrl.hash || '');

    // path between the basePath and the filename
    const src = opts.src.filter(item => pathname.indexOf(item) !== -1)[0];
    if (!src) {
        throw Error(`"src" not found in ${pathname}`);
    }

    const ext = path.extname(pathname);
    const fileMeta = {
        filename,
        // the absolute path without the #hash param and ?query
        absolutePath: pathname,
        fullName: path.basename(pathname),
        path: path.relative(src, path.dirname(pathname)),
        // name without extension
        name: path.basename(pathname, ext),
        // extension without the '.'
        ext: ext.slice(1),
        extra,
        src
    };

    if (ignore(fileMeta, opts)) {
        return Promise.resolve();
    }

    return fileMeta;
}


/**
 * process to copy an asset based on the css file, destination
 * and the url value
 *
 * @param {Object} result
 * @param {Object} decl postcss declaration
 * @param {Object} node postcss-value-parser
 * @param {Object} opts plugin options
 * @return {Promise}
 */
function processUrl(result, decl, node, opts) {
    // ignore absolute urls, hasshes, data uris or by **ignore option**
    if (node.value.indexOf('!') === 0) {
        node.value = node.value.slice(1);
        return Promise.resolve();
    }

    if (node.value.indexOf('/') === 0 ||
        node.value.indexOf('data:') === 0 ||
        node.value.indexOf('#') === 0 ||
        /^[a-z]+:\/\//.test(node.value)
    ) {
        return Promise.resolve();
    }

    /**
     * dirname of the read file css
     * @type {String}
     */
    const dirname = opts.inputPath(decl);

    let fileMeta = getFileMeta(dirname, node.value, opts);

    return copy(
        fileMeta.absolutePath,
        () => {
            return fileMeta.resultAbsolutePath;
        },
        contents => {
            fileMeta.contents = contents;
            fileMeta.hash = opts.hashFunction(contents);
            let tpl = opts.template;
            if (typeof tpl === 'function') {
                tpl = tpl(fileMeta);
            } else {
                tags.forEach(tag => {
                    tpl = tpl.replace(
                        '[' + tag + ']',
                        fileMeta[tag] || opts[tag]
                    );
                });
            }

            fileMeta.resultAbsolutePath = path.resolve(opts.dest, tpl);

            return Promise.resolve(
                opts.transform(fileMeta)
            )
            .then(transformed => {
                fileMeta = transformed || {};
                return fileMeta.contents;
            });
        }<commit_msg>fix regression issue with the ignore option<commit_after>import postcss from 'postcss';
import path from 'path';
import valueParser from 'postcss-value-parser';
import url from 'url';
import crypto from 'crypto';
import micromatch from 'micromatch';
import copy from './lib/copy';

const tags = [
    'path',
    'name',
    'hash',
    'ext'
];

/**
 * Helper function to ignore files
 *
 * @param  {string} filename
 * @param  {string} extra
 * @param  {Object} opts plugin options
 * @return {boolean}
 */
function ignore(fileMeta, opts) {
    if (typeof opts.ignore === 'function') {
        return opts.ignore(fileMeta, opts);
    }

    if (typeof opts.ignore === 'string' || Array.isArray(opts.ignore)) {
        return micromatch.any(fileMeta.filename + fileMeta.extra, opts.ignore);
    }

    return false;
}

/**
 * Helper function that reads the file ang get some helpful information
 * to the copy process.
 *
 * @param  {string} dirname path of the read file css
 * @param  {string} value url
 * @param  {Object} opts plugin options
 * @return {Promise} resolve => fileMeta | reject => error message
 */
function getFileMeta(dirname, value, opts) {
    const parsedUrl = url.parse(value, true);
    const filename = parsedUrl.pathname;
    const pathname = path.resolve(dirname, filename);
    const extra = (parsedUrl.search || '') + (parsedUrl.hash || '');

    // path between the basePath and the filename
    const src = opts.src.filter(item => pathname.indexOf(item) !== -1)[0];
    if (!src) {
        throw Error(`"src" not found in ${pathname}`);
    }

    const ext = path.extname(pathname);
    const fileMeta = {
        filename,
        // the absolute path without the #hash param and ?query
        absolutePath: pathname,
        fullName: path.basename(pathname),
        path: path.relative(src, path.dirname(pathname)),
        // name without extension
        name: path.basename(pathname, ext),
        // extension without the '.'
        ext: ext.slice(1),
        extra,
        src
    };

    if (ignore(fileMeta, opts)) {
        return false;
    }

    return fileMeta;
}


/**
 * process to copy an asset based on the css file, destination
 * and the url value
 *
 * @param {Object} result
 * @param {Object} decl postcss declaration
 * @param {Object} node postcss-value-parser
 * @param {Object} opts plugin options
 * @return {Promise}
 */
function processUrl(result, decl, node, opts) {
    // ignore from the css file by `!`
    if (node.value.indexOf('!') === 0) {
        node.value = node.value.slice(1);
        return Promise.resolve();
    }

    if (node.value.indexOf('/') === 0 ||
        node.value.indexOf('data:') === 0 ||
        node.value.indexOf('#') === 0 ||
        /^[a-z]+:\/\//.test(node.value)
    ) {
        return Promise.resolve();
    }

    /**
     * dirname of the read file css
     * @type {String}
     */
    const dirname = opts.inputPath(decl);

    let fileMeta = getFileMeta(dirname, node.value, opts);

    // ignore from the fileMeta config
    if (fileMeta === false) {
        return Promise.resolve();
    }

    return copy(
        fileMeta.absolutePath,
        () => {
            return fileMeta.resultAbsolutePath;
        },
        contents => {
            fileMeta.contents = contents;
            fileMeta.hash = opts.hashFunction(contents);
            let tpl = opts.template;
            if (typeof tpl === 'function') {
                tpl = tpl(fileMeta);
            } else {
                tags.forEach(tag => {
                    tpl = tpl.replace(
                        '[' + tag + ']',
                        fileMeta[tag] || opts[tag]
                    );
                });
            }

            fileMeta.resultAbsolutePath = path.resolve(opts.dest, tpl);

            return Promise.resolve(
                opts.transform(fileMeta)
            )
            .then(transformed => {
                fileMeta = transformed || {};
                return fileMeta.contents;
            });
        }
{'fraction_non_alphanumeric': 0.13381078024877496, 'fraction_numerical': 0.00201030280185953, 'mean_word_length': 2.110590074247753, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2039573', 'n_tokens_mistral': 2300, 'n_tokens_neox': 2240, 'n_words': 638}