Skip to content

Commit 1a82f63

Browse files
Fix issues in create-app for Angular (#1010)
fix issues in cli for Angular
1 parent 3dc299d commit 1a82f63

File tree

7 files changed

+61
-14
lines changed

7 files changed

+61
-14
lines changed

packages/devextreme-cli/src/utility/ng-version.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ const semver = require('semver').SemVer;
22
const execSync = require('child_process').execSync;
33

44
function parseNgCliVersion(stdout) {
5-
return /angular.cli:\s*(\S+)/ig.exec(stdout)[1];
5+
return stdout.trim();
66
}
77

88
const getLocalNgVersion = () => {
99
try {
10-
const version = parseNgCliVersion(execSync('ng v').toString());
10+
const version = parseNgCliVersion(execSync('ng --version').toString());
1111
return new semver(version);
1212
} catch(e) {
1313
return '';
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
export const navigation = [];
1+
type NavigationItem = { path?: string, text: string, icon?: string, items?: NavigationItem[] };
2+
export const navigation: NavigationItem[] = [];
3+

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

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ import {
4444

4545
import {
4646
NodeDependencyType,
47-
addPackageJsonDependency
47+
addPackageJsonDependency,
48+
getPackageJsonDependency,
4849
} from '@schematics/angular/utility/dependencies';
4950

5051
import { getSourceFile } from '../utility/source';
@@ -173,6 +174,28 @@ async function updateBudgets(host: Tree, options: any) {
173174
return host;
174175
}
175176

177+
async function addPolyfills(host: Tree, project: string, polyfills: string[]) {
178+
const projectName = await getProjectName(host, project);
179+
180+
modifyJSONFile(host, './angular.json', config => {
181+
const buildOptions: Record<string, any> = config.projects[projectName]['architect']['build']['options'];
182+
183+
if (!buildOptions.polyfills) {
184+
buildOptions.polyfills = [];
185+
}
186+
187+
polyfills.forEach((polyfill) => {
188+
if (!buildOptions.polyfills.includes(polyfill)) {
189+
buildOptions.polyfills.push(polyfill);
190+
}
191+
});
192+
193+
return config;
194+
});
195+
196+
return host;
197+
}
198+
176199
function addViewportToBody(sourcePath: string = '') {
177200
return (host: Tree) => {
178201
const indexPath = join(sourcePath, 'index.html');
@@ -199,8 +222,8 @@ function modifyFileRule(path: string, callback: (source: SourceFile) => Change[]
199222
};
200223
}
201224

202-
function updateAppComponent(host: Tree, sourcePath: string, templateOptions: any = {}) {
203-
const appMComponentPath = sourcePath + (isAngularVersionHigherThan(host, 20) ? 'app.ts' : 'app.component.ts');
225+
function updateAppComponent(sourcePath: string, templateOptions: any = {}) {
226+
const appMComponentPath = sourcePath + templateOptions.name + '.ts';
204227

205228
const importSetter = (importName: string, path: string, alias: string) => {
206229
return (source: SourceFile) => {
@@ -250,13 +273,25 @@ function hasRouting(host: Tree, sourcePath: string) {
250273

251274
function addPackagesToDependency(globalNgCliVersion: string) {
252275
const version = new SemVer(globalNgCliVersion.replace(/\^|\~/g, ''));
276+
253277
return (host: Tree) => {
278+
const zonejs = getPackageJsonDependency(host, 'zone.js');
279+
254280
addPackageJsonDependency(host, {
255281
type: NodeDependencyType.Default,
256282
name: '@angular/cdk',
257-
version: `~${version.major}.${version.minor}.0`
283+
version: version.raw.includes('next') || version.raw.includes('-rc')
284+
? 'next' : `^${version.major}.0.0`
258285
});
259286

287+
if (!zonejs) {
288+
addPackageJsonDependency(host, {
289+
type: NodeDependencyType.Default,
290+
name: 'zone.js',
291+
version: version.major > 18 ? '~0.15.0' : '~0.14.0'
292+
});
293+
}
294+
260295
return host;
261296
};
262297
}
@@ -389,11 +424,12 @@ export default function(options: any): Rule {
389424
const rules = [
390425
modifyContentByTemplate(sourcePath, projectFilesSource, null, templateOptions, modifyContent),
391426
updateDevextremeConfig(sourcePath),
392-
updateAppComponent(host, appPath, templateOptions),
427+
updateAppComponent(appPath, templateOptions),
393428
addBuildThemeScript(),
394429
() => addCustomThemeStyles(host, options, sourcePath) as any,
395430
addViewportToBody(sourcePath),
396-
addPackagesToDependency(options.globalNgCliVersion)
431+
addPackagesToDependency(options.globalNgCliVersion),
432+
() => addPolyfills(host, options.project, ['zone.js'])
397433
];
398434

399435
if (options.updateBudgets) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ describe('layout', () => {
174174
const tree = await runner.runSchematic('add-layout', options, appTree);
175175
const packageConfig = JSON.parse(tree.readContent('package.json'));
176176

177-
expect(packageConfig.dependencies['@angular/cdk']).toBe('~17.2.0');
177+
expect(packageConfig.dependencies['@angular/cdk']).toBe('^17.0.0');
178178
});
179179

180180
it('should update budgets if updateBudgets option is true', async () => {

packages/devextreme-schematics/src/add-sample-views/index_spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ describe('sample views', () => {
4949

5050
const navigationContent = tree.readContent('/src/app/app-navigation.ts');
5151
expect(navigationContent).toMatch(/text: 'Home'/);
52-
expect(navigationContent).toContain(`export const navigation = [
52+
expect(navigationContent).toContain(
53+
`type NavigationItem = { path?: string, text: string, icon?: string, items?: NavigationItem[] };
54+
export const navigation: NavigationItem[] = [
5355
{
5456
text: 'Home',
5557
path: '/home',

packages/devextreme-schematics/src/add-view/index_spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,9 @@ describe('view', () => {
117117
expect(moduleContent).toMatch(/text: 'Test'/);
118118
expect(moduleContent).toMatch(/icon: 'folder'/);
119119

120-
expect(moduleContent).toContain(`navigation = [
120+
expect(moduleContent).toContain(
121+
`type NavigationItem = { path?: string, text: string, icon?: string, items?: NavigationItem[] };
122+
export const navigation: NavigationItem[] = [
121123
{
122124
text: 'Test',
123125
path: '/pages/test',

packages/devextreme-schematics/src/utility/change.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,19 @@ export function insertItemToArray(
3636

3737
const nodeContent = node.getText();
3838
const nodePosition = node.getStart();
39-
const leftBracketPosition = nodePosition + nodeContent.indexOf('[');
39+
const leftBracketPosition = nodePosition + nodeContent.indexOf('[', nodeContent.lastIndexOf('=') || 0);
4040
const rightBracketPosition = nodePosition + nodeContent.lastIndexOf(']');
4141
let itemPosition = leftBracketPosition + 1;
4242
let fileRecorder = host.beginUpdate(filePath);
4343

4444
item = newLine + item;
4545

46-
const isNodeEmpty = !/\[[\s\S]*\S+[\s\S]*\]/m.test(nodeContent);
46+
const isNodeEmpty = !(
47+
nodeContent.includes('=')
48+
? /=\s*\[[\s\S]*\S+[\s\S]*\]/m
49+
: /\[[\s\S]*\S+[\s\S]*\]/m
50+
).test(nodeContent);
51+
4752
if (isNodeEmpty) {
4853
const formattedArray = `[${newLine}]`;
4954
fileRecorder.remove(leftBracketPosition, rightBracketPosition - leftBracketPosition + 1);

0 commit comments

Comments
 (0)