From ccfdc880c22c1f74d91dd4e67f30f061a5721563 Mon Sep 17 00:00:00 2001 From: "Anantachai Saothong (Manta)" Date: Sun, 4 Feb 2024 02:18:41 +0700 Subject: [PATCH] feat: support environment without .git directory --- index.js | 62 +++++++++++++++++++++++++------------------------------- 1 file changed, 28 insertions(+), 34 deletions(-) diff --git a/index.js b/index.js index 1b2b80f..523c067 100644 --- a/index.js +++ b/index.js @@ -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.') } @@ -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)) }