cleanup(angular): rearrange validation helper function (#16906)

This commit is contained in:
Leosvel Pérez Espinosa 2023-05-10 12:55:51 +01:00 committed by GitHub
parent bdfb10db0a
commit caefe8f509
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 52 additions and 40 deletions

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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;

View File

@ -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.`
);
}
}