IgniteUI / igniteui-cli

Ignite UI Command-Line Interface by Infragistics
https://bit.ly/2L19rhp
MIT License
118 stars 4 forks source link

TypeScriptFileUpdate refactoring #1243

Closed jackofdiamond5 closed 2 months ago

jackofdiamond5 commented 5 months ago

Closes #30401, https://github.com/IgniteUI/igniteui-cli/issues/1210

This PR introduces new types AngularTypeScriptFileUpdate, WebComponentsTypeScriptFileUpdate, ReactTypeScriptFileUpdate which use the TypeScriptASTTransformer internally to apply specific mutations to the AST depending on the platform.

The old TypeScriptFileUpdate that was in the core package has now been reworked so that it serves as a base for the platform-specific file updates. This caused the need to update some parts of the CLI that were relying on it, like the add command, the Angular specific templates (AngularTemplate & IgniteUIForAngularTemplate), and the cli-config schematic since they should now point to the AngularTypeScriptFileUpdate.

Additionally, this PR introduces a lot of new custom types that are related to routing and Angular metadata. I've tried to keep these as general as possible so that they can be later extended by the different platforms. The attempt was to make everything as strictly typed as possible and minimize the usage of magic strings and inline type definitions.

The AngularTypeScriptFileUpdate is initialized with a filePath, standalone flag and a formatSettings. The path is used to get to the ts.SourceFIle which the transformer will be mutating while the format settings are used during finalization to apply the desired formatting to the parsed tree. The standalone flag is Angular specific and is pretty self-explanatory - some of the logic in the file update is specific for standalone components and requires that the flag is present.


The reworked version of the TypeScriptFileUpdate that exists in core includes the handling of import declarations as well as general routing. Each platform should use it as a base and add additional logic where needed.

TypeScriptFileUpdate

Public API:

addRoute - Adds a route entry to the specified routes collection. This is an abstract method that must be implemented by the different platforms since every platform has different ways of describing their routes.

addChildRoute - Adds a child route to a parent route. Targeting a parent route (by path), this method will add a route to a route's children array (creating it if it does not exist)

finalize - Parses the AST and returns the resulting formatted source code.

Protected API:

buildRouteStructure - Builds the route structure for the given route per platform. Each platform has a different way of describing its route objects, so they have to implement this abstract method and define how a route is to be built.

createDynamicImport - Creates an arrow function of the form () => import(path).then(m => m.prop).

createArrowFunctionWithCallExpression - Creates an arrow function with a call expression that takes the form memberName: () => callExpressionName(callExpressionArgs).

applyConfigTransformation - Applies a configuration transformation to a string.

asArray - Converts a string or string array union to array.

requestSideEffectsImportForModule - Creates a side effect import declaration for a given module. Checks if the import has been added already.

requestImportForRouteIdentifier - Adds an import declaration for an identifier that exists in a route node. Checks if it does not collide with other imports first.

AngularTypeScriptFileUpdate

Extends the TypeScriptFileUpdate with the following members:

addStandaloneComponentMeta - This method operates for standalone components only and uses a TemplateDependency to apply mutations to a standalone component's @Component decorator constructor function. It also supports replacing specific placeholders/variables that are defined as a KeyValuePair object.

addNgModuleMeta - This method operates for NgModule-based projects only and uses a TemplateDependency to apply mutations to an @NgModule decorator constructor function. It also supports replacing specific placeholders/variables that are defined as a KeyValuePair object.

coveralls commented 3 months ago

Coverage Status

coverage: 66.597% (-0.7%) from 67.261% when pulling 5da0da38c2659132e7060c50de13dc432608d92f on bpenkov/angular-ts-file-update into d19120abd1db318561d30ed436865ff142e48a6f on master.

coveralls commented 3 months ago

Coverage Status

coverage: 66.64% (-0.6%) from 67.261% when pulling 00716dfec7485bb9ad35312a81d3e87c051bd2e9 on bpenkov/angular-ts-file-update into d19120abd1db318561d30ed436865ff142e48a6f on master.

coveralls commented 3 months ago

Coverage Status

coverage: 66.643% (-0.6%) from 67.261% when pulling 4ada9c55e1406900f0253378a9c4ec1c3de22e95 on bpenkov/angular-ts-file-update into d19120abd1db318561d30ed436865ff142e48a6f on master.

coveralls commented 3 months ago

Coverage Status

coverage: 66.643% (-0.6%) from 67.261% when pulling 456915c6811422574aa856ef55be32db0a01cbf2 on bpenkov/angular-ts-file-update into d19120abd1db318561d30ed436865ff142e48a6f on master.

coveralls commented 3 months ago

Coverage Status

coverage: 66.643% (-0.6%) from 67.261% when pulling c4e654fdd782e88bcd21bb8c171a24c0598e36e7 on bpenkov/angular-ts-file-update into d19120abd1db318561d30ed436865ff142e48a6f on master.

coveralls commented 3 months ago

Coverage Status

coverage: 66.667% (-0.6%) from 67.261% when pulling 0544961c7b1d356f5a56c4b46e70e0ae19d95cf4 on bpenkov/angular-ts-file-update into d19120abd1db318561d30ed436865ff142e48a6f on master.

coveralls commented 3 months ago

Coverage Status

coverage: 66.667% (-0.6%) from 67.261% when pulling b887f042fc1355d83d466e76c2bc8d1d780c82f8 on bpenkov/angular-ts-file-update into d19120abd1db318561d30ed436865ff142e48a6f on master.

coveralls commented 2 months ago

Coverage Status

coverage: 70.793% (+4.0%) from 66.802% when pulling 372f1afc9ddaa33abbf39b21730730933eb4ca31 on bpenkov/angular-ts-file-update into 92a14ef05f174bb5e5bc09978c986eb8f27e57e1 on master.