All files / src/commands/add-eslint index.js

82.14% Statements 23/28
64.71% Branches 11/17
66.67% Functions 10/15
82.14% Lines 23/28

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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120                        12x               12x     12x               12x                                   12x       11x     11x       11x         11x         11x       11x         11x         11x           1x 1x 1x 1x         11x           1x 1x         1x 1x       11x        
import {join} from 'path';
import merge from 'lodash.merge';
import {
    EslintConfigModuleEditor,
    PackageJsonEditor,
    Scaffolder,
    allDoExist,
    allDoNotExist,
    allDoNotExistSync,
    install
} from '../../api';
 
const ESLINT_DEPENDENCIES = [
    'eslint@^7',
    'babel-eslint',
    'eslint-config-omaha-prime-grade',
    'eslint-plugin-import',
    // 'eslint-plugin-promise'
    'watch'
];
const ESLINT_REACT_PLUGINS = [
    'eslint-plugin-react'
];
const ESLINT_SETTINGS = {
    extends: [
        `'omaha-prime-grade'`,
        `'plugin:import/errors'`,
        `'plugin:import/warnings'`,
        `'plugin:promise/recommended'`
    ]
};
const REACT_ESLINT_SETTINGS = merge({}, ESLINT_SETTINGS, {
    parserOptions: {
        ecmaFeatures: {
            jsx: true
        }
    },
    plugins: [
        `'jsx-a11y'`
    ],
    extends: [,,,,
        `'plugin:react/recommended'`,
        `'plugin:jsx-a11y/recommended'`
    ]
});
/**
 * @type {task[]}
 * @see https://eslint.org/
 */
export const tasks = [
    {
        text: 'Create ESLint configuration and .eslintignore files',
        task: async ({overwrite}) => {
            await (new EslintConfigModuleEditor())
                .create()
                .commit();
            (allDoNotExistSync('.eslintignore') || overwrite) && await (new Scaffolder(join(__dirname, 'templates')))
                .copy('.eslintignore')
                .commit();
        },
        condition: ({overwrite}) => overwrite || allDoNotExist('.eslintrc.js', '.eslintrc', '.eslintrc.json', '.eslintrc.yml')
    },
    {
        text: 'Add lint tasks to package.json',
        task: async ({sourceDirectory}) => {
            const scripts = {
                lint: `eslint . -c ./.eslintrc.js --ext .js,.jsx --fix`,
                'lint:ing': `watch "npm run lint" ${sourceDirectory}`,
                'lint:tests': 'eslint __tests__/**/*.js -c ./.eslintrc.js --fix --no-ignore'
            };
            await (new PackageJsonEditor())
                .extend({scripts})
                .commit();
        },
        condition: () => allDoExist('package.json')
    },
    {
        text: 'Install ESLint dependencies',
        task: ({skipInstall}) => install(ESLINT_DEPENDENCIES, {dev: true, skipInstall}),
        condition: ({skipInstall}) => !skipInstall && allDoExist('package.json')
    },
    {
        text: 'Install ESLint React plugins',
        task: ({skipInstall}) => install(ESLINT_REACT_PLUGINS, {dev: true, skipInstall}),
        condition: ({skipInstall, useReact}) => !skipInstall && useReact && allDoExist('package.json'),
        optional: ({useReact}) => useReact
    },
    {
        text: 'Add lit-html support to ESLint configuration file',
        task: async ({browser, skipInstall}) => {
            const env = {browser};
            const plugins = [`'lit'`];
            await install(['eslint-plugin-lit'], {dev: true, skipInstall});
            await (new EslintConfigModuleEditor())
                .extend(merge({}, ESLINT_SETTINGS, {env, plugins}))
                .extend({extends: [,,, `'plugin:lit/recommended'`]})
                .commit();
        },
        condition: ({browser, useReact}) => browser && !useReact && allDoExist('package.json', '.eslintrc.js'),
        optional: ({browser, useReact}) => browser && !useReact
    },
    {
        text: 'Add React support to ESLint configuration file',
        task: async ({browser, skipInstall}) => {
            const env = {browser};
            const settings = {
                react: {
                    version: `'detect'`
                }
            };
            await install(['eslint-plugin-jsx-a11y'], {dev: true, skipInstall});
            await (new EslintConfigModuleEditor())
                .extend(merge({}, REACT_ESLINT_SETTINGS, {env, settings}))
                .commit();
        },
        condition: ({useReact}) => useReact && allDoExist('.eslintrc.js'),
        optional: ({useReact}) => useReact
    }
];
export default tasks;