Skip to content

Commit 235f740

Browse files
refactor: detect standalone application when using ng add (#3885)
1 parent 98480e9 commit 235f740

File tree

30 files changed

+178
-44
lines changed

30 files changed

+178
-44
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import * as ts from 'typescript';
2+
import { Tree } from '@angular-devkit/schematics';
3+
import { findBootstrapApplicationCall } from '@schematics/angular/private/standalone';
4+
5+
export function isStandaloneApp(host: Tree, mainPath: string): boolean {
6+
const source = ts.createSourceFile(
7+
mainPath,
8+
host.readText(mainPath),
9+
ts.ScriptTarget.Latest,
10+
true
11+
);
12+
const bootstrapCall = findBootstrapApplicationCall(source);
13+
14+
return bootstrapCall !== null;
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import * as ts from 'typescript';
2+
import { Tree } from '@angular-devkit/schematics';
3+
import { findBootstrapApplicationCall } from '@schematics/angular/private/standalone';
4+
5+
export function isStandaloneApp(host: Tree, mainPath: string): boolean {
6+
const source = ts.createSourceFile(
7+
mainPath,
8+
host.readText(mainPath),
9+
ts.ScriptTarget.Latest,
10+
true
11+
);
12+
const bootstrapCall = findBootstrapApplicationCall(source);
13+
14+
return bootstrapCall !== null;
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import * as ts from 'typescript';
2+
import { Tree } from '@angular-devkit/schematics';
3+
import { findBootstrapApplicationCall } from '@schematics/angular/private/standalone';
4+
5+
export function isStandaloneApp(host: Tree, mainPath: string): boolean {
6+
const source = ts.createSourceFile(
7+
mainPath,
8+
host.readText(mainPath),
9+
ts.ScriptTarget.Latest,
10+
true
11+
);
12+
const bootstrapCall = findBootstrapApplicationCall(source);
13+
14+
return bootstrapCall !== null;
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import * as ts from 'typescript';
2+
import { Tree } from '@angular-devkit/schematics';
3+
import { findBootstrapApplicationCall } from '@schematics/angular/private/standalone';
4+
5+
export function isStandaloneApp(host: Tree, mainPath: string): boolean {
6+
const source = ts.createSourceFile(
7+
mainPath,
8+
host.readText(mainPath),
9+
ts.ScriptTarget.Latest,
10+
true
11+
);
12+
const bootstrapCall = findBootstrapApplicationCall(source);
13+
14+
return bootstrapCall !== null;
15+
}

modules/effects/schematics/ng-add/index.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,6 @@ describe('Effects ng-add Schematic', () => {
231231
const standaloneDefaultOptions = {
232232
...defaultOptions,
233233
project: 'bar-standalone',
234-
standalone: true,
235234
};
236235

237236
it('provides minimal effects setup', async () => {

modules/effects/schematics/ng-add/index.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import {
3333
callsProvidersFunction,
3434
} from '@schematics/angular/private/standalone';
3535
import { getProjectMainFile } from '../../schematics-core/utility/project';
36+
import { isStandaloneApp } from '../../schematics-core/utility/standalone';
3637

3738
function addImportToNgModule(options: EffectOptions): Rule {
3839
return (host: Tree) => {
@@ -199,9 +200,12 @@ function addStandaloneConfig(options: EffectOptions): Rule {
199200

200201
export default function (options: EffectOptions): Rule {
201202
return (host: Tree, context: SchematicContext) => {
203+
const mainFile = getProjectMainFile(host, options);
204+
const isStandalone = isStandaloneApp(host, mainFile);
205+
202206
options.path = getProjectPath(host, options);
203207

204-
if (options.module && !options.standalone) {
208+
if (options.module && !isStandalone) {
205209
options.module = findModuleFromOptions(host, options);
206210
}
207211

@@ -226,7 +230,7 @@ export default function (options: EffectOptions): Rule {
226230
move(parsedPath.path),
227231
]);
228232

229-
const configOrModuleUpdate = options.standalone
233+
const configOrModuleUpdate = isStandalone
230234
? addStandaloneConfig(options)
231235
: addImportToNgModule(options);
232236

modules/effects/schematics/ng-add/schema.json

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,6 @@
5555
"type": "boolean",
5656
"default": true,
5757
"description": "Setup root effects module without registering initial effects."
58-
},
59-
"standalone": {
60-
"type": "boolean",
61-
"default": false,
62-
"description": "Configure @ngrx/effects for standalone application"
6358
}
6459
},
6560
"required": []

modules/effects/schematics/ng-add/schema.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,4 @@ export interface Schema {
1111
* Setup root effects module without registering initial effects.
1212
*/
1313
minimal?: boolean;
14-
standalone?: boolean;
1514
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import * as ts from 'typescript';
2+
import { Tree } from '@angular-devkit/schematics';
3+
import { findBootstrapApplicationCall } from '@schematics/angular/private/standalone';
4+
5+
export function isStandaloneApp(host: Tree, mainPath: string): boolean {
6+
const source = ts.createSourceFile(
7+
mainPath,
8+
host.readText(mainPath),
9+
ts.ScriptTarget.Latest,
10+
true
11+
);
12+
const bootstrapCall = findBootstrapApplicationCall(source);
13+
14+
return bootstrapCall !== null;
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import * as ts from 'typescript';
2+
import { Tree } from '@angular-devkit/schematics';
3+
import { findBootstrapApplicationCall } from '@schematics/angular/private/standalone';
4+
5+
export function isStandaloneApp(host: Tree, mainPath: string): boolean {
6+
const source = ts.createSourceFile(
7+
mainPath,
8+
host.readText(mainPath),
9+
ts.ScriptTarget.Latest,
10+
true
11+
);
12+
const bootstrapCall = findBootstrapApplicationCall(source);
13+
14+
return bootstrapCall !== null;
15+
}

0 commit comments

Comments
 (0)