Skip to content

Commit

Permalink
get internalPaths for perfectionis dynamically
Browse files Browse the repository at this point in the history
  • Loading branch information
razvanMiu committed May 14, 2024
1 parent 592d479 commit 6ab568a
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 7 deletions.
14 changes: 10 additions & 4 deletions packages/eslint-config-custom/base.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
const getFilePath = require('./utils').getFilePath
const perfRules = require('./perfRules')

let currentProject

const isProduction = process.env.NODE_ENV === 'production'
const ignoreInProduction = ['*.stories.ts', '*.stories.tsx']

const project = [
`${__dirname}/../*/tsconfig.json`,
`${__dirname}/tsconfig.json`,
]
try {
currentProject = [getFilePath(process.cwd(), 'tsconfig.json')]
} catch (e) {
currentProject = []
}

const project = [`${__dirname}/../*/tsconfig.json`, ...currentProject]

module.exports = {
env: {
Expand Down
14 changes: 11 additions & 3 deletions packages/eslint-config-custom/perfRules.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
/* eslint-disable perfectionist/sort-objects */
const getFile = require('./utils').getFile

let packageJson, internalPaths
const packages = [
'@edw/base',
'@edw/drupal',
'tsconfig',
'eslint-config-custom',
]

const apps = ['@edw/mlf', '@edw/idra']
try {
packageJson = getFile(process.cwd(), 'package.json')
internalPaths = packageJson.internalPaths || []
} catch (e) {
internalPaths = []
}

const perfRules = {
'perfectionist/sort-array-includes': 'error',
Expand Down Expand Up @@ -35,7 +43,7 @@ const perfRules = {
react: ['react react-* next'],
},
value: {
'internal-type': apps.reduce((acc, app) => {
'internal-type': internalPaths.reduce((acc, app) => {
acc.push(`${app}/**/@types`)
acc.push(`${app}/**/@types/**`)
return acc
Expand All @@ -52,7 +60,7 @@ const perfRules = {
server: ['server-only'],
},
},
'internal-pattern': apps.reduce(
'internal-pattern': internalPaths.reduce(
(acc, app) => {
acc.push(app)
acc.push(`${app}/**`)
Expand Down
36 changes: 36 additions & 0 deletions packages/eslint-config-custom/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
function getFile(path = process.cwd(), fileName) {
if (!fileName) {
throw new Error('No fileName provided')
}
if (path === '/' || path === '') {
throw new Error(`No ${fileName} found`)
}
try {
return require(`${path}/${fileName}`)
} catch (e) {
const parentPath = path.split('/').slice(0, -1).join('/')
return getFile(parentPath, fileName)
}
}

function getFilePath(path = process.cwd(), fileName) {
if (!fileName) {
throw new Error('No fileName provided')
}
if (path === '/' || path === '') {
throw new Error(`No ${fileName} found`)
}
try {
const filePath = `${path}/${fileName}`
require(filePath)
return filePath
} catch (e) {
const parentPath = path.split('/').slice(0, -1).join('/')
return getFilePath(parentPath, fileName)
}
}

module.exports = {
getFile,
getFilePath,
}

0 comments on commit 6ab568a

Please sign in to comment.