Skip to content

Commit fa5140a

Browse files
sergey-krasVasilyStrelyaev
authored andcommitted
Fix for CLI: ng app works incorrectly with ng 20 (#960)
* feat: added angular 20 for app * feat: small fix angular 20 for app * feat: small fix angular 20 for app * fix: reverted files * fix: small fix * fix: fix build * fix: fixed tests * fix: small fix * fix: small fix * fix: small fix * fix: revert refactoring * fix: reverted patch * fix: reverted patch
1 parent 6f1ccb7 commit fa5140a

File tree

6 files changed

+37
-9
lines changed

6 files changed

+37
-9
lines changed

packages/devextreme-cli/src/applications/application.angular.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ const bumpAngular = (appPath, versionTag) => {
8585
for(const depName in section) {
8686
section[depName] = depName.startsWith('@angular') ? versionTag : section[depName];
8787
}
88+
return section;
8889
};
8990

9091
return {

packages/devextreme-schematics/src/add-layout/files/src/app/__name__.component.html renamed to packages/devextreme-schematics/src/add-layout/files/src/app/__name__.html

File renamed without changes.

packages/devextreme-schematics/src/add-layout/files/src/app/__name__.component.scss renamed to packages/devextreme-schematics/src/add-layout/files/src/app/__name__.scss

File renamed without changes.

packages/devextreme-schematics/src/add-layout/files/src/app/__name__.component.ts renamed to packages/devextreme-schematics/src/add-layout/files/src/app/__name__.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import { UnauthenticatedContentComponent } from './unauthenticated-content';
99

1010
@Component({
1111
selector: '<%= prefix %>-root',
12-
templateUrl: './<%= name %>.component.html',
13-
styleUrls: ['./<%= name %>.component.scss'],
12+
templateUrl: './<%= name %>.html',
13+
styleUrls: ['./<%= name %>.scss'],
1414
standalone: true,
1515
imports: [
1616
RouterModule,
@@ -23,7 +23,7 @@ import { UnauthenticatedContentComponent } from './unauthenticated-content';
2323
],
2424
providers: []
2525
})
26-
export class <%= strings.classify(name) %>Component {
26+
export class <%= strings.classify(name) %> {
2727
@HostBinding('class') get getClass() {
2828
const sizeClassName = Object.keys(this.screen.sizes).filter(cl => this.screen.sizes[cl]).join(' ');
2929
return `${sizeClassName} app` ;

packages/devextreme-schematics/src/add-layout/index.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ import { getWorkspace } from '@schematics/angular/utility/workspace';
6666
import { Change } from '@schematics/angular/utility/change';
6767

6868
import { PatchNodePackageInstallTask } from '../utility/patch';
69+
import { isAngularVersionHigherThan } from '../utility/angular-version';
6970

7071
const projectFilesSource = './files/src';
7172
const workspaceFilesSource = './files';
@@ -166,8 +167,8 @@ function modifyFileRule(path: string, callback: (source: SourceFile) => Change[]
166167
};
167168
}
168169

169-
function updateAppComponent(sourcePath: string, templateOptions: any = {}) {
170-
const appMComponentPath = sourcePath + 'app.component.ts';
170+
function updateAppComponent(host: Tree, sourcePath: string, templateOptions: any = {}) {
171+
const appMComponentPath = sourcePath + (isAngularVersionHigherThan(host, 20) ? 'app.ts' : 'app.component.ts');
171172

172173
const importSetter = (importName: string, path: string, alias: string) => {
173174
return (source: SourceFile) => {
@@ -192,15 +193,18 @@ function getComponentName(host: Tree, sourcePath: string) {
192193
let name = '';
193194
let index = 0;
194195

195-
if (!host.exists(sourcePath + 'app.component.ts')) {
196+
if (!host.exists(sourcePath + 'app.component.ts') && !host.exists(sourcePath + 'app.ts')) {
196197
name = 'app';
197198
}
198199

199200
while (!name) {
200201
index++;
201202
const componentName = `app${index}`;
202203

203-
if (!host.exists(`${sourcePath}${componentName}.component.ts`)) {
204+
if (
205+
!host.exists(`${sourcePath}${componentName}.component.ts`)
206+
&& !host.exists(`${sourcePath}${componentName}.ts`)
207+
) {
204208
name = componentName;
205209
}
206210
}
@@ -302,6 +306,10 @@ const modifyRouting = (host: Tree, routingFilePath: string) => {
302306
}
303307
};
304308

309+
function setPostfix(host: Tree, name: string) {
310+
return name + (isAngularVersionHigherThan(host, 20) ? '' : '.component');
311+
}
312+
305313
export default function(options: any): Rule {
306314
return async (host: Tree) => {
307315
const project = await getProjectName(host, options.project);
@@ -315,8 +323,9 @@ export default function(options: any): Rule {
315323
const override = options.resolveConflicts === 'override';
316324
const componentName = override ? 'app' : getComponentName(host, appPath);
317325
const pathToCss = sourcePath?.replace(/\/?(\w)+\/?/g, '../');
326+
318327
const templateOptions = {
319-
name: componentName,
328+
name: setPostfix(host, componentName),
320329
layout,
321330
title,
322331
strings,
@@ -340,7 +349,7 @@ export default function(options: any): Rule {
340349
const rules = [
341350
modifyContentByTemplate(sourcePath, projectFilesSource, null, templateOptions, modifyContent),
342351
updateDevextremeConfig(sourcePath),
343-
updateAppComponent(appPath, templateOptions),
352+
updateAppComponent(host, appPath, templateOptions),
344353
addBuildThemeScript(),
345354
() => addCustomThemeStyles(host, options, sourcePath) as any,
346355
addViewportToBody(sourcePath),
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Tree } from '@angular-devkit/schematics';
2+
import { SemVer } from 'semver';
3+
4+
export function getAngularVersion(host: Tree): number {
5+
const packageJson = JSON.parse(host.read('package.json')?.toString() || '{}');
6+
const angularCore = packageJson.dependencies?.['@angular/core'];
7+
8+
if (angularCore) {
9+
const version = new SemVer(angularCore.replace(/\^|\~/g, ''));
10+
return version.major;
11+
}
12+
13+
throw new Error('Angular version not found');
14+
}
15+
16+
export function isAngularVersionHigherThan(host: Tree, version: number): boolean {
17+
return getAngularVersion(host) >= version;
18+
}

0 commit comments

Comments
 (0)