diff --git a/projects/commit-lint/src/lib/tasks/validate-nx-tags.task.ts b/projects/commit-lint/src/lib/tasks/validate-nx-tags.task.ts index b3300b1..48a2c6e 100644 --- a/projects/commit-lint/src/lib/tasks/validate-nx-tags.task.ts +++ b/projects/commit-lint/src/lib/tasks/validate-nx-tags.task.ts @@ -56,8 +56,7 @@ export function validateNxTagsTask(options: ValidateNxTagsOptions) { ); try { - // eslint-disable-next-line @typescript-eslint/no-var-requires - validateNxTags(require(nx), linterConfig, linter); + validateNxTags(linterConfig, linter); } catch (e: any) { errorWithExit(e.message); } diff --git a/projects/commit-lint/src/lib/validators/nx-tags.validator.spec.ts b/projects/commit-lint/src/lib/validators/nx-tags.validator.spec.ts index 84afb0a..e8b3d41 100644 --- a/projects/commit-lint/src/lib/validators/nx-tags.validator.spec.ts +++ b/projects/commit-lint/src/lib/validators/nx-tags.validator.spec.ts @@ -1,71 +1,6 @@ import { Linter } from '../resources/enums/linter.enum'; import { validateNxTags } from './nx-tags.validator'; -const nxJson = { - projects: { - 'pet-shop-e2e': { - tags: [], - }, - 'pet-shop': { - tags: [], - }, - 'shared-data-access': { - tags: ['scope:shared', 'type:data-access'], - }, - 'shared-resource': { - tags: ['scope:shared', 'type:resource'], - }, - 'shared-ui-avatar': { - tags: ['scope:shared', 'type:ui'], - }, - }, -}; -const nxJsonWithAdditionalDeclarations = { - projects: { - 'pet-shop-e2e': { - tags: [], - }, - 'pet-shop': { - tags: [], - }, - 'dashboard-feature': { - tags: ['scope:dashboard', 'type:feature'], - }, - 'dashboard-filter-feature': { - tags: ['scope:dashboard-filter', 'type:feature'], - }, - 'dashboard-filter-data-access': { - tags: ['scope:dashboard-filter', 'type:data-access'], - }, - 'dashboard-filter-ui': { - tags: ['scope:dashboard-filter', 'type:ui'], - }, - 'dashboard-filter-util': { - tags: ['scope:dashboard-filter', 'type:util'], - }, - 'dashboard-filter-resource': { - tags: ['scope:dashboard-filter', 'type:resource'], - }, - 'dashboard-shared-data-access': { - tags: ['scope:dashboard-shared', 'type:data-access'], - }, - 'dashboard-shared-summary-resource': { - tags: ['scope:dashboard-shared', 'type:resource'], - }, - 'dashboard-shared-summary-ui': { - tags: ['scope:dashboard-shared', 'type:ui'], - }, - 'shared-data-access': { - tags: ['scope:shared', 'type:data-access'], - }, - 'shared-resource': { - tags: ['scope:shared', 'type:resource'], - }, - 'shared-ui-avatar': { - tags: ['scope:shared', 'type:ui'], - }, - }, -}; const tslintJson = { rules: { 'nx-enforce-module-boundaries': [ @@ -291,40 +226,26 @@ const eslintjson = { describe('validateNxTags', () => { it('should not throw error with valid tslint config', () => { - expect(() => - validateNxTags(nxJson, tslintJson, Linter.TsLint) - ).not.toThrowError(); + expect(() => validateNxTags(tslintJson, Linter.TsLint)).not.toThrowError(); }); it('should throw error with invalid tslint config', () => { expect(() => - validateNxTags( - nxJsonWithAdditionalDeclarations, - tslintJsonWithAdditionalDeclarations, - Linter.TsLint - ) + validateNxTags(tslintJsonWithAdditionalDeclarations, Linter.TsLint) ).toThrowError(); }); it('should not throw error with valid eslintrc config', () => { - expect(() => - validateNxTags(nxJson, eslintrc, Linter.TsLint) - ).not.toThrowError(); + expect(() => validateNxTags(eslintrc, Linter.TsLint)).not.toThrowError(); }); it('should not throw error with valid eslintjson config', () => { - expect(() => - validateNxTags(nxJson, eslintjson, Linter.EsLint) - ).not.toThrowError(); + expect(() => validateNxTags(eslintjson, Linter.EsLint)).not.toThrowError(); }); it('should throw error with invalid eslintrc config', () => { expect(() => - validateNxTags( - nxJsonWithAdditionalDeclarations, - eslintrcWithAdditionalDeclarations, - Linter.EsLint - ) + validateNxTags(eslintrcWithAdditionalDeclarations, Linter.EsLint) ).toThrowError(); }); }); diff --git a/projects/commit-lint/src/lib/validators/nx-tags.validator.ts b/projects/commit-lint/src/lib/validators/nx-tags.validator.ts index 85fe4e8..91a5781 100644 --- a/projects/commit-lint/src/lib/validators/nx-tags.validator.ts +++ b/projects/commit-lint/src/lib/validators/nx-tags.validator.ts @@ -1,5 +1,7 @@ import { Linter } from '../resources/enums/linter.enum'; import { getConfig } from '../utils/get-config.util'; +import { readFileSync } from 'fs'; +import { glob } from 'glob'; const tagsToIgnore = getConfig().nxTagsValidator.ignore; @@ -22,11 +24,13 @@ function isReservedTag(tag: string): boolean { return tagsToIgnore.indexOf(tag) !== -1; } -function parseNxJson(nxJson: any): string[] { +function parseNxJson(): string[] { const declaredTags: string[] = []; + glob.sync('**/project.json', { ignore: '**/node_modules/**' }).map((file) => { + const content = readFileSync(file, 'utf-8'); + const projectJson = JSON.parse(content); - Object.keys(nxJson.projects).forEach((key) => { - nxJson.projects[key].tags.forEach((tag: string) => { + projectJson.tags.forEach((tag: string) => { if (!isReservedTag(tag) && declaredTags.indexOf(tag) === -1) { declaredTags.push(tag); } @@ -88,12 +92,8 @@ function extractTagsFromDepConstraints(constraints: any[]): string[] { return usedTags; } -export function validateNxTags( - nxJson: any, - lintJson: any, - linter: Linter -): void { - const declaredTags = parseNxJson(nxJson); +export function validateNxTags(lintJson: any, linter: Linter): void { + const declaredTags = parseNxJson(); let usedTags: string[] = []; switch (linter) {