Skip to content

Commit

Permalink
refactor: detect standalone application when using ng add (#3885)
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonroberts authored May 9, 2023
1 parent 98480e9 commit 235f740
Show file tree
Hide file tree
Showing 30 changed files with 178 additions and 44 deletions.
15 changes: 15 additions & 0 deletions modules/component-store/schematics-core/utility/standalone.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import * as ts from 'typescript';
import { Tree } from '@angular-devkit/schematics';
import { findBootstrapApplicationCall } from '@schematics/angular/private/standalone';

export function isStandaloneApp(host: Tree, mainPath: string): boolean {
const source = ts.createSourceFile(
mainPath,
host.readText(mainPath),
ts.ScriptTarget.Latest,
true
);
const bootstrapCall = findBootstrapApplicationCall(source);

return bootstrapCall !== null;
}
15 changes: 15 additions & 0 deletions modules/component/schematics-core/utility/standalone.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import * as ts from 'typescript';
import { Tree } from '@angular-devkit/schematics';
import { findBootstrapApplicationCall } from '@schematics/angular/private/standalone';

export function isStandaloneApp(host: Tree, mainPath: string): boolean {
const source = ts.createSourceFile(
mainPath,
host.readText(mainPath),
ts.ScriptTarget.Latest,
true
);
const bootstrapCall = findBootstrapApplicationCall(source);

return bootstrapCall !== null;
}
15 changes: 15 additions & 0 deletions modules/data/schematics-core/utility/standalone.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import * as ts from 'typescript';
import { Tree } from '@angular-devkit/schematics';
import { findBootstrapApplicationCall } from '@schematics/angular/private/standalone';

export function isStandaloneApp(host: Tree, mainPath: string): boolean {
const source = ts.createSourceFile(
mainPath,
host.readText(mainPath),
ts.ScriptTarget.Latest,
true
);
const bootstrapCall = findBootstrapApplicationCall(source);

return bootstrapCall !== null;
}
15 changes: 15 additions & 0 deletions modules/effects/schematics-core/utility/standalone.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import * as ts from 'typescript';
import { Tree } from '@angular-devkit/schematics';
import { findBootstrapApplicationCall } from '@schematics/angular/private/standalone';

export function isStandaloneApp(host: Tree, mainPath: string): boolean {
const source = ts.createSourceFile(
mainPath,
host.readText(mainPath),
ts.ScriptTarget.Latest,
true
);
const bootstrapCall = findBootstrapApplicationCall(source);

return bootstrapCall !== null;
}
1 change: 0 additions & 1 deletion modules/effects/schematics/ng-add/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,6 @@ describe('Effects ng-add Schematic', () => {
const standaloneDefaultOptions = {
...defaultOptions,
project: 'bar-standalone',
standalone: true,
};

it('provides minimal effects setup', async () => {
Expand Down
8 changes: 6 additions & 2 deletions modules/effects/schematics/ng-add/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
callsProvidersFunction,
} from '@schematics/angular/private/standalone';
import { getProjectMainFile } from '../../schematics-core/utility/project';
import { isStandaloneApp } from '../../schematics-core/utility/standalone';

function addImportToNgModule(options: EffectOptions): Rule {
return (host: Tree) => {
Expand Down Expand Up @@ -199,9 +200,12 @@ function addStandaloneConfig(options: EffectOptions): Rule {

export default function (options: EffectOptions): Rule {
return (host: Tree, context: SchematicContext) => {
const mainFile = getProjectMainFile(host, options);
const isStandalone = isStandaloneApp(host, mainFile);

options.path = getProjectPath(host, options);

if (options.module && !options.standalone) {
if (options.module && !isStandalone) {
options.module = findModuleFromOptions(host, options);
}

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

const configOrModuleUpdate = options.standalone
const configOrModuleUpdate = isStandalone
? addStandaloneConfig(options)
: addImportToNgModule(options);

Expand Down
5 changes: 0 additions & 5 deletions modules/effects/schematics/ng-add/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,6 @@
"type": "boolean",
"default": true,
"description": "Setup root effects module without registering initial effects."
},
"standalone": {
"type": "boolean",
"default": false,
"description": "Configure @ngrx/effects for standalone application"
}
},
"required": []
Expand Down
1 change: 0 additions & 1 deletion modules/effects/schematics/ng-add/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,4 @@ export interface Schema {
* Setup root effects module without registering initial effects.
*/
minimal?: boolean;
standalone?: boolean;
}
15 changes: 15 additions & 0 deletions modules/entity/schematics-core/utility/standalone.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import * as ts from 'typescript';
import { Tree } from '@angular-devkit/schematics';
import { findBootstrapApplicationCall } from '@schematics/angular/private/standalone';

export function isStandaloneApp(host: Tree, mainPath: string): boolean {
const source = ts.createSourceFile(
mainPath,
host.readText(mainPath),
ts.ScriptTarget.Latest,
true
);
const bootstrapCall = findBootstrapApplicationCall(source);

return bootstrapCall !== null;
}
15 changes: 15 additions & 0 deletions modules/router-store/schematics-core/utility/standalone.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import * as ts from 'typescript';
import { Tree } from '@angular-devkit/schematics';
import { findBootstrapApplicationCall } from '@schematics/angular/private/standalone';

export function isStandaloneApp(host: Tree, mainPath: string): boolean {
const source = ts.createSourceFile(
mainPath,
host.readText(mainPath),
ts.ScriptTarget.Latest,
true
);
const bootstrapCall = findBootstrapApplicationCall(source);

return bootstrapCall !== null;
}
1 change: 0 additions & 1 deletion modules/router-store/schematics/ng-add/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ describe('Router Store ng-add Schematic', () => {
const standaloneDefaultOptions = {
...defaultOptions,
project: 'bar-standalone',
standalone: true,
};

it('provides initial setup', async () => {
Expand Down
8 changes: 6 additions & 2 deletions modules/router-store/schematics/ng-add/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
callsProvidersFunction,
} from '@schematics/angular/private/standalone';
import { getProjectMainFile } from '../../schematics-core/utility/project';
import { isStandaloneApp } from '../../schematics-core/utility/standalone';

function addImportToNgModule(options: RouterStoreOptions): Rule {
return (host: Tree) => {
Expand Down Expand Up @@ -130,9 +131,12 @@ function addStandaloneConfig(options: RouterStoreOptions): Rule {

export default function (options: RouterStoreOptions): Rule {
return (host: Tree, context: SchematicContext) => {
const mainFile = getProjectMainFile(host, options);
const isStandalone = isStandaloneApp(host, mainFile);

options.path = getProjectPath(host, options);

if (options.module && !options.standalone) {
if (options.module && !isStandalone) {
options.module = findModuleFromOptions(host, {
name: '',
module: options.module,
Expand All @@ -143,7 +147,7 @@ export default function (options: RouterStoreOptions): Rule {
const parsedPath = parseName(options.path, '');
options.path = parsedPath.path;

const configOrModuleUpdate = options.standalone
const configOrModuleUpdate = isStandalone
? addStandaloneConfig(options)
: addImportToNgModule(options);

Expand Down
5 changes: 0 additions & 5 deletions modules/router-store/schematics/ng-add/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@
"description": "Allows specification of the declaring module.",
"alias": "m",
"subtype": "filepath"
},
"standalone": {
"type": "boolean",
"default": false,
"description": "Configure @ngrx/router-store for standalone application"
}
},
"required": []
Expand Down
1 change: 0 additions & 1 deletion modules/router-store/schematics/ng-add/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ export interface Schema {
path?: string;
project?: string;
module?: string;
standalone?: string;
}
15 changes: 15 additions & 0 deletions modules/schematics-core/utility/standalone.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import * as ts from 'typescript';
import { Tree } from '@angular-devkit/schematics';
import { findBootstrapApplicationCall } from '@schematics/angular/private/standalone';

export function isStandaloneApp(host: Tree, mainPath: string): boolean {
const source = ts.createSourceFile(
mainPath,
host.readText(mainPath),
ts.ScriptTarget.Latest,
true
);
const bootstrapCall = findBootstrapApplicationCall(source);

return bootstrapCall !== null;
}
15 changes: 15 additions & 0 deletions modules/schematics/schematics-core/utility/standalone.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import * as ts from 'typescript';
import { Tree } from '@angular-devkit/schematics';
import { findBootstrapApplicationCall } from '@schematics/angular/private/standalone';

export function isStandaloneApp(host: Tree, mainPath: string): boolean {
const source = ts.createSourceFile(
mainPath,
host.readText(mainPath),
ts.ScriptTarget.Latest,
true
);
const bootstrapCall = findBootstrapApplicationCall(source);

return bootstrapCall !== null;
}
15 changes: 15 additions & 0 deletions modules/store-devtools/schematics-core/utility/standalone.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import * as ts from 'typescript';
import { Tree } from '@angular-devkit/schematics';
import { findBootstrapApplicationCall } from '@schematics/angular/private/standalone';

export function isStandaloneApp(host: Tree, mainPath: string): boolean {
const source = ts.createSourceFile(
mainPath,
host.readText(mainPath),
ts.ScriptTarget.Latest,
true
);
const bootstrapCall = findBootstrapApplicationCall(source);

return bootstrapCall !== null;
}
1 change: 0 additions & 1 deletion modules/store-devtools/schematics/ng-add/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ describe('Store-Devtools ng-add Schematic', () => {
const standaloneDefaultOptions = {
...defaultOptions,
project: 'bar-standalone',
standalone: true,
};

it('provides initial setup', async () => {
Expand Down
8 changes: 6 additions & 2 deletions modules/store-devtools/schematics/ng-add/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
callsProvidersFunction,
} from '@schematics/angular/private/standalone';
import { getProjectMainFile } from '../../schematics-core/utility/project';
import { isStandaloneApp } from '../../schematics-core/utility/standalone';

function addImportToNgModule(options: StoreDevtoolsOptions): Rule {
return (host: Tree) => {
Expand Down Expand Up @@ -154,9 +155,12 @@ function addStandaloneConfig(options: StoreDevtoolsOptions): Rule {

export default function (options: StoreDevtoolsOptions): Rule {
return (host: Tree, context: SchematicContext) => {
const mainFile = getProjectMainFile(host, options);
const isStandalone = isStandaloneApp(host, mainFile);

options.path = getProjectPath(host, options);

if (options.module && !options.standalone) {
if (options.module && !isStandalone) {
options.module = findModuleFromOptions(host, {
name: '',
module: options.module,
Expand All @@ -173,7 +177,7 @@ export default function (options: StoreDevtoolsOptions): Rule {
);
}

const configOrModuleUpdate = options.standalone
const configOrModuleUpdate = isStandalone
? addStandaloneConfig(options)
: addImportToNgModule(options);

Expand Down
5 changes: 0 additions & 5 deletions modules/store-devtools/schematics/ng-add/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,6 @@
"type": "boolean",
"default": false,
"description": "boolean - pauses recording actions and state changes when the extension window is not open."
},
"standalone": {
"type": "boolean",
"default": false,
"description": "Configure @ngrx/store-devtools for standalone application"
}
},
"required": []
Expand Down
1 change: 0 additions & 1 deletion modules/store-devtools/schematics/ng-add/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@ export interface Schema {
module?: string;
maxAge?: number;
autoPause?: boolean;
standalone?: boolean;
}
15 changes: 15 additions & 0 deletions modules/store/schematics-core/utility/standalone.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import * as ts from 'typescript';
import { Tree } from '@angular-devkit/schematics';
import { findBootstrapApplicationCall } from '@schematics/angular/private/standalone';

export function isStandaloneApp(host: Tree, mainPath: string): boolean {
const source = ts.createSourceFile(
mainPath,
host.readText(mainPath),
ts.ScriptTarget.Latest,
true
);
const bootstrapCall = findBootstrapApplicationCall(source);

return bootstrapCall !== null;
}
1 change: 0 additions & 1 deletion modules/store/schematics/ng-add/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,6 @@ describe('Store ng-add Schematic', () => {
const standaloneDefaultOptions = {
...defaultOptions,
project: 'bar-standalone',
standalone: true,
};

it('provides minimal store setup', async () => {
Expand Down
8 changes: 6 additions & 2 deletions modules/store/schematics/ng-add/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
callsProvidersFunction,
} from '@schematics/angular/private/standalone';
import { getProjectMainFile } from '../../schematics-core/utility/project';
import { isStandaloneApp } from '../../schematics-core/utility/standalone';

function addImportToNgModule(options: RootStoreOptions): Rule {
return (host: Tree) => {
Expand Down Expand Up @@ -212,12 +213,15 @@ function addStandaloneConfig(options: RootStoreOptions): Rule {

export default function (options: RootStoreOptions): Rule {
return (host: Tree, context: SchematicContext) => {
const mainFile = getProjectMainFile(host, options);
const isStandalone = isStandaloneApp(host, mainFile);

options.path = getProjectPath(host, options);

const parsedPath = parseName(options.path, '');
options.path = parsedPath.path;

if (options.module && !options.standalone) {
if (options.module && !isStandalone) {
options.module = findModuleFromOptions(host, {
name: '',
module: options.module,
Expand All @@ -238,7 +242,7 @@ export default function (options: RootStoreOptions): Rule {
move(parsedPath.path),
]);

const configOrModuleUpdate = options.standalone
const configOrModuleUpdate = isStandalone
? addStandaloneConfig(options)
: addImportToNgModule(options);

Expand Down
5 changes: 0 additions & 5 deletions modules/store/schematics/ng-add/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,6 @@
"type": "boolean",
"default": false,
"description": "Do not register the NgRx ESLint Plugin."
},
"standalone": {
"type": "boolean",
"default": false,
"description": "Configure store for standalone application"
}
},
"required": []
Expand Down
1 change: 0 additions & 1 deletion modules/store/schematics/ng-add/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@ export interface Schema {
*/
minimal?: boolean;
skipESLintPlugin?: boolean;
standalone?: boolean;
}
Loading

0 comments on commit 235f740

Please sign in to comment.