all files / lib/ utils.js

100% Statements 16/16
100% Branches 2/2
100% Functions 0/0
100% Lines 15/15
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                                                                                                   
'use strict';
 
const fs = require('fs');
const path = require('path');
 
/**
 * Parses a path into directory and filename or file regular expression pattern.
 *
 * @example console.log(utils.parseWatchPath(input));
 * @param {String} input Path to parse.
 * @return {Object} Object with directory, filename (pattern) and boolean flag.
 * @public
 */
 
const parseWatchPath = input => {
 
    const recursivePattern = /\/\*\*\/\/*/;
 
    const recursive = recursivePattern.test(input);
    const directory = path.dirname(input.replace(recursivePattern, '/'));
    const filename = path.basename(input).replace(/^\*/, '');
 
    return {
        directory,
        filename,
        recursive
    };
 
};
 
 
/**
 * Find local .eslintrc file.
 *
 * @example utils.findESLintConfigFile(input).then((path) => console.log(path));
 * @param {String} input Directory or file.
 * @return {Object} Promise
 * @public
 */
 
const findESLintConfigFile = input => new Promise(resolve => {
 
    const localConfigPath = path.resolve(path.join(input, '.eslintrc'));
 
    fs.stat(localConfigPath, err => {
 
        if (err) {
 
            resolve(path.resolve(path.join(__dirname, '../.eslintrc')));
 
        } else {
 
            resolve(localConfigPath);
 
        }
 
    });
 
});
 
module.exports = {
    findESLintConfigFile,
    parseWatchPath
};