|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const path = require('path'); |
| 4 | + |
| 5 | +const { hasDep, configFor, pipe, merge, forFiles } = require('./-utils'); |
| 6 | + |
| 7 | +/** |
| 8 | + * @param {import('./types').Options} options |
| 9 | + */ |
| 10 | +const configBuilder = (options = {}) => { |
| 11 | + let hasTypeScript = hasDep('typescript'); |
| 12 | + |
| 13 | + let personalPreferences = pipe({}, (config) => merge(config, require('./base').base)); |
| 14 | + |
| 15 | + if (options.prettierIntegration) { |
| 16 | + personalPreferences = merge(personalPreferences, require('./rules/prettier').resolveRule()); |
| 17 | + } |
| 18 | + |
| 19 | + return { |
| 20 | + modules: { |
| 21 | + get js() { |
| 22 | + return pipe( |
| 23 | + { |
| 24 | + parserOptions: { |
| 25 | + sourceType: 'module', |
| 26 | + ecmaVersion: 'latest', |
| 27 | + }, |
| 28 | + env: { |
| 29 | + browser: false, |
| 30 | + node: true, |
| 31 | + es6: true, |
| 32 | + }, |
| 33 | + plugins: ['n'], |
| 34 | + extends: ['plugin:n/recommended'], |
| 35 | + }, |
| 36 | + (config) => merge(config, personalPreferences), |
| 37 | + (config) => merge(config, require('./rules/imports')) |
| 38 | + ); |
| 39 | + }, |
| 40 | + get ts() { |
| 41 | + if (!hasTypeScript) return; |
| 42 | + |
| 43 | + return pipe( |
| 44 | + { |
| 45 | + parserOptions: { |
| 46 | + sourceType: 'module', |
| 47 | + ecmaVersion: 'latest', |
| 48 | + }, |
| 49 | + env: { |
| 50 | + browser: false, |
| 51 | + node: true, |
| 52 | + es6: true, |
| 53 | + }, |
| 54 | + plugins: ['n'], |
| 55 | + extends: ['plugin:n/recommended', 'plugin:import/typescript'], |
| 56 | + }, |
| 57 | + (config) => merge(config, personalPreferences), |
| 58 | + (config) => merge(config, require('./rules/imports')), |
| 59 | + (config) => merge(config, require('./rules/typescript')) |
| 60 | + ); |
| 61 | + }, |
| 62 | + }, |
| 63 | + commonjs: { |
| 64 | + get js() { |
| 65 | + return pipe( |
| 66 | + { |
| 67 | + parserOptions: { |
| 68 | + sourceType: 'script', |
| 69 | + ecmaVersion: 'latest', |
| 70 | + }, |
| 71 | + env: { |
| 72 | + browser: false, |
| 73 | + node: true, |
| 74 | + es6: true, |
| 75 | + }, |
| 76 | + plugins: ['n'], |
| 77 | + extends: ['plugin:n/recommended'], |
| 78 | + }, |
| 79 | + (config) => merge(config, personalPreferences), |
| 80 | + (config) => merge(config, require('./rules/imports')) |
| 81 | + ); |
| 82 | + }, |
| 83 | + get ts() { |
| 84 | + let hasTypeScript = hasDep('typescript'); |
| 85 | + |
| 86 | + if (!hasTypeScript) return; |
| 87 | + |
| 88 | + return pipe( |
| 89 | + { |
| 90 | + parserOptions: { |
| 91 | + sourceType: 'script', |
| 92 | + ecmaVersion: 'latest', |
| 93 | + }, |
| 94 | + env: { |
| 95 | + browser: false, |
| 96 | + node: true, |
| 97 | + es6: true, |
| 98 | + }, |
| 99 | + plugins: ['n'], |
| 100 | + extends: ['plugin:n/recommended', 'plugin:import/typescript'], |
| 101 | + }, |
| 102 | + (config) => merge(config, personalPreferences), |
| 103 | + (config) => merge(config, require('./rules/imports')), |
| 104 | + (config) => merge(config, require('./rules/typescript')) |
| 105 | + ); |
| 106 | + }, |
| 107 | + }, |
| 108 | + get tests() { |
| 109 | + return { |
| 110 | + rules: { |
| 111 | + // devDependencies |
| 112 | + 'n/no-unpublished-import': 'off', |
| 113 | + // side-effects are... fine? |
| 114 | + 'import/no-unassigned-import': 'off', |
| 115 | + }, |
| 116 | + }; |
| 117 | + }, |
| 118 | + get config() { |
| 119 | + return { |
| 120 | + rules: { |
| 121 | + // devDependencies |
| 122 | + 'n/no-unpublished-import': 'off', |
| 123 | + }, |
| 124 | + }; |
| 125 | + }, |
| 126 | + }; |
| 127 | +}; |
| 128 | + |
| 129 | +function crossPlatform(options = {}) { |
| 130 | + let config = configBuilder(options); |
| 131 | + |
| 132 | + return configFor([ |
| 133 | + forFiles('**/*.cjs', config.commonjs.js), |
| 134 | + forFiles('**/*.cts', config.commonjs.ts), |
| 135 | + forFiles('**/*.{mts,ts}', config.modules.ts), |
| 136 | + forFiles('**/*.{mjs,js}', config.modules.js), |
| 137 | + forFiles('config/**/*', config.config), |
| 138 | + forFiles(['vitest.config.ts', 'tests/**/*'], config.tests), |
| 139 | + ]); |
| 140 | +} |
| 141 | + |
| 142 | +module.exports = crossPlatform; |
0 commit comments