UNPKG

eslint-config-galex

Version:
153 lines (152 loc) 5.92 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getDependencies = exports.detectNest = exports.detectTailwind = exports.detectStorybook = exports.detectTestingLibrary = exports.detectNodeTypes = exports.detectJestDom = exports.detectJest = exports.detectTypeScript = exports.detectReact = void 0; const read_pkg_up_1 = require("read-pkg-up"); const getToplevelTsConfig_1 = require("./getToplevelTsConfig"); const defaultsAndDetection_1 = require("./utils/defaultsAndDetection"); const detectReact = (dependencies) => { const hasReact = dependencies.has('react'); const isPreact = dependencies.has('preact'); const version = hasReact ? /* istanbul ignore next packageJson can't have undefined values */ dependencies.get('react') ?? null : isPreact ? /* istanbul ignore next packageJson can't have undefined values */ dependencies.get('preact') ?? null : null; return { hasReact: hasReact || isPreact, isCreateReactApp: hasReact && dependencies.has('react-scripts'), isNext: hasReact && dependencies.has('next'), isRemix: hasReact && dependencies.has('@remix-run/react'), isPreact, version, }; }; exports.detectReact = detectReact; const maybeFindTsConfig = ({ cwd, tsConfigPath }) => { try { return (0, getToplevelTsConfig_1.getTopLevelTsConfig)({ cwd, tsConfigPath }); } catch (error) { /* istanbul ignore next we only throw errors */ if (error instanceof Error) { /* istanbul ignore next warning aint that relevant */ const info = tsConfigPath ? `TypeScript found in \`package.json\`, but no config was found or is readable at "${tsConfigPath}":` : 'TypeScript found in `package.json` but no `tsconfig.json` was found:'; // eslint-disable-next-line no-console console.info(info, error.message); } return null; } }; const detectTypeScript = (dependencies, maybeFindTsConfigArgs) => { const version = dependencies.get('typescript') ?? null; const config = version ? maybeFindTsConfig(maybeFindTsConfigArgs) : null; const hasTypeScript = !!version && !!config; return { config, hasTypeScript, version: hasTypeScript ? version : null, }; }; exports.detectTypeScript = detectTypeScript; const detectJest = (dependencies) => { return dependencies.has('react-scripts') ? true : dependencies.has('jest'); }; exports.detectJest = detectJest; const detectJestDom = (dependencies) => { return dependencies.has('@testing-library/jest-dom'); }; exports.detectJestDom = detectJestDom; const detectNodeTypes = (dependencies) => { return dependencies.has('@types/node'); }; exports.detectNodeTypes = detectNodeTypes; const detectTestingLibrary = (dependencies) => { return defaultsAndDetection_1.testingLibFamily.some(pkg => dependencies.has(`@testing-library/${pkg}`)); }; exports.detectTestingLibrary = detectTestingLibrary; const detectStorybook = (dependencies) => { return { hasStorybook: [...dependencies.keys()].some(dependency => dependency.startsWith('@storybook/') && dependency !== '@storybook/testing-library'), hasStorybookTestingLibrary: dependencies.has('@storybook/testing-library'), }; }; exports.detectStorybook = detectStorybook; const detectTailwind = (dependencies) => { return dependencies.has('tailwindcss'); }; exports.detectTailwind = detectTailwind; const detectNest = (dependencies) => { return dependencies.has('@nestjs/core'); }; exports.detectNest = detectNest; const getDependencies = ({ cwd = process.cwd(), tsConfigPath, } = {}) => { try { const result = (0, read_pkg_up_1.sync)({ cwd, normalize: true }); if (!result) { throw new Error('unable to read package.json'); } const deps = new Map(Object.entries({ ...result.packageJson.dependencies, ...result.packageJson.peerDependencies, ...result.packageJson.devDependencies, })); const react = (0, exports.detectReact)(deps); const typescript = (0, exports.detectTypeScript)(deps, { cwd, tsConfigPath, }); const hasJest = (0, exports.detectJest)(deps); const hasJestDom = (0, exports.detectJestDom)(deps); const hasNodeTypes = (0, exports.detectNodeTypes)(deps); const hasTestingLibrary = (0, exports.detectTestingLibrary)(deps); const storybook = (0, exports.detectStorybook)(deps); const hasNest = (0, exports.detectNest)(deps); const hasTailwind = (0, exports.detectTailwind)(deps); return { hasJest, hasJestDom, hasNodeTypes, hasTestingLibrary, hasNest, storybook, react, typescript, hasTailwind, }; } catch (error) { // eslint-disable-next-line no-console console.error('error parsing `package.json`!', error); return { hasJest: false, hasJestDom: false, hasNodeTypes: false, hasTestingLibrary: false, hasNest: false, storybook: { hasStorybook: false, hasStorybookTestingLibrary: false, }, react: { hasReact: false, isCreateReactApp: false, isNext: false, isRemix: false, isPreact: false, version: null, }, typescript: { config: null, hasTypeScript: false, version: null, }, hasTailwind: false, }; } }; exports.getDependencies = getDependencies;