@barcidev/ngx-autogen 0.1.35 → 0.1.36

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@barcidev/ngx-autogen",
3
- "version": "0.1.35",
3
+ "version": "0.1.36",
4
4
  "description": "A collection of Angular schematics for essential functionalities.",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -3,7 +3,7 @@ export declare const mergeFilesSmart: (urlPath: string, destPath: string, option
3
3
  /**
4
4
  * Añade un provider a un componente Standalone y gestiona sus imports.
5
5
  */
6
- export declare function addProviderToStandaloneComponent(componentPath: string, providerName: string, imports: {
6
+ export declare function addMetadataToStandaloneComponent(componentPath: string, symbolName: string, importSources: {
7
7
  symbol: string;
8
8
  path: string;
9
- }[]): Rule;
9
+ }[], metadataField?: "providers" | "imports"): Rule;
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.mergeFilesSmart = void 0;
4
- exports.addProviderToStandaloneComponent = addProviderToStandaloneComponent;
4
+ exports.addMetadataToStandaloneComponent = addMetadataToStandaloneComponent;
5
5
  const schematics_1 = require("@angular-devkit/schematics");
6
6
  const change_1 = require("@schematics/angular/utility/change");
7
7
  const ts = require("typescript");
@@ -26,58 +26,58 @@ exports.mergeFilesSmart = mergeFilesSmart;
26
26
  /**
27
27
  * Añade un provider a un componente Standalone y gestiona sus imports.
28
28
  */
29
- function addProviderToStandaloneComponent(componentPath, providerName, imports) {
29
+ function addMetadataToStandaloneComponent(componentPath, symbolName, importSources, metadataField = "imports") {
30
30
  return (tree) => {
31
- var _a;
32
31
  const text = tree.read(componentPath);
33
32
  if (!text)
34
33
  throw new schematics_2.SchematicsException(`No se encontró el archivo: ${componentPath}`);
35
34
  const sourceText = text.toString("utf-8");
36
35
  const source = ts.createSourceFile(componentPath, sourceText, ts.ScriptTarget.Latest, true);
37
36
  const recorder = tree.beginUpdate(componentPath);
38
- // 1. Gestionar Imports
39
- imports.forEach((item) => {
37
+ // 1. Gestionar Imports de TypeScript (Cabecera del archivo)
38
+ importSources.forEach((item) => {
40
39
  const importChange = (0, ast_utils_1.insertImport)(source, componentPath, item.symbol, item.path);
41
40
  if (importChange instanceof change_1.InsertChange) {
42
41
  recorder.insertLeft(importChange.pos, importChange.toAdd);
43
42
  }
44
43
  });
45
- // 2. Buscar el decorador @Component y su propiedad 'providers'
44
+ // 2. Buscar la clase con el decorador @Component
46
45
  const componentClass = source.statements.find((s) => {
47
46
  var _a;
48
47
  return ts.isClassDeclaration(s) &&
49
- !!((_a = s.modifiers) === null || _a === void 0 ? void 0 : _a.some((m) => m.kind === ts.SyntaxKind.ExportKeyword));
48
+ !!((_a = ts
49
+ .getDecorators(s)) === null || _a === void 0 ? void 0 : _a.some((d) => ts.isCallExpression(d.expression) &&
50
+ ts.isIdentifier(d.expression.expression) &&
51
+ d.expression.expression.text === "Component"));
50
52
  });
51
53
  if (!componentClass)
52
54
  return tree;
53
- const decorator = (_a = ts
54
- .getDecorators(componentClass)) === null || _a === void 0 ? void 0 : _a.find((d) => ts.isCallExpression(d.expression) &&
55
- ts.isIdentifier(d.expression.expression) &&
56
- d.expression.expression.text === "Component");
57
- if (decorator && ts.isCallExpression(decorator.expression)) {
58
- const args = decorator.expression.arguments;
59
- if (args.length > 0 && ts.isObjectLiteralExpression(args[0])) {
60
- const objectLiteral = args[0];
61
- const providersProp = objectLiteral.properties.find((p) => ts.isPropertyAssignment(p) &&
62
- ts.isIdentifier(p.name) &&
63
- p.name.text === "providers");
64
- if (providersProp &&
65
- ts.isArrayLiteralExpression(providersProp.initializer)) {
66
- // Caso: El array de providers YA existe
67
- const elements = providersProp.initializer.elements;
68
- const pos = elements.length > 0
69
- ? elements[elements.length - 1].getEnd()
70
- : providersProp.initializer.getStart() + 1;
71
- const toAdd = elements.length > 0 ? `, ${providerName}` : providerName;
72
- recorder.insertRight(pos, toAdd);
73
- }
74
- else {
75
- // Caso: El array de providers NO existe, lo creamos
76
- const pos = objectLiteral.getStart() + 1;
77
- const toAdd = `\n providers: [${providerName}],`;
78
- recorder.insertRight(pos, toAdd);
79
- }
55
+ const decorator = ts
56
+ .getDecorators(componentClass)
57
+ .find((d) => d.expression.expression.getText() ===
58
+ "Component");
59
+ const callExpr = decorator.expression;
60
+ const objectLiteral = callExpr.arguments[0];
61
+ // 3. Buscar la propiedad específica (imports o providers)
62
+ const targetProp = objectLiteral.properties.find((p) => ts.isPropertyAssignment(p) &&
63
+ ts.isIdentifier(p.name) &&
64
+ p.name.text === metadataField);
65
+ if (targetProp && ts.isArrayLiteralExpression(targetProp.initializer)) {
66
+ // CASO A: El array YA existe
67
+ const elements = targetProp.initializer.elements;
68
+ if (elements.length > 0) {
69
+ const lastElement = elements[elements.length - 1];
70
+ recorder.insertRight(lastElement.getEnd(), `, ${symbolName}`);
80
71
  }
72
+ else {
73
+ recorder.insertRight(targetProp.initializer.getStart() + 1, symbolName);
74
+ }
75
+ }
76
+ else {
77
+ // CASO B: La propiedad NO existe, la creamos al inicio del objeto
78
+ const pos = objectLiteral.getStart() + 1;
79
+ const toAdd = `\n ${metadataField}: [${symbolName}],`;
80
+ recorder.insertRight(pos, toAdd);
81
81
  }
82
82
  tree.commitUpdate(recorder);
83
83
  return tree;
@@ -114,7 +114,7 @@ function registerProviderInComponent(options, componentFile) {
114
114
  const componentPath = (0, path_1.join)(options.path, componentFile);
115
115
  const i18nConstantName = `${schematics_1.strings.camelize(options.name)}I18n`;
116
116
  return (0, schematics_1.chain)([
117
- (0, file_actions_1.addProviderToStandaloneComponent)(componentPath, `provideTranslocoScopeWrapper(${i18nConstantName})`, [
117
+ (0, file_actions_1.addMetadataToStandaloneComponent)(componentPath, `provideTranslocoScopeWrapper(${i18nConstantName})`, [
118
118
  {
119
119
  symbol: "provideTranslocoScopeWrapper",
120
120
  path: "@barcidev/typed-transloco",
@@ -123,8 +123,8 @@ function registerProviderInComponent(options, componentFile) {
123
123
  symbol: i18nConstantName,
124
124
  path: `./${schematics_1.strings.dasherize(options.name)}.i18n`,
125
125
  },
126
- ]),
127
- (0, file_actions_1.addProviderToStandaloneComponent)(componentPath, "AppTypedTranslocoDirective", [
126
+ ], "providers"),
127
+ (0, file_actions_1.addMetadataToStandaloneComponent)(componentPath, "AppTypedTranslocoDirective", [
128
128
  {
129
129
  symbol: "AppTypedTranslocoDirective",
130
130
  path: "@i18n/app-typed-transloco.directive",