Skip to content

Commit

Permalink
3.10.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Igorkowalski94 committed Nov 2, 2024
1 parent a75959f commit 9f6a44a
Show file tree
Hide file tree
Showing 60 changed files with 504 additions and 248 deletions.
4 changes: 4 additions & 0 deletions fileComposition.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,10 @@
"type": "object",
"additionalProperties": false,
"properties": {
"projectRoot": {
"type": "string",
"default": "."
},
"filesRules": {
"type": "array",
"default": [],
Expand Down
4 changes: 4 additions & 0 deletions independentModules.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@
"type": "boolean",
"default": true
},
"tsconfigPath": {
"type": "string",
"default": "./tsconfig.json"
},
"pathAliases": {
"type": "object",
"default": {},
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"author": "Igor Kowalski (Igorkowalski94)",
"name": "eslint-plugin-project-structure",
"version": "3.9.2",
"version": "3.10.0",
"license": "MIT",
"description": "Powerful ESLint plugin with rules to help you achieve a scalable, consistent, and well-structured project. Create your own framework! Define your folder structure, file composition, advanced naming conventions, and create independent modules. Take your project to the next level and save time by automating the review of key principles of a healthy project! react folder structure react file structure react project structure react conventions architecture react next.js angular node solid vue svelte",
"keywords": [
Expand Down
8 changes: 4 additions & 4 deletions src/helpers/cleanUpErrorFromCache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe("cleanUpErrorFromCache", () => {

expect(
cleanUpErrorFromCache({
cwd: "cwd",
projectRoot: "projectRoot",
filename: "",
}),
).toEqual(undefined);
Expand All @@ -37,7 +37,7 @@ describe("cleanUpErrorFromCache", () => {
(unlinkSync as jest.Mock).mockImplementation(unlinkSyncMock);

cleanUpErrorFromCache({
cwd: "cwd",
projectRoot: "projectRoot",
filename: "filename1",
});

Expand All @@ -57,12 +57,12 @@ describe("cleanUpErrorFromCache", () => {
);

cleanUpErrorFromCache({
cwd: "cwd",
projectRoot: "projectRoot",
filename: "filename1",
});

expect(createProjectStructureCacheFileMock).toHaveBeenCalledWith({
cwd: "cwd",
projectRoot: "projectRoot",
projectStructureCache: [
{ errorMessage: "error2", filename: "filename2" },
],
Expand Down
12 changes: 7 additions & 5 deletions src/helpers/cleanUpErrorFromCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import { readProjectStructureCacheFile } from "helpers/readProjectStructureCache

interface CleanUpErrorFromCacheProps {
filename: string;
cwd: string;
projectRoot: string;
}

export const cleanUpErrorFromCache = ({
cwd,
projectRoot,
filename,
}: CleanUpErrorFromCacheProps): void => {
const projectStructureCache = readProjectStructureCacheFile(cwd);
const projectStructureCache = readProjectStructureCacheFile(projectRoot);

if (!projectStructureCache) return;

Expand All @@ -24,10 +24,12 @@ export const cleanUpErrorFromCache = ({
);

if (!projectStructureCacheClean.length)
return unlinkSync(path.join(cwd, PROJECT_STRUCTURE_CACHE_FILE_NAME));
return unlinkSync(
path.join(projectRoot, PROJECT_STRUCTURE_CACHE_FILE_NAME),
);

createProjectStructureCacheFile({
cwd,
projectRoot,
projectStructureCache: projectStructureCacheClean,
});
};
6 changes: 3 additions & 3 deletions src/helpers/createProjectStructureCacheFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import { ProjectStructureCache } from "types";

interface CreateProjectStructureCacheFileProps {
projectStructureCache: ProjectStructureCache;
cwd: string;
projectRoot: string;
}

export const createProjectStructureCacheFile = ({
cwd,
projectRoot,
projectStructureCache,
}: CreateProjectStructureCacheFileProps): void => {
const filePath = path.join(cwd, PROJECT_STRUCTURE_CACHE_FILE_NAME);
const filePath = path.join(projectRoot, PROJECT_STRUCTURE_CACHE_FILE_NAME);
const jsonData = JSON.stringify(projectStructureCache, null, 2);

writeFileSync(filePath, jsonData, "utf8");
Expand Down
22 changes: 22 additions & 0 deletions src/helpers/getProjectRoot.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import path from "path";

import { getProjectRoot } from "helpers/getProjectRoot";

describe("getProjectRoot", () => {
it("should return rootPath when !projectRootConfig", () => {
jest
.spyOn(path, "dirname")
.mockReturnValue(path.resolve("c:/users/project/node_modules/test.ts"));

expect(getProjectRoot()).toEqual(path.resolve("c:/users/project"));
});

it("should return rootPath with projectRootConfig", () => {
jest
.spyOn(path, "dirname")
.mockReturnValue(path.resolve("c:/users/project/node_modules/test.ts"));
expect(getProjectRoot("packages/package-name")).toEqual(
path.resolve("c:/users/project/packages/package-name"),
);
});
});
12 changes: 12 additions & 0 deletions src/helpers/getProjectRoot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import path, { dirname, sep } from "path";

export const getProjectRoot = (projectRootConfig?: string): string => {
const dirnameSplit = dirname(__filename).split(sep);

const indexOfNodeModules = dirnameSplit.indexOf("node_modules");
const rootPath = dirnameSplit.slice(0, indexOfNodeModules).join(sep);

if (!projectRootConfig) return rootPath;

return path.resolve(rootPath, projectRootConfig);
};
12 changes: 6 additions & 6 deletions src/helpers/handleCache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ describe("handleCache", () => {
);

handleCache({
cwd: "cwd",
projectRoot: "projectRoot",
errorCache: { errorMessage: "error", filename: "" },
});

expect(createProjectStructureCacheFileMock).toHaveBeenCalledWith({
cwd: "cwd",
projectRoot: "projectRoot",
projectStructureCache: [{ errorMessage: "error", filename: "" }],
});
});
Expand All @@ -54,12 +54,12 @@ describe("handleCache", () => {
);

handleCache({
cwd: "cwd",
projectRoot: "projectRoot",
errorCache: { errorMessage: "error1", filename: "" },
});

expect(createProjectStructureCacheFileMock).not.toHaveBeenCalledWith({
cwd: "cwd",
projectRoot: "projectRoot",
projectStructureCache: [
{ errorMessage: "error1", filename: "1" },
{ errorMessage: "error2", filename: "2" },
Expand All @@ -86,12 +86,12 @@ describe("handleCache", () => {
);

handleCache({
cwd: "cwd",
projectRoot: "projectRoot",
errorCache: { errorMessage: "error3", filename: "3" },
});

expect(createProjectStructureCacheFileMock).not.toHaveBeenCalledWith({
cwd: "cwd",
projectRoot: "projectRoot",
projectStructureCache: [
{ errorMessage: "error1", filename: "1" },
{ errorMessage: "error2", filename: "2" },
Expand Down
13 changes: 8 additions & 5 deletions src/helpers/handleCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@ import { readProjectStructureCacheFile } from "helpers/readProjectStructureCache

interface HandleCacheProps {
errorCache: ErrorCache;
cwd: string;
projectRoot: string;
}

export const handleCache = ({ cwd, errorCache }: HandleCacheProps): void => {
const projectStructureCache = readProjectStructureCacheFile(cwd);
export const handleCache = ({
projectRoot,
errorCache,
}: HandleCacheProps): void => {
const projectStructureCache = readProjectStructureCacheFile(projectRoot);

if (!projectStructureCache)
return createProjectStructureCacheFile({
cwd,
projectRoot,
projectStructureCache: [errorCache],
});

Expand All @@ -30,7 +33,7 @@ export const handleCache = ({ cwd, errorCache }: HandleCacheProps): void => {
if (isErrorInCache) return;

createProjectStructureCacheFile({
cwd,
projectRoot,
projectStructureCache: [errorCache, ...projectStructureCacheClean],
});
};
6 changes: 3 additions & 3 deletions src/helpers/isErrorInCache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe("isErrorInCache", () => {

expect(
isErrorInCache({
cwd: "cwd",
projectRoot: "projectRoot",
errorCache: { errorMessage: "error", filename: "" },
}),
).toEqual(false);
Expand All @@ -28,7 +28,7 @@ describe("isErrorInCache", () => {

expect(
isErrorInCache({
cwd: "cwd",
projectRoot: "projectRoot",
errorCache: { errorMessage: "error", filename: "filename1" },
}),
).toEqual(true);
Expand All @@ -41,7 +41,7 @@ describe("isErrorInCache", () => {

expect(
isErrorInCache({
cwd: "cwd",
projectRoot: "projectRoot",
errorCache: { errorMessage: "error", filename: "filename1" },
}),
).toEqual(false);
Expand Down
8 changes: 4 additions & 4 deletions src/helpers/isErrorInCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ import { readProjectStructureCacheFile } from "helpers/readProjectStructureCache

interface IsErrorInCacheProps {
errorCache: ErrorCache;
cwd: string;
projectRoot: string;
}

export const isErrorInCache = ({
cwd,
projectRoot,
errorCache,
}: IsErrorInCacheProps): boolean => {
handleCache({
cwd,
projectRoot,
errorCache,
});

const projectStructureCache = readProjectStructureCacheFile(cwd);
const projectStructureCache = readProjectStructureCacheFile(projectRoot);

const cacheData = projectStructureCache?.find(
(cache) => cache.errorMessage === errorCache.errorMessage,
Expand Down
17 changes: 12 additions & 5 deletions src/helpers/readConfigFile/helpers/getConfigPath.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,33 @@ import path from "path";

import { getMissingConfigFileError } from "errors/getMissingConfigFileError";

import { getProjectRoot } from "helpers/getProjectRoot";
import { getConfigPath } from "helpers/readConfigFile/helpers/getConfigPath";

jest.mock("helpers/getProjectRoot", () => ({ getProjectRoot: jest.fn() }));

describe("getConfigPath", () => {
afterEach(() => {
jest.restoreAllMocks();
});

it("should throw getMissingConfigFileError when config path is missing", () => {
expect(() =>
getConfigPath({ cwd: "src", key: "ruleKey", settings: {} }),
).toThrow(getMissingConfigFileError("ruleKey"));
(getProjectRoot as jest.Mock).mockReturnValue("src");

expect(() => getConfigPath({ key: "ruleKey", settings: {} })).toThrow(
getMissingConfigFileError("ruleKey"),
);
});

it("should return config path when settings contain config path - relative", () => {
jest
.spyOn(path, "resolve")
.mockImplementation(() => "C:\\relative\\src\\config.json");

(getProjectRoot as jest.Mock).mockReturnValue("C:/relative/src");

expect(
getConfigPath({
cwd: "C:/relative/src",
key: "ruleKey",
settings: { ruleKey: "config.json" },
}),
Expand All @@ -34,9 +40,10 @@ describe("getConfigPath", () => {
.spyOn(path, "resolve")
.mockImplementation(() => "D:\\relative\\src\\config.json");

(getProjectRoot as jest.Mock).mockReturnValue("C:/absolute/src");

expect(
getConfigPath({
cwd: "C:/absolute/src",
key: "ruleKey",
settings: {
ruleKey: "D:\\relative\\src\\config.json",
Expand Down
6 changes: 3 additions & 3 deletions src/helpers/readConfigFile/helpers/getConfigPath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ import { SharedConfigurationSettings } from "@typescript-eslint/utils/dist/ts-es

import { getMissingConfigFileError } from "errors/getMissingConfigFileError";

import { getProjectRoot } from "helpers/getProjectRoot";

interface GetConfigPathProps {
cwd: string;
settings: SharedConfigurationSettings;
key: string;
}

export const getConfigPath = ({
cwd,
settings,
key,
}: GetConfigPathProps): string => {
const configPath = settings[key];

if (!configPath) throw getMissingConfigFileError(key);

return path.resolve(cwd, configPath as string);
return path.resolve(getProjectRoot(), configPath as string);
};
5 changes: 0 additions & 5 deletions src/helpers/readConfigFile/readConfigFile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ describe("readConfigFile", () => {
it("should return config from options", () => {
expect(
readConfigFile({
cwd: "",
key: "",
options: [{ name: "options" }],
settings: {},
Expand All @@ -49,7 +48,6 @@ describe("readConfigFile", () => {

expect(
readConfigFile({
cwd: "",
key: "",
options: undefined,
settings: {},
Expand All @@ -65,7 +63,6 @@ describe("readConfigFile", () => {

expect(
readConfigFile({
cwd: "",
key: "",
options: undefined,
settings: {},
Expand All @@ -81,7 +78,6 @@ describe("readConfigFile", () => {

expect(
readConfigFile({
cwd: "",
key: "",
options: undefined,
settings: {},
Expand All @@ -96,7 +92,6 @@ describe("readConfigFile", () => {

expect(() =>
readConfigFile({
cwd: "",
key: "",
options: undefined,
settings: {},
Expand Down
Loading

0 comments on commit 9f6a44a

Please sign in to comment.