diff --git a/CHANGELOG.md b/CHANGELOG.md index 8fcaa145..786db1ed 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,11 @@ ## UNRELEASED +## [10.0.2] 2022-08-15 + +- Fix error when absolute files sent as positional arguments on a linux system ([#232](https://github.com/nvuillam/npm-groovy-lint/issues/232)) +- Improve performances by calculating the longest command directory to send as base path to CodeNarc + ## [10.0.1] 2022-08-14 - Fix error when files sent as positional arguments ([#232](https://github.com/nvuillam/npm-groovy-lint/issues/232)) diff --git a/lib/codenarc-factory.js b/lib/codenarc-factory.js index bb9e7b0d..abcda69f 100644 --- a/lib/codenarc-factory.js +++ b/lib/codenarc-factory.js @@ -2,6 +2,7 @@ "use strict"; const debug = require("debug")("npm-groovy-lint"); +const commondir = require("commondir"); const fs = require("fs-extra"); const os = require("os"); const path = require("path"); @@ -55,8 +56,11 @@ async function prepareCodeNarcCall(options) { // Define base directory const baseBefore = (cnPath !== "." && cnPath.startsWith("/")) || cnPath.includes(":/") || cnPath.includes(":\\") ? "" : process.cwd() + "/"; - result.codeNarcBaseDir = cnPath !== "." ? baseBefore + cnPath.replace(/^"(.*)"$/, "$1") : process.cwd(); - result.codeNarcBaseDir = path.resolve(result.codeNarcBaseDir); + const codeNarcBaseDir = positionalArgs.length > 0 ? + (await getCodeNarcBaseDirFromFiles(positionalArgs)) : + cnPath !== "." ? baseBefore + cnPath.replace(/^"(.*)"$/, "$1") : + process.cwd(); + result.codeNarcBaseDir = path.resolve(codeNarcBaseDir); result.codenarcArgs.push(`-basedir=${result.codeNarcBaseDir}`); // Create ruleSet groovy file if necessary @@ -90,17 +94,18 @@ async function prepareCodeNarcCall(options) { if (directoryExists(resolvedPath)) { finalPattern = "**/" + path.normalize(pathname.replace(/[/\\]$/u, "")).replace(/\\/gu, "/") + filePatterns; } - // Absolute or cwd - relative path file - if (fs.existsSync(finalPattern)) { + // Relative with codeNarcBaseDir + else if (fs.existsSync(path.join(result.codeNarcBaseDir, finalPattern))) { const absolutePath = path.resolve(finalPattern).replace(/\\/gu, "/"); - const relativePath = path.relative(process.cwd(), path.resolve(finalPattern)).replace(/\\/gu, "/"); fileList.push(absolutePath); + const relativePath = finalPattern.replace(/\\/gu, "/"); finalPattern = "**/" + path.normalize(relativePath.replace(/[/\\]$/u, "")).replace(/\\/gu, "/"); } - // Relative with codeNardBaseDir - else if (fs.existsSync(path.join(result.codeNarcBaseDir, finalPattern))) { - const relativePath = path.relative(process.cwd(), path.join(result.codeNarcBaseDir, finalPattern)).replace(/\\/gu, "/"); - fileList.push(relativePath); + // Absolute or cwd - relative path file + else if (fs.existsSync(finalPattern)) { + const absolutePath = path.resolve(finalPattern).replace(/\\/gu, "/"); + fileList.push(absolutePath); + const relativePath = path.relative(result.codeNarcBaseDir, path.resolve(finalPattern)).replace(/\\/gu, "/"); finalPattern = "**/" + path.normalize(relativePath.replace(/[/\\]$/u, "")).replace(/\\/gu, "/"); } // Directory or ant pattern @@ -174,6 +179,25 @@ async function prepareCodeNarcCall(options) { return result; } +// Calculate longest base dir by analyzing the list of files +async function getCodeNarcBaseDirFromFiles(positionalArgs) { + // All arguments are not files + if (!positionalArgs.every(fileOrDirOrPattern => fs.existsSync(fileOrDirOrPattern) || directoryExists(fileOrDirOrPattern))) { + return process.cwd() + } + const folders = positionalArgs.map((fileOrDir) => { + // Dir + if (directoryExists(fileOrDir)) { + return path.resolve(fileOrDir); + } + // File dir + const fileAbsolute = path.resolve(fileOrDir); + return path.dirname(fileAbsolute); + }); + const baseDirFromFiles = commondir(folders); + return baseDirFromFiles +} + // Parse XML result file as js object async function parseCodeNarcResult(options, codeNarcBaseDir, codeNarcJsonResult, tmpGroovyFileName, parseErrors) { if (!codeNarcJsonResult || !codeNarcJsonResult.codeNarc || !codeNarcJsonResult.packages) { diff --git a/package-lock.json b/package-lock.json index 461411c6..09ae1680 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14,6 +14,7 @@ "axios": "^0.21.1", "chalk": "^4.1.2", "cli-progress": "^3.10.0", + "commondir": "^1.0.1", "debug": "^4.1.1", "decode-html": "^2.0.0", "find-java-home": "^1.1.0", @@ -1159,8 +1160,7 @@ "node_modules/commondir": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/commondir/-/commondir-1.0.1.tgz", - "integrity": "sha512-W9pAhw0ja1Edb5GVdIF1mjZw/ASI0AlShXM83UUGe2DVr5TdAPEA1OA8m/g8zWp9x6On7gqufY+FatDbC3MDQg==", - "dev": true + "integrity": "sha512-W9pAhw0ja1Edb5GVdIF1mjZw/ASI0AlShXM83UUGe2DVr5TdAPEA1OA8m/g8zWp9x6On7gqufY+FatDbC3MDQg==" }, "node_modules/concat-map": { "version": "0.0.1", @@ -5320,8 +5320,7 @@ "commondir": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/commondir/-/commondir-1.0.1.tgz", - "integrity": "sha512-W9pAhw0ja1Edb5GVdIF1mjZw/ASI0AlShXM83UUGe2DVr5TdAPEA1OA8m/g8zWp9x6On7gqufY+FatDbC3MDQg==", - "dev": true + "integrity": "sha512-W9pAhw0ja1Edb5GVdIF1mjZw/ASI0AlShXM83UUGe2DVr5TdAPEA1OA8m/g8zWp9x6On7gqufY+FatDbC3MDQg==" }, "concat-map": { "version": "0.0.1", diff --git a/package.json b/package.json index fdfa6343..5f398110 100644 --- a/package.json +++ b/package.json @@ -49,6 +49,7 @@ "axios": "^0.21.1", "chalk": "^4.1.2", "cli-progress": "^3.10.0", + "commondir": "^1.0.1", "debug": "^4.1.1", "decode-html": "^2.0.0", "find-java-home": "^1.1.0", diff --git a/test/lint-api.test.js b/test/lint-api.test.js index 752494c9..8734e4c0 100644 --- a/test/lint-api.test.js +++ b/test/lint-api.test.js @@ -265,11 +265,9 @@ describe("Lint with API", () => { const linter = await new NpmGroovyLint([ process.execPath, "", - "--path", - "./lib/example", "--verbose", - SAMPLE_FILE_SMALL, - SAMPLE_FILE_WITH_SPACES + path.join("./lib/example",SAMPLE_FILE_SMALL), + path.join("./lib/example",SAMPLE_FILE_WITH_SPACES) ], { verbose: true }).run();