Skip to content

Commit

Permalink
feat: support environment without .git directory
Browse files Browse the repository at this point in the history
  • Loading branch information
ThisIsManta committed Feb 3, 2024
1 parent ff219fc commit ccfdc88
Showing 1 changed file with 28 additions and 34 deletions.
62 changes: 28 additions & 34 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,21 @@
const fs = require('fs')
const path = require('path')

const rootPath = findRootPath(__dirname)
if (!rootPath) {
throw new Error('Could not find the root directory of the repository.')
}

const plugin = fs.readdirSync(rootPath, { withFileTypes: true })
.find(item =>
item.isFile() && /^\.eslint-plugin-local\.c?js$/.test(item.name) ||
item.isDirectory() && item.name === '.eslint-plugin-local'
)

if (!plugin) {
const rulePath = findRulePath(__dirname)
if (!rulePath) {
throw new Error('Could not find ".eslint-plugin-local" file or directory.')
}

if (plugin.isDirectory()) {
const index = fs.readdirSync(path.join(rootPath, plugin.name), { withFileTypes: true })
.find(item => item.isFile() && /^index\.c?js$/.test(item.name))

if (!index) {
throw new Error('Could not find "index" file in ".eslint-plugin-local" directory.')
}

// Do not use `index.path` as it is only available starting from Node.js v18.17.0 but VSCode uses v18.15.0 as of writing
module.exports = require(path.posix.join(rootPath, plugin.name, index.name))
} else {
module.exports = require(path.posix.join(rootPath, plugin.name))
}
module.exports = require(rulePath) || {}

const { name, version } = require('./package.json')
module.exports.meta = module.exports.meta || {}
module.exports.meta.name = name
module.exports.meta.version = version
module.exports.rules = module.exports.rules || {}

if (process.argv.includes('test')) {
if (Object.keys(module.exports.rules || {}).length === 0) {
if (Object.keys(module.exports.rules).length === 0) {
throw new Error('Could not find any rules.')
}

Expand All @@ -51,21 +30,36 @@ if (process.argv.includes('test')) {
}

/**
* Returns the current dependent repository path which `.git` directory resides.
*
* Do not write `require('../../package.json')` which might break when using PNPM.
* Returns the file path representing `.eslint-plugin-local` or `null` if not found.
* @param {string} workPath
* @returns {string | null}
*/
function findRootPath(workPath) {
function findRulePath(workPath) {
if (workPath === path.dirname(workPath)) {
return null
}

const testPath = path.join(workPath, '.git')
if (fs.existsSync(testPath) && fs.lstatSync(testPath).isDirectory()) {
return workPath
const item = fs.readdirSync(workPath, { withFileTypes: true })
.find(item =>
item.isFile() && /^\.eslint-plugin-local\.c?js$/.test(item.name) ||
item.isDirectory() && item.name === '.eslint-plugin-local'
)

if (item?.isFile()) {
// Do not write `item.path` as it is only available starting from Node.js v18.17.0 but VSCode uses v18.15.0 as of writing
return path.join(workPath, item.name)
}

if (item?.isDirectory()) {
for (const file of ['index.cjs', 'index.js']) {
const testPath = path.join(workPath, item.name, file)
if (fs.existsSync(testPath)) {
return testPath
}
}

return null
}

return findRootPath(path.dirname(workPath))
return findRulePath(path.dirname(workPath))
}

0 comments on commit ccfdc88

Please sign in to comment.