All files / src/utils common.js

89.74% Statements 35/39
60% Branches 3/5
83.33% Functions 15/18
92% Lines 23/25

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          25x             25x 193x 182x 25x 25x 25x 25x 470x 25x                                 86x                                   25x 2x 2x             25x           25x 60x 60x             25x                       25x 55x 111x             25x
import {join} from 'path';
import {is} from 'ramda';
import {pathExists, pathExistsSync} from 'fs-extra';
import prettier from 'prettier';
 
const PRETTIER_OPTIONS = {
    bracketSpacing: false,
    parser: 'json-stringify',
    printWidth: 150,
    tabWidth: 4,
    singleQuote: true
};
const newMap = val => new Map(val);
const joinPath = name => join(process.cwd(), name);
const checkPathExists = name => name |> joinPath |> pathExists;
const checkPathExistsSync = name => name |> joinPath |> pathExistsSync;
export const dict = val => val |> Object.entries |> newMap;
export const isEmptyString = data => typeof data === 'string' && data.length === 0;
export const parse = data => data |> JSON.stringify |> JSON.parse;
export const maybeApply = (val, options) => is(Function)(val) ? val(options) : val;
export const getBinDirectory = path => {
    const [packageDirectory] = path.split('Makefile');
    return `${packageDirectory}node_modules/.bin/`;
};
/**
 * Format input code using Prettier
 * @param {*} [code=''] Code to be formatted
 * @example <caption>Prettier options</caption>
 * {
 *     bracketSpacing: false,
 *     parser: 'json-stringify',
 *     printWidth: 150,
 *     tabWidth: 4,
 *     singleQuote: true
 * }
 * @return {string} Code formatted by Prettier
 */
export const format = (code = {}) => prettier
    .format(JSON.stringify(code), PRETTIER_OPTIONS)
    .replace(/"/g, '');
/**
 * Check that at least one file or files exist
 * @param  {...string} args File or folder path(s)
 * @example
 * // some/folder/
 * //   ├─ foo.js
 * //   └── bar.js
 * const hasFoo = someDoExist('some/folder/foo.js');
 * const hasBaz = someDoExist('some/folder/baz.js');
 * const hasSomething = someDoExist('some/folder/bar.js', 'some/folder/baz.js');
 * console.log(hasFoo); // true
 * console.log(hasBaz); // false
 * console.log(hasSomething); // true
 * @return {boolean} Some files/path do exist (true) or all do not exist (false)
 */
export const someDoExist = async (...args) => {
    const checks = await Promise.all(args.map(checkPathExists));
    return checks.some(Boolean);
};
/**
 * Check that at least one file or files exist (synchronous version of {@link someDoExist})
 * @param  {...string} args File or folder path(s)
 * @return {boolean} Some files/path do exist (true) or all do not exist (false)
 */
export const someDoExistSync = (...args) => args.map(checkPathExistsSync).some(Boolean);
/**
 * Check that all files exist
 * @param  {...string} args File of folder paths
 * @return {boolean} All files/paths exist (true) or do not (false)
 */
export const allDoExist = async (...args) => {
    const checks = await Promise.all(args.map(checkPathExists));
    return checks.every(Boolean);
};
/**
 * Check that all files exist (synchronous version of {@link allDoExist})
 * @param  {...string} args File of folder paths
 * @return {boolean} All files/paths exist (true) or do not (false)
 */
export const allDoExistSync = (...args) => args.map(checkPathExistsSync).every(Boolean);
/**
 * Check that all files do not exist
 * @example
 * // some/folder/
 * //   ├─ foo.js
 * //   └── bar.js
 * const noPackageJson = allDoNotExist('some/folder/package.json');
 * console.log(noPackageJson); // true
 * @param  {...string} args File or folder path(s)
 * @return {boolean} All files/paths do not exist (true) or some do (false)
 */
export const allDoNotExist = async (...args) => {
    const checks = await Promise.all(args.map(checkPathExists));
    return checks.every(val => !val);
};
/**
 * Check that all files do not exist (synchronous version of {@link allDoNotExist})
 * @param  {...string} args File or folder path(s)
 * @return {boolean} All files/paths do not exist (true) or some do (false)
 */
export const allDoNotExistSync = (...args) => args.map(checkPathExistsSync).every(val => !val);