diff --git a/packages/angular/src/generators/component/lib/validate-options.ts b/packages/angular/src/generators/component/lib/validate-options.ts index 4b5ed70f13..01ce003201 100644 --- a/packages/angular/src/generators/component/lib/validate-options.ts +++ b/packages/angular/src/generators/component/lib/validate-options.ts @@ -1,6 +1,6 @@ import type { Tree } from '@nx/devkit'; -import { checkPathUnderProjectRoot } from '../../utils/path'; import { + validatePathIsUnderProjectRoot, validateProject, validateStandaloneOption, } from '../../utils/validations'; @@ -8,6 +8,6 @@ import type { Schema } from '../schema'; export function validateOptions(tree: Tree, options: Schema): void { validateProject(tree, options.project); - checkPathUnderProjectRoot(tree, options.project, options.path); + validatePathIsUnderProjectRoot(tree, options.project, options.path); validateStandaloneOption(tree, options.standalone); } diff --git a/packages/angular/src/generators/directive/lib/validate-options.ts b/packages/angular/src/generators/directive/lib/validate-options.ts index 4b5ed70f13..01ce003201 100644 --- a/packages/angular/src/generators/directive/lib/validate-options.ts +++ b/packages/angular/src/generators/directive/lib/validate-options.ts @@ -1,6 +1,6 @@ import type { Tree } from '@nx/devkit'; -import { checkPathUnderProjectRoot } from '../../utils/path'; import { + validatePathIsUnderProjectRoot, validateProject, validateStandaloneOption, } from '../../utils/validations'; @@ -8,6 +8,6 @@ import type { Schema } from '../schema'; export function validateOptions(tree: Tree, options: Schema): void { validateProject(tree, options.project); - checkPathUnderProjectRoot(tree, options.project, options.path); + validatePathIsUnderProjectRoot(tree, options.project, options.path); validateStandaloneOption(tree, options.standalone); } diff --git a/packages/angular/src/generators/pipe/lib/validate-options.ts b/packages/angular/src/generators/pipe/lib/validate-options.ts index 4b5ed70f13..01ce003201 100644 --- a/packages/angular/src/generators/pipe/lib/validate-options.ts +++ b/packages/angular/src/generators/pipe/lib/validate-options.ts @@ -1,6 +1,6 @@ import type { Tree } from '@nx/devkit'; -import { checkPathUnderProjectRoot } from '../../utils/path'; import { + validatePathIsUnderProjectRoot, validateProject, validateStandaloneOption, } from '../../utils/validations'; @@ -8,6 +8,6 @@ import type { Schema } from '../schema'; export function validateOptions(tree: Tree, options: Schema): void { validateProject(tree, options.project); - checkPathUnderProjectRoot(tree, options.project, options.path); + validatePathIsUnderProjectRoot(tree, options.project, options.path); validateStandaloneOption(tree, options.standalone); } diff --git a/packages/angular/src/generators/scam-directive/lib/validate-options.ts b/packages/angular/src/generators/scam-directive/lib/validate-options.ts index 81d70bbc7a..12de038dbf 100644 --- a/packages/angular/src/generators/scam-directive/lib/validate-options.ts +++ b/packages/angular/src/generators/scam-directive/lib/validate-options.ts @@ -1,9 +1,11 @@ import type { Tree } from '@nx/devkit'; -import { checkPathUnderProjectRoot } from '../../utils/path'; -import { validateProject } from '../../utils/validations'; +import { + validatePathIsUnderProjectRoot, + validateProject, +} from '../../utils/validations'; import type { Schema } from '../schema'; export function validateOptions(tree: Tree, options: Schema): void { validateProject(tree, options.project); - checkPathUnderProjectRoot(tree, options.project, options.path); + validatePathIsUnderProjectRoot(tree, options.project, options.path); } diff --git a/packages/angular/src/generators/scam-pipe/lib/validate-options.ts b/packages/angular/src/generators/scam-pipe/lib/validate-options.ts index 81d70bbc7a..12de038dbf 100644 --- a/packages/angular/src/generators/scam-pipe/lib/validate-options.ts +++ b/packages/angular/src/generators/scam-pipe/lib/validate-options.ts @@ -1,9 +1,11 @@ import type { Tree } from '@nx/devkit'; -import { checkPathUnderProjectRoot } from '../../utils/path'; -import { validateProject } from '../../utils/validations'; +import { + validatePathIsUnderProjectRoot, + validateProject, +} from '../../utils/validations'; import type { Schema } from '../schema'; export function validateOptions(tree: Tree, options: Schema): void { validateProject(tree, options.project); - checkPathUnderProjectRoot(tree, options.project, options.path); + validatePathIsUnderProjectRoot(tree, options.project, options.path); } diff --git a/packages/angular/src/generators/scam/lib/validate-options.ts b/packages/angular/src/generators/scam/lib/validate-options.ts index 81d70bbc7a..12de038dbf 100644 --- a/packages/angular/src/generators/scam/lib/validate-options.ts +++ b/packages/angular/src/generators/scam/lib/validate-options.ts @@ -1,9 +1,11 @@ import type { Tree } from '@nx/devkit'; -import { checkPathUnderProjectRoot } from '../../utils/path'; -import { validateProject } from '../../utils/validations'; +import { + validatePathIsUnderProjectRoot, + validateProject, +} from '../../utils/validations'; import type { Schema } from '../schema'; export function validateOptions(tree: Tree, options: Schema): void { validateProject(tree, options.project); - checkPathUnderProjectRoot(tree, options.project, options.path); + validatePathIsUnderProjectRoot(tree, options.project, options.path); } diff --git a/packages/angular/src/generators/utils/path.ts b/packages/angular/src/generators/utils/path.ts index 26b0544f78..a405897c3e 100644 --- a/packages/angular/src/generators/utils/path.ts +++ b/packages/angular/src/generators/utils/path.ts @@ -31,30 +31,6 @@ export function getRelativeImportToFile( )}`; } -export function checkPathUnderProjectRoot( - tree: Tree, - projectName: string, - path: string -): void { - if (!path) { - return; - } - - const { root } = readProjectConfiguration(tree, projectName); - - let pathToComponent = normalizePath(path); - pathToComponent = pathToComponent.startsWith('/') - ? pathToComponent.slice(1) - : pathToComponent; - - if (!pathStartsWith(pathToComponent, root)) { - throw new Error( - `The path provided (${path}) does not exist under the project root (${root}). ` + - `Please make sure to provide a path that exists under the project root.` - ); - } -} - export type PathGenerationOptions = { name: string; project: string; diff --git a/packages/angular/src/generators/utils/validations.ts b/packages/angular/src/generators/utils/validations.ts index 2462e06815..ed05775c80 100644 --- a/packages/angular/src/generators/utils/validations.ts +++ b/packages/angular/src/generators/utils/validations.ts @@ -1,6 +1,12 @@ import type { Tree } from '@nx/devkit'; -import { getProjects, stripIndents } from '@nx/devkit'; +import { + getProjects, + normalizePath, + readProjectConfiguration, + stripIndents, +} from '@nx/devkit'; import { lt } from 'semver'; +import { pathStartsWith } from './path'; import { getInstalledAngularVersionInfo } from './version-utils'; export function validateProject(tree: Tree, projectName: string): void { @@ -30,3 +36,27 @@ export function validateStandaloneOption( You can resolve this error by removing the "standalone" option or by migrating to Angular 14.1.0.`); } } + +export function validatePathIsUnderProjectRoot( + tree: Tree, + projectName: string, + path: string +): void { + if (!path) { + return; + } + + const { root } = readProjectConfiguration(tree, projectName); + + let pathToComponent = normalizePath(path); + pathToComponent = pathToComponent.startsWith('/') + ? pathToComponent.slice(1) + : pathToComponent; + + if (!pathStartsWith(pathToComponent, root)) { + throw new Error( + `The path provided (${path}) does not exist under the project root (${root}). ` + + `Please make sure to provide a path that exists under the project root.` + ); + } +}