Skip to content

Commit

Permalink
feat(angular): export some utils as public api #15669
Browse files Browse the repository at this point in the history
  • Loading branch information
Coly010 committed Apr 3, 2023
1 parent 9b60863 commit 3895696
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/angular/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"./executors": "./executors.js",
"./tailwind": "./tailwind.js",
"./src/generators/utils": "./src/generators/utils/index.js",
"./src/utils": "./src/utils/public-api.js",
"./module-federation": "./module-federation/index.js",
"./plugins/component-testing": "./plugins/component-testing.js"
},
Expand Down
62 changes: 60 additions & 2 deletions packages/angular/src/utils/nx-devkit/ast-utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import type * as ts from 'typescript';
import { findNodes } from 'nx/src/utils/typescript';
import { getSourceNodes } from '@nrwl/js';
import {
getImport,
getSourceNodes,
insertChange,
removeChange,
replaceChange,
} from '@nrwl/js';
import { dirname, join } from 'path';
import { names, readProjectConfiguration, Tree } from '@nrwl/devkit';
import { getImport, insertChange, removeChange, replaceChange } from '@nrwl/js';
import { ensureTypescript } from '@nrwl/js/src/utils/typescript/ensure-typescript';

let tsModule: typeof import('typescript');
Expand Down Expand Up @@ -65,6 +70,11 @@ function _angularImportsFromNode(
}
}

/**
* Check if the Component, Directive or Pipe is standalone
* @param sourceFile TS Source File containing the token to check
* @param decoratorName The type of decorator to check (Component, Directive, Pipe)
*/
export function isStandalone(
sourceFile: ts.SourceFile,
decoratorName: DecoratorName
Expand Down Expand Up @@ -341,6 +351,13 @@ export function removeFromNgModule(
}
}

/**
* Add an import to a Standalone Component
* @param host Virtual Tree
* @param source TS Source File containing the Component
* @param componentPath The path to the Component
* @param symbolName The import to add to the Component
*/
export function addImportToComponent(
host: Tree,
source: ts.SourceFile,
Expand All @@ -357,6 +374,13 @@ export function addImportToComponent(
);
}

/**
* Add an import to a Standalone Directive
* @param host Virtual Tree
* @param source TS Source File containing the Directive
* @param directivePath The path to the Directive
* @param symbolName The import to add to the Directive
*/
export function addImportToDirective(
host: Tree,
source: ts.SourceFile,
Expand All @@ -373,6 +397,13 @@ export function addImportToDirective(
);
}

/**
* Add an import to a Standalone Pipe
* @param host Virtual Tree
* @param source TS Source File containing the Pipe
* @param pipePath The path to the Pipe
* @param symbolName The import to add to the Pipe
*/
export function addImportToPipe(
host: Tree,
source: ts.SourceFile,
Expand All @@ -389,6 +420,13 @@ export function addImportToPipe(
);
}

/**
* Add an import to an NgModule
* @param host Virtual Tree
* @param source TS Source File containing the NgModule
* @param modulePath The path to the NgModule
* @param symbolName The import to add to the NgModule
*/
export function addImportToModule(
host: Tree,
source: ts.SourceFile,
Expand Down Expand Up @@ -638,6 +676,12 @@ function getListOfRoutes(
return null;
}

/**
* Add a provider to bootstrapApplication call for Standalone Applications
* @param tree Virtual Tree
* @param filePath Path to the file containing the bootstrapApplication call
* @param providerToAdd Provider to add
*/
export function addProviderToBootstrapApplication(
tree: Tree,
filePath: string,
Expand Down Expand Up @@ -672,6 +716,13 @@ export function addProviderToBootstrapApplication(
tree.write(filePath, newFileContents);
}

/**
* Add a provider to an NgModule
* @param host Virtual Tree
* @param source TS Source File containing the NgModule
* @param modulePath Path to the NgModule
* @param symbolName The provider to add
*/
export function addProviderToModule(
host: Tree,
source: ts.SourceFile,
Expand All @@ -687,6 +738,13 @@ export function addProviderToModule(
);
}

/**
* Add a provider to a Standalone Component
* @param host Virtual Tree
* @param source TS Source File containing the Component
* @param componentPath Path to the Component
* @param symbolName The provider to add
*/
export function addProviderToComponent(
host: Tree,
source: ts.SourceFile,
Expand Down
16 changes: 16 additions & 0 deletions packages/angular/src/utils/nx-devkit/route-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ import { ensureTypescript } from '@nrwl/js/src/utils/typescript/ensure-typescrip

let tsModule: typeof import('typescript');

/**
* Add a new route to a routes definition
* @param tree Virtual Tree
* @param routesFile File containing the routes definition
* @param route Route to add
* @param lazy If Route should be lazy-loaded
* @param routesConst Used when eager-loading a route: Class name of the Component
* @param importPath Used when eager-loading a route: The import path to the Component
*/
export function addRoute(
tree: Tree,
routesFile: string,
Expand Down Expand Up @@ -81,6 +90,13 @@ export function addRoute(
tree.write(routesFile, newRoutesFileContents);
}

/**
* Add a provider to a standalone routes definition
* @param tree Virtual Tree
* @param routesFile The file containing the routes definition
* @param routeToAddProviderTo The route to add the provider to
* @param providerToAdd The provider to add to the route
*/
export function addProviderToRoute(
tree: Tree,
routesFile: string,
Expand Down
12 changes: 12 additions & 0 deletions packages/angular/src/utils/public-api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export {
isStandalone,
addImportToComponent,
addImportToDirective,
addImportToPipe,
addImportToModule,
addProviderToBootstrapApplication,
addProviderToComponent,
addProviderToModule,
} from './nx-devkit/ast-utils';

export { addRoute, addProviderToRoute } from './nx-devkit/route-utils';

0 comments on commit 3895696

Please sign in to comment.