cleanup(angular): update component generator to be more consistent with similar generators (#16078)
This commit is contained in:
parent
e71d0156b1
commit
29f5326b80
@ -52,7 +52,7 @@
|
|||||||
"alias": "t"
|
"alias": "t"
|
||||||
},
|
},
|
||||||
"standalone": {
|
"standalone": {
|
||||||
"description": "Whether the generated component is standalone. _Note: This is only supported in Angular versions >= 14.1.0_",
|
"description": "Whether the generated component is standalone. _Note: This is only supported in Angular versions >= 14.1.0_.",
|
||||||
"type": "boolean",
|
"type": "boolean",
|
||||||
"default": false,
|
"default": false,
|
||||||
"x-priority": "important"
|
"x-priority": "important"
|
||||||
|
|||||||
@ -1,11 +1,12 @@
|
|||||||
import { NormalizedSchema } from './normalized-schema';
|
import type { Tree } from '@nrwl/devkit';
|
||||||
import type { ProjectConfiguration, Tree } from '@nrwl/devkit';
|
|
||||||
import { addProjectConfiguration } from '@nrwl/devkit';
|
import { addProjectConfiguration } from '@nrwl/devkit';
|
||||||
|
import type { AngularProjectConfiguration } from '../../../utils/types';
|
||||||
import { getInstalledAngularVersionInfo } from '../../utils/version-utils';
|
import { getInstalledAngularVersionInfo } from '../../utils/version-utils';
|
||||||
|
import type { NormalizedSchema } from './normalized-schema';
|
||||||
|
|
||||||
export function createProject(tree: Tree, options: NormalizedSchema) {
|
export function createProject(tree: Tree, options: NormalizedSchema) {
|
||||||
const installedAngularInfo = getInstalledAngularVersionInfo(tree);
|
const installedAngularInfo = getInstalledAngularVersionInfo(tree);
|
||||||
const project: ProjectConfiguration & { prefix: string } = {
|
const project: AngularProjectConfiguration = {
|
||||||
name: options.name,
|
name: options.name,
|
||||||
projectType: 'application',
|
projectType: 'application',
|
||||||
prefix: options.prefix,
|
prefix: options.prefix,
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
import componentGenerator from './component';
|
|
||||||
import { warnForSchematicUsage } from '../utils/warn-for-schematic-usage';
|
|
||||||
import { convertNxGenerator } from '@nrwl/devkit';
|
import { convertNxGenerator } from '@nrwl/devkit';
|
||||||
|
import { warnForSchematicUsage } from '../utils/warn-for-schematic-usage';
|
||||||
|
import { componentGenerator } from './component';
|
||||||
|
|
||||||
export default warnForSchematicUsage(convertNxGenerator(componentGenerator));
|
export default warnForSchematicUsage(convertNxGenerator(componentGenerator));
|
||||||
|
|||||||
@ -5,7 +5,7 @@ import {
|
|||||||
writeJson,
|
writeJson,
|
||||||
} from '@nrwl/devkit';
|
} from '@nrwl/devkit';
|
||||||
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing';
|
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing';
|
||||||
import componentGenerator from './component';
|
import { componentGenerator } from './component';
|
||||||
|
|
||||||
describe('component Generator', () => {
|
describe('component Generator', () => {
|
||||||
it('should create the component correctly and export it in the entry point when "export=true"', async () => {
|
it('should create the component correctly and export it in the entry point when "export=true"', async () => {
|
||||||
@ -686,7 +686,7 @@ describe('component Generator', () => {
|
|||||||
standalone: true,
|
standalone: true,
|
||||||
})
|
})
|
||||||
).rejects
|
).rejects
|
||||||
.toThrow(stripIndents`The "standalone" option is only supported in Angular >= 14.1.0. You are currently using 14.0.0.
|
.toThrow(stripIndents`The "standalone" option is only supported in Angular >= 14.1.0. You are currently using "14.0.0".
|
||||||
You can resolve this error by removing the "standalone" option or by migrating to Angular 14.1.0.`);
|
You can resolve this error by removing the "standalone" option or by migrating to Angular 14.1.0.`);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@ -4,32 +4,19 @@ import {
|
|||||||
generateFiles,
|
generateFiles,
|
||||||
joinPathFragments,
|
joinPathFragments,
|
||||||
names,
|
names,
|
||||||
readNxJson,
|
|
||||||
stripIndents,
|
|
||||||
} from '@nrwl/devkit';
|
} from '@nrwl/devkit';
|
||||||
import { lt } from 'semver';
|
|
||||||
import { checkPathUnderProjectRoot } from '../utils/path';
|
|
||||||
import { getInstalledAngularVersionInfo } from '../utils/version-utils';
|
|
||||||
import { exportComponentInEntryPoint } from './lib/component';
|
|
||||||
import { normalizeOptions } from './lib/normalize-options';
|
|
||||||
import type { Schema } from './schema';
|
|
||||||
import { addToNgModule } from '../utils';
|
import { addToNgModule } from '../utils';
|
||||||
import { findModuleFromOptions } from './lib/module';
|
import {
|
||||||
|
exportComponentInEntryPoint,
|
||||||
|
findModuleFromOptions,
|
||||||
|
normalizeOptions,
|
||||||
|
validateOptions,
|
||||||
|
} from './lib';
|
||||||
|
import type { Schema } from './schema';
|
||||||
|
|
||||||
export async function componentGenerator(tree: Tree, rawOptions: Schema) {
|
export async function componentGenerator(tree: Tree, rawOptions: Schema) {
|
||||||
const installedAngularVersionInfo = getInstalledAngularVersionInfo(tree);
|
validateOptions(tree, rawOptions);
|
||||||
|
const options = normalizeOptions(tree, rawOptions);
|
||||||
if (
|
|
||||||
lt(installedAngularVersionInfo.version, '14.1.0') &&
|
|
||||||
rawOptions.standalone
|
|
||||||
) {
|
|
||||||
throw new Error(stripIndents`The "standalone" option is only supported in Angular >= 14.1.0. You are currently using ${installedAngularVersionInfo.version}.
|
|
||||||
You can resolve this error by removing the "standalone" option or by migrating to Angular 14.1.0.`);
|
|
||||||
}
|
|
||||||
|
|
||||||
const options = await normalizeOptions(tree, rawOptions);
|
|
||||||
|
|
||||||
checkPathUnderProjectRoot(tree, options.project, options.path);
|
|
||||||
|
|
||||||
const pathToGenerate = options.flat
|
const pathToGenerate = options.flat
|
||||||
? joinPathFragments(__dirname, './files/__fileName__')
|
? joinPathFragments(__dirname, './files/__fileName__')
|
||||||
@ -38,10 +25,6 @@ export async function componentGenerator(tree: Tree, rawOptions: Schema) {
|
|||||||
const componentNames = names(options.name);
|
const componentNames = names(options.name);
|
||||||
const typeNames = names(options.type);
|
const typeNames = names(options.type);
|
||||||
|
|
||||||
const selector =
|
|
||||||
options.selector ||
|
|
||||||
buildSelector(tree, componentNames.fileName, options.prefix);
|
|
||||||
|
|
||||||
generateFiles(tree, pathToGenerate, options.path, {
|
generateFiles(tree, pathToGenerate, options.path, {
|
||||||
fileName: componentNames.fileName,
|
fileName: componentNames.fileName,
|
||||||
className: componentNames.className,
|
className: componentNames.className,
|
||||||
@ -55,7 +38,7 @@ export async function componentGenerator(tree: Tree, rawOptions: Schema) {
|
|||||||
changeDetection: options.changeDetection,
|
changeDetection: options.changeDetection,
|
||||||
viewEncapsulation: options.viewEncapsulation,
|
viewEncapsulation: options.viewEncapsulation,
|
||||||
displayBlock: options.displayBlock,
|
displayBlock: options.displayBlock,
|
||||||
selector,
|
selector: options.selector,
|
||||||
tpl: '',
|
tpl: '',
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -123,12 +106,4 @@ export async function componentGenerator(tree: Tree, rawOptions: Schema) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function buildSelector(tree: Tree, name: string, prefix: string) {
|
|
||||||
const selectorPrefix = names(
|
|
||||||
prefix ?? readNxJson(tree).npmScope ?? 'app'
|
|
||||||
).fileName;
|
|
||||||
|
|
||||||
return names(`${selectorPrefix}-${name}`).fileName;
|
|
||||||
}
|
|
||||||
|
|
||||||
export default componentGenerator;
|
export default componentGenerator;
|
||||||
|
|||||||
@ -1,10 +1,11 @@
|
|||||||
import type { Tree } from '@nrwl/devkit';
|
import type { Tree } from '@nrwl/devkit';
|
||||||
import { logger, readProjectConfiguration, stripIndents } from '@nrwl/devkit';
|
import { logger, readProjectConfiguration, stripIndents } from '@nrwl/devkit';
|
||||||
import { getComponentFileInfo } from '../../utils/file-info';
|
import { ensureTypescript } from '@nrwl/js/src/utils/typescript/ensure-typescript';
|
||||||
|
import type { StringLiteral } from 'typescript';
|
||||||
import { locateLibraryEntryPointFromDirectory } from '../../utils/entry-point';
|
import { locateLibraryEntryPointFromDirectory } from '../../utils/entry-point';
|
||||||
|
import { getComponentFileInfo } from '../../utils/file-info';
|
||||||
import { getRelativeImportToFile } from '../../utils/path';
|
import { getRelativeImportToFile } from '../../utils/path';
|
||||||
import type { NormalizedSchema } from '../schema';
|
import type { NormalizedSchema } from '../schema';
|
||||||
import { shouldExportInEntryPoint } from './entry-point';
|
|
||||||
import { findModuleFromOptions } from './module';
|
import { findModuleFromOptions } from './module';
|
||||||
|
|
||||||
export function exportComponentInEntryPoint(
|
export function exportComponentInEntryPoint(
|
||||||
@ -57,3 +58,26 @@ export function exportComponentInEntryPoint(
|
|||||||
|
|
||||||
tree.write(entryPointPath, updateEntryPointContent);
|
tree.write(entryPointPath, updateEntryPointContent);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function shouldExportInEntryPoint(
|
||||||
|
tree: Tree,
|
||||||
|
entryPoint: string,
|
||||||
|
modulePath: string
|
||||||
|
): boolean {
|
||||||
|
if (!modulePath) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
ensureTypescript();
|
||||||
|
const { tsquery } = require('@phenomnomnominal/tsquery');
|
||||||
|
const moduleImportPath = getRelativeImportToFile(entryPoint, modulePath);
|
||||||
|
const entryPointContent = tree.read(entryPoint, 'utf-8');
|
||||||
|
const entryPointAst = tsquery.ast(entryPointContent);
|
||||||
|
const moduleExport = tsquery(
|
||||||
|
entryPointAst,
|
||||||
|
`ExportDeclaration StringLiteral[value='${moduleImportPath}']`,
|
||||||
|
{ visitAllChildren: true }
|
||||||
|
)[0] as StringLiteral;
|
||||||
|
|
||||||
|
return Boolean(moduleExport);
|
||||||
|
}
|
||||||
|
|||||||
@ -1,27 +0,0 @@
|
|||||||
import type { Tree } from '@nrwl/devkit';
|
|
||||||
import type { StringLiteral } from 'typescript';
|
|
||||||
import { getRelativeImportToFile } from '../../utils/path';
|
|
||||||
import { ensureTypescript } from '@nrwl/js/src/utils/typescript/ensure-typescript';
|
|
||||||
|
|
||||||
export function shouldExportInEntryPoint(
|
|
||||||
tree: Tree,
|
|
||||||
entryPoint: string,
|
|
||||||
modulePath: string
|
|
||||||
): boolean {
|
|
||||||
if (!modulePath) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
ensureTypescript();
|
|
||||||
const { tsquery } = require('@phenomnomnominal/tsquery');
|
|
||||||
const moduleImportPath = getRelativeImportToFile(entryPoint, modulePath);
|
|
||||||
const entryPointContent = tree.read(entryPoint, 'utf-8');
|
|
||||||
const entryPointAst = tsquery.ast(entryPointContent);
|
|
||||||
const moduleExport = tsquery(
|
|
||||||
entryPointAst,
|
|
||||||
`ExportDeclaration StringLiteral[value='${moduleImportPath}']`,
|
|
||||||
{ visitAllChildren: true }
|
|
||||||
)[0] as StringLiteral;
|
|
||||||
|
|
||||||
return Boolean(moduleExport);
|
|
||||||
}
|
|
||||||
4
packages/angular/src/generators/component/lib/index.ts
Normal file
4
packages/angular/src/generators/component/lib/index.ts
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
export * from './component';
|
||||||
|
export * from './module';
|
||||||
|
export * from './normalize-options';
|
||||||
|
export * from './validate-options';
|
||||||
@ -1,29 +1,34 @@
|
|||||||
import type { Tree } from '@nrwl/devkit';
|
import type { Tree } from '@nrwl/devkit';
|
||||||
import { joinPathFragments, readProjectConfiguration } from '@nrwl/devkit';
|
import { joinPathFragments, readProjectConfiguration } from '@nrwl/devkit';
|
||||||
|
import type { AngularProjectConfiguration } from '../../../utils/types';
|
||||||
|
import { parseNameWithPath } from '../../utils/names';
|
||||||
|
import { buildSelector } from '../../utils/selector';
|
||||||
import type { NormalizedSchema, Schema } from '../schema';
|
import type { NormalizedSchema, Schema } from '../schema';
|
||||||
|
|
||||||
export async function normalizeOptions(
|
export function normalizeOptions(
|
||||||
tree: Tree,
|
tree: Tree,
|
||||||
options: Schema
|
options: Schema
|
||||||
): Promise<NormalizedSchema> {
|
): NormalizedSchema {
|
||||||
const { projectType, root, sourceRoot } = readProjectConfiguration(
|
const { prefix, projectType, root, sourceRoot } = readProjectConfiguration(
|
||||||
tree,
|
tree,
|
||||||
options.project
|
options.project
|
||||||
);
|
) as AngularProjectConfiguration;
|
||||||
const projectSourceRoot = sourceRoot ?? joinPathFragments(root, 'src');
|
|
||||||
|
|
||||||
const parsedName = options.name.split('/');
|
const projectSourceRoot = sourceRoot ?? joinPathFragments(root, 'src');
|
||||||
const name = parsedName.pop();
|
const { name, path: namePath } = parseNameWithPath(options.name);
|
||||||
const namedPath = parsedName.join('/');
|
|
||||||
|
|
||||||
const path =
|
const path =
|
||||||
options.path ??
|
options.path ??
|
||||||
joinPathFragments(
|
joinPathFragments(
|
||||||
projectSourceRoot,
|
projectSourceRoot,
|
||||||
projectType === 'application' ? 'app' : 'lib',
|
projectType === 'application' ? 'app' : 'lib',
|
||||||
namedPath
|
namePath
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const selector =
|
||||||
|
options.selector ??
|
||||||
|
buildSelector(tree, name, options.prefix, prefix, 'fileName');
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...options,
|
...options,
|
||||||
name,
|
name,
|
||||||
@ -33,5 +38,6 @@ export async function normalizeOptions(
|
|||||||
path,
|
path,
|
||||||
projectSourceRoot,
|
projectSourceRoot,
|
||||||
projectRoot: root,
|
projectRoot: root,
|
||||||
|
selector,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,13 @@
|
|||||||
|
import type { Tree } from '@nrwl/devkit';
|
||||||
|
import { checkPathUnderProjectRoot } from '../../utils/path';
|
||||||
|
import {
|
||||||
|
validateProject,
|
||||||
|
validateStandaloneOption,
|
||||||
|
} 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);
|
||||||
|
validateStandaloneOption(tree, options.standalone);
|
||||||
|
}
|
||||||
@ -25,4 +25,5 @@ export interface NormalizedSchema extends Schema {
|
|||||||
path: string;
|
path: string;
|
||||||
projectSourceRoot: string;
|
projectSourceRoot: string;
|
||||||
projectRoot: string;
|
projectRoot: string;
|
||||||
|
selector: string;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -54,7 +54,7 @@
|
|||||||
"alias": "t"
|
"alias": "t"
|
||||||
},
|
},
|
||||||
"standalone": {
|
"standalone": {
|
||||||
"description": "Whether the generated component is standalone. _Note: This is only supported in Angular versions >= 14.1.0_",
|
"description": "Whether the generated component is standalone. _Note: This is only supported in Angular versions >= 14.1.0_.",
|
||||||
"type": "boolean",
|
"type": "boolean",
|
||||||
"default": false,
|
"default": false,
|
||||||
"x-priority": "important"
|
"x-priority": "important"
|
||||||
|
|||||||
@ -6,10 +6,11 @@ import {
|
|||||||
logger,
|
logger,
|
||||||
Tree,
|
Tree,
|
||||||
} from '@nrwl/devkit';
|
} from '@nrwl/devkit';
|
||||||
import { warnForSchematicUsage } from '../utils/warn-for-schematic-usage';
|
|
||||||
import { ConvertTSLintToESLintSchema, ProjectConverter } from '@nrwl/linter';
|
import { ConvertTSLintToESLintSchema, ProjectConverter } from '@nrwl/linter';
|
||||||
import type { Linter } from 'eslint';
|
import type { Linter } from 'eslint';
|
||||||
|
import type { AngularProjectConfiguration } from '../../utils/types';
|
||||||
import { addLintingGenerator } from '../add-linting/add-linting';
|
import { addLintingGenerator } from '../add-linting/add-linting';
|
||||||
|
import { warnForSchematicUsage } from '../utils/warn-for-schematic-usage';
|
||||||
|
|
||||||
export async function conversionGenerator(
|
export async function conversionGenerator(
|
||||||
host: Tree,
|
host: Tree,
|
||||||
@ -33,7 +34,7 @@ export async function conversionGenerator(
|
|||||||
await addLintingGenerator(host, {
|
await addLintingGenerator(host, {
|
||||||
projectName,
|
projectName,
|
||||||
projectRoot: projectConfig.root,
|
projectRoot: projectConfig.root,
|
||||||
prefix: (projectConfig as any).prefix || 'app',
|
prefix: (projectConfig as AngularProjectConfiguration).prefix || 'app',
|
||||||
/**
|
/**
|
||||||
* We set the parserOptions.project config just in case the converted config uses
|
* We set the parserOptions.project config just in case the converted config uses
|
||||||
* rules which require type-checking. Later in the conversion we check if it actually
|
* rules which require type-checking. Later in the conversion we check if it actually
|
||||||
|
|||||||
@ -1,21 +1,18 @@
|
|||||||
import type { ProjectConfiguration, Tree } from '@nrwl/devkit';
|
import type { Tree } from '@nrwl/devkit';
|
||||||
import {
|
import { joinPathFragments, readProjectConfiguration } from '@nrwl/devkit';
|
||||||
joinPathFragments,
|
import type { AngularProjectConfiguration } from '../../../utils/types';
|
||||||
names,
|
import { parseNameWithPath } from '../../utils/names';
|
||||||
readNxJson,
|
import { buildSelector } from '../../utils/selector';
|
||||||
readProjectConfiguration,
|
|
||||||
} from '@nrwl/devkit';
|
|
||||||
import { parseName } from '../../utils/names';
|
|
||||||
import type { Schema } from '../schema';
|
import type { Schema } from '../schema';
|
||||||
|
|
||||||
export function normalizeOptions(tree: Tree, options: Schema) {
|
export function normalizeOptions(tree: Tree, options: Schema) {
|
||||||
const { prefix, projectType, root, sourceRoot } = readProjectConfiguration(
|
const { prefix, projectType, root, sourceRoot } = readProjectConfiguration(
|
||||||
tree,
|
tree,
|
||||||
options.project
|
options.project
|
||||||
) as ProjectConfiguration & { prefix?: string };
|
) as AngularProjectConfiguration;
|
||||||
|
|
||||||
const projectSourceRoot = sourceRoot ?? joinPathFragments(root, 'src');
|
const projectSourceRoot = sourceRoot ?? joinPathFragments(root, 'src');
|
||||||
const { name, path: namePath } = parseName(options.name);
|
const { name, path: namePath } = parseNameWithPath(options.name);
|
||||||
|
|
||||||
const path =
|
const path =
|
||||||
options.path ??
|
options.path ??
|
||||||
@ -26,7 +23,8 @@ export function normalizeOptions(tree: Tree, options: Schema) {
|
|||||||
);
|
);
|
||||||
|
|
||||||
const selector =
|
const selector =
|
||||||
options.selector ?? buildSelector(tree, name, options.prefix, prefix);
|
options.selector ??
|
||||||
|
buildSelector(tree, name, options.prefix, prefix, 'propertyName');
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...options,
|
...options,
|
||||||
@ -37,18 +35,3 @@ export function normalizeOptions(tree: Tree, options: Schema) {
|
|||||||
selector,
|
selector,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function buildSelector(
|
|
||||||
tree: Tree,
|
|
||||||
name: string,
|
|
||||||
prefix: string,
|
|
||||||
projectPrefix: string
|
|
||||||
): string {
|
|
||||||
let selector = name;
|
|
||||||
prefix ??= projectPrefix ?? readNxJson(tree).npmScope;
|
|
||||||
if (prefix) {
|
|
||||||
selector = `${prefix}-${selector}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
return names(selector).propertyName;
|
|
||||||
}
|
|
||||||
|
|||||||
@ -1,21 +1,13 @@
|
|||||||
import type { Tree } from '@nrwl/devkit';
|
import type { Tree } from '@nrwl/devkit';
|
||||||
import { getProjects, stripIndents } from '@nrwl/devkit';
|
|
||||||
import { lt } from 'semver';
|
|
||||||
import { checkPathUnderProjectRoot } from '../../utils/path';
|
import { checkPathUnderProjectRoot } from '../../utils/path';
|
||||||
import { getInstalledAngularVersionInfo } from '../../utils/version-utils';
|
import {
|
||||||
|
validateProject,
|
||||||
|
validateStandaloneOption,
|
||||||
|
} from '../../utils/validations';
|
||||||
import type { Schema } from '../schema';
|
import type { Schema } from '../schema';
|
||||||
|
|
||||||
export function validateOptions(tree: Tree, options: Schema): void {
|
export function validateOptions(tree: Tree, options: Schema): void {
|
||||||
const projects = getProjects(tree);
|
validateProject(tree, options.project);
|
||||||
if (!projects.has(options.project)) {
|
|
||||||
throw new Error(`Project "${options.project}" does not exist!`);
|
|
||||||
}
|
|
||||||
|
|
||||||
checkPathUnderProjectRoot(tree, options.project, options.path);
|
checkPathUnderProjectRoot(tree, options.project, options.path);
|
||||||
|
validateStandaloneOption(tree, options.standalone);
|
||||||
const installedAngularVersionInfo = getInstalledAngularVersionInfo(tree);
|
|
||||||
if (lt(installedAngularVersionInfo.version, '14.1.0') && options.standalone) {
|
|
||||||
throw new Error(stripIndents`The "standalone" option is only supported in Angular >= 14.1.0. You are currently using "${installedAngularVersionInfo.version}".
|
|
||||||
You can resolve this error by removing the "standalone" option or by migrating to Angular 14.1.0.`);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,12 +1,13 @@
|
|||||||
import type { ProjectConfiguration, Tree } from '@nrwl/devkit';
|
import type { Tree } from '@nrwl/devkit';
|
||||||
import { addProjectConfiguration, joinPathFragments } from '@nrwl/devkit';
|
import { addProjectConfiguration, joinPathFragments } from '@nrwl/devkit';
|
||||||
import { NormalizedSchema } from './normalized-schema';
|
import type { AngularProjectConfiguration } from '../../../utils/types';
|
||||||
|
import type { NormalizedSchema } from './normalized-schema';
|
||||||
|
|
||||||
export function addProject(
|
export function addProject(
|
||||||
tree: Tree,
|
tree: Tree,
|
||||||
libraryOptions: NormalizedSchema['libraryOptions']
|
libraryOptions: NormalizedSchema['libraryOptions']
|
||||||
) {
|
) {
|
||||||
const project: ProjectConfiguration & { prefix: string } = {
|
const project: AngularProjectConfiguration = {
|
||||||
name: libraryOptions.name,
|
name: libraryOptions.name,
|
||||||
root: libraryOptions.projectRoot,
|
root: libraryOptions.projectRoot,
|
||||||
sourceRoot: joinPathFragments(libraryOptions.projectRoot, 'src'),
|
sourceRoot: joinPathFragments(libraryOptions.projectRoot, 'src'),
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
import type { ProjectConfiguration, Tree } from '@nrwl/devkit';
|
import type { Tree } from '@nrwl/devkit';
|
||||||
import {
|
import {
|
||||||
generateFiles,
|
generateFiles,
|
||||||
getWorkspaceLayout,
|
getWorkspaceLayout,
|
||||||
@ -6,14 +6,15 @@ import {
|
|||||||
names,
|
names,
|
||||||
offsetFromRoot,
|
offsetFromRoot,
|
||||||
} from '@nrwl/devkit';
|
} from '@nrwl/devkit';
|
||||||
import { NormalizedSchema } from './normalized-schema';
|
|
||||||
import { UnitTestRunner } from '../../../utils/test-runners';
|
|
||||||
import { getRootTsConfigFileName } from '@nrwl/js';
|
import { getRootTsConfigFileName } from '@nrwl/js';
|
||||||
|
import { UnitTestRunner } from '../../../utils/test-runners';
|
||||||
|
import type { AngularProjectConfiguration } from '../../../utils/types';
|
||||||
|
import type { NormalizedSchema } from './normalized-schema';
|
||||||
|
|
||||||
export function createFiles(
|
export function createFiles(
|
||||||
tree: Tree,
|
tree: Tree,
|
||||||
options: NormalizedSchema,
|
options: NormalizedSchema,
|
||||||
project: ProjectConfiguration & { prefix: string }
|
project: AngularProjectConfiguration
|
||||||
) {
|
) {
|
||||||
const { npmScope } = getWorkspaceLayout(tree);
|
const { npmScope } = getWorkspaceLayout(tree);
|
||||||
const rootOffset = offsetFromRoot(options.libraryOptions.projectRoot);
|
const rootOffset = offsetFromRoot(options.libraryOptions.projectRoot);
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
import type { ProjectConfiguration, Tree } from '@nrwl/devkit';
|
import type { Tree } from '@nrwl/devkit';
|
||||||
import {
|
import {
|
||||||
formatFiles,
|
formatFiles,
|
||||||
generateFiles,
|
generateFiles,
|
||||||
@ -7,9 +7,10 @@ import {
|
|||||||
names,
|
names,
|
||||||
readProjectConfiguration,
|
readProjectConfiguration,
|
||||||
} from '@nrwl/devkit';
|
} from '@nrwl/devkit';
|
||||||
import type { Schema } from './schema';
|
import type { AngularProjectConfiguration } from '../../utils/types';
|
||||||
import { checkPathUnderProjectRoot } from '../utils/path';
|
|
||||||
import { addToNgModule, findModule } from '../utils';
|
import { addToNgModule, findModule } from '../utils';
|
||||||
|
import { checkPathUnderProjectRoot } from '../utils/path';
|
||||||
|
import type { Schema } from './schema';
|
||||||
|
|
||||||
let tsModule: typeof import('typescript');
|
let tsModule: typeof import('typescript');
|
||||||
|
|
||||||
@ -24,7 +25,7 @@ export async function pipeGenerator(tree: Tree, schema: Schema) {
|
|||||||
const project = readProjectConfiguration(
|
const project = readProjectConfiguration(
|
||||||
tree,
|
tree,
|
||||||
schema.project
|
schema.project
|
||||||
) as ProjectConfiguration & { prefix?: string };
|
) as AngularProjectConfiguration;
|
||||||
|
|
||||||
const path = schema.path ?? `${project.sourceRoot}`;
|
const path = schema.path ?? `${project.sourceRoot}`;
|
||||||
const pipeNames = names(schema.name);
|
const pipeNames = names(schema.name);
|
||||||
|
|||||||
@ -1,25 +1,23 @@
|
|||||||
import type { ProjectConfiguration, Tree } from '@nrwl/devkit';
|
import type { Tree } from '@nrwl/devkit';
|
||||||
import {
|
import {
|
||||||
generateFiles,
|
generateFiles,
|
||||||
joinPathFragments,
|
joinPathFragments,
|
||||||
readNxJson,
|
readNxJson,
|
||||||
readProjectConfiguration,
|
readProjectConfiguration,
|
||||||
} from '@nrwl/devkit';
|
} from '@nrwl/devkit';
|
||||||
import type { Schema } from '../schema';
|
|
||||||
import { addRoute } from '../../../utils/nx-devkit/route-utils';
|
import { addRoute } from '../../../utils/nx-devkit/route-utils';
|
||||||
|
import type { AngularProjectConfiguration } from '../../../utils/types';
|
||||||
|
import type { Schema } from '../schema';
|
||||||
|
|
||||||
export function addRemoteEntry(
|
export function addRemoteEntry(
|
||||||
tree: Tree,
|
tree: Tree,
|
||||||
{ appName, routing, mfType, prefix, standalone }: Schema,
|
{ appName, routing, prefix, standalone }: Schema,
|
||||||
appRoot: string
|
appRoot: string
|
||||||
) {
|
) {
|
||||||
prefix =
|
prefix =
|
||||||
prefix ??
|
prefix ??
|
||||||
(
|
(readProjectConfiguration(tree, appName) as AngularProjectConfiguration)
|
||||||
readProjectConfiguration(tree, appName) as ProjectConfiguration & {
|
?.prefix ??
|
||||||
prefix?: string;
|
|
||||||
}
|
|
||||||
)?.prefix ??
|
|
||||||
readNxJson(tree).npmScope;
|
readNxJson(tree).npmScope;
|
||||||
generateFiles(
|
generateFiles(
|
||||||
tree,
|
tree,
|
||||||
|
|||||||
@ -2,7 +2,7 @@ import { normalizePath } from '@nrwl/devkit';
|
|||||||
|
|
||||||
export type NameInfo = { name: string; path: string };
|
export type NameInfo = { name: string; path: string };
|
||||||
|
|
||||||
export function parseName(rawName: string): NameInfo {
|
export function parseNameWithPath(rawName: string): NameInfo {
|
||||||
const parsedName = normalizePath(rawName).split('/');
|
const parsedName = normalizePath(rawName).split('/');
|
||||||
const name = parsedName.pop();
|
const name = parsedName.pop();
|
||||||
const path = parsedName.join('/');
|
const path = parsedName.join('/');
|
||||||
|
|||||||
18
packages/angular/src/generators/utils/selector.ts
Normal file
18
packages/angular/src/generators/utils/selector.ts
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
import type { Tree } from '@nrwl/devkit';
|
||||||
|
import { names, readNxJson } from '@nrwl/devkit';
|
||||||
|
|
||||||
|
export function buildSelector(
|
||||||
|
tree: Tree,
|
||||||
|
name: string,
|
||||||
|
prefix: string | undefined,
|
||||||
|
projectPrefix: string | undefined,
|
||||||
|
casing: keyof Pick<ReturnType<typeof names>, 'fileName' | 'propertyName'>
|
||||||
|
): string {
|
||||||
|
let selector = name;
|
||||||
|
prefix ??= projectPrefix ?? readNxJson(tree)?.npmScope;
|
||||||
|
if (prefix) {
|
||||||
|
selector = `${prefix}-${selector}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
return names(selector)[casing];
|
||||||
|
}
|
||||||
32
packages/angular/src/generators/utils/validations.ts
Normal file
32
packages/angular/src/generators/utils/validations.ts
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
import type { Tree } from '@nrwl/devkit';
|
||||||
|
import { getProjects, stripIndents } from '@nrwl/devkit';
|
||||||
|
import { lt } from 'semver';
|
||||||
|
import { getInstalledAngularVersionInfo } from './version-utils';
|
||||||
|
|
||||||
|
export function validateProject(tree: Tree, projectName: string): void {
|
||||||
|
const projects = getProjects(tree);
|
||||||
|
|
||||||
|
if (!projects.has(projectName)) {
|
||||||
|
throw new Error(
|
||||||
|
`Project "${projectName}" does not exist! Please provide an existing project name.`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function validateStandaloneOption(
|
||||||
|
tree: Tree,
|
||||||
|
standalone: boolean | undefined,
|
||||||
|
angularVersion?: string
|
||||||
|
): void {
|
||||||
|
if (!standalone) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const installedAngularVersion =
|
||||||
|
angularVersion ?? getInstalledAngularVersionInfo(tree).version;
|
||||||
|
|
||||||
|
if (lt(installedAngularVersion, '14.1.0')) {
|
||||||
|
throw new Error(stripIndents`The "standalone" option is only supported in Angular >= 14.1.0. You are currently using "${installedAngularVersion}".
|
||||||
|
You can resolve this error by removing the "standalone" option or by migrating to Angular 14.1.0.`);
|
||||||
|
}
|
||||||
|
}
|
||||||
5
packages/angular/src/utils/types.ts
Normal file
5
packages/angular/src/utils/types.ts
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
import type { ProjectConfiguration } from '@nrwl/devkit';
|
||||||
|
|
||||||
|
export type AngularProjectConfiguration = ProjectConfiguration & {
|
||||||
|
prefix?: string;
|
||||||
|
};
|
||||||
Loading…
x
Reference in New Issue
Block a user