diff --git a/packages/angular/src/generators/add-linting/add-linting.ts b/packages/angular/src/generators/add-linting/add-linting.ts index 697155cdc5729..8fe088b4e99f4 100755 --- a/packages/angular/src/generators/add-linting/add-linting.ts +++ b/packages/angular/src/generators/add-linting/add-linting.ts @@ -1,24 +1,24 @@ import { formatFiles, - GeneratorCallback, joinPathFragments, - readProjectConfiguration, runTasksInSerial, - Tree, + type GeneratorCallback, + type Tree, } from '@nx/devkit'; +import { camelize, dasherize } from '@nx/devkit/src/utils/string-utils'; import { Linter, lintProjectGenerator } from '@nx/eslint'; -import { addAngularEsLintDependencies } from './lib/add-angular-eslint-dependencies'; -import type { AddLintingGeneratorSchema } from './schema'; +import { + javaScriptOverride, + typeScriptOverride, +} from '@nx/eslint/src/generators/init/global-eslint-config'; import { findEslintFile, isEslintConfigSupported, replaceOverridesInLintConfig, } from '@nx/eslint/src/generators/utils/eslint-file'; -import { camelize, dasherize } from '@nx/devkit/src/utils/string-utils'; -import { - javaScriptOverride, - typeScriptOverride, -} from '@nx/eslint/src/generators/init/global-eslint-config'; +import { addAngularEsLintDependencies } from './lib/add-angular-eslint-dependencies'; +import { isBuildableLibraryProject } from './lib/buildable-project'; +import type { AddLintingGeneratorSchema } from './schema'; export async function addLintingGenerator( tree: Tree, @@ -106,7 +106,7 @@ export async function addLintingGenerator( } if (!options.skipPackageJson) { - const installTask = addAngularEsLintDependencies(tree); + const installTask = addAngularEsLintDependencies(tree, options.projectName); tasks.push(installTask); } @@ -117,13 +117,4 @@ export async function addLintingGenerator( return runTasksInSerial(...tasks); } -function isBuildableLibraryProject(tree: Tree, projectName: string): boolean { - const projectConfig = readProjectConfiguration(tree, projectName); - return ( - projectConfig.projectType === 'library' && - projectConfig.targets?.build && - !!projectConfig.targets.build - ); -} - export default addLintingGenerator; diff --git a/packages/angular/src/generators/add-linting/lib/add-angular-eslint-dependencies.ts b/packages/angular/src/generators/add-linting/lib/add-angular-eslint-dependencies.ts index db305e1bce82c..08a4d46c413d5 100644 --- a/packages/angular/src/generators/add-linting/lib/add-angular-eslint-dependencies.ts +++ b/packages/angular/src/generators/add-linting/lib/add-angular-eslint-dependencies.ts @@ -1,19 +1,27 @@ -import type { GeneratorCallback, Tree } from '@nx/devkit'; -import { addDependenciesToPackageJson } from '@nx/devkit'; +import { + addDependenciesToPackageJson, + type GeneratorCallback, + type Tree, +} from '@nx/devkit'; import { versions } from '../../utils/version-utils'; +import { isBuildableLibraryProject } from './buildable-project'; -export function addAngularEsLintDependencies(tree: Tree): GeneratorCallback { +export function addAngularEsLintDependencies( + tree: Tree, + projectName: string +): GeneratorCallback { const angularEslintVersionToInstall = versions(tree).angularEslintVersion; - const jsoncEslintParserVersionToInstall = - versions(tree).jsoncEslintParserVersion; - return addDependenciesToPackageJson( - tree, - {}, - { - '@angular-eslint/eslint-plugin': angularEslintVersionToInstall, - '@angular-eslint/eslint-plugin-template': angularEslintVersionToInstall, - '@angular-eslint/template-parser': angularEslintVersionToInstall, - 'jsonc-eslint-parser': jsoncEslintParserVersionToInstall, - } - ); + const devDependencies = { + '@angular-eslint/eslint-plugin': angularEslintVersionToInstall, + '@angular-eslint/eslint-plugin-template': angularEslintVersionToInstall, + '@angular-eslint/template-parser': angularEslintVersionToInstall, + }; + + if (isBuildableLibraryProject(tree, projectName)) { + const jsoncEslintParserVersionToInstall = + versions(tree).jsoncEslintParserVersion; + devDependencies['jsonc-eslint-parser'] = jsoncEslintParserVersionToInstall; + } + + return addDependenciesToPackageJson(tree, {}, devDependencies); } diff --git a/packages/angular/src/generators/add-linting/lib/buildable-project.ts b/packages/angular/src/generators/add-linting/lib/buildable-project.ts new file mode 100644 index 0000000000000..271986324e6e4 --- /dev/null +++ b/packages/angular/src/generators/add-linting/lib/buildable-project.ts @@ -0,0 +1,12 @@ +import { readProjectConfiguration, type Tree } from '@nx/devkit'; + +export function isBuildableLibraryProject( + tree: Tree, + projectName: string +): boolean { + const projectConfig = readProjectConfiguration(tree, projectName); + + return ( + projectConfig.projectType === 'library' && !!projectConfig.targets?.build + ); +}