@barcidev/ngx-autogen 0.1.34 → 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.34",
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": {
@@ -50,7 +50,7 @@
50
50
  "dependencies": {
51
51
  "@angular-devkit/core": "^21.2.0",
52
52
  "@angular-devkit/schematics": "^21.2.0",
53
- "@barcidev/typed-transloco": "^0.0.11",
53
+ "@barcidev/typed-transloco": "^0.0.12",
54
54
  "@schematics/angular": "^21.2.0",
55
55
  "typescript": "~5.9.2"
56
56
  },
@@ -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;
@@ -34,13 +34,9 @@ function transloco(options) {
34
34
  const finalOptions = Object.assign(Object.assign({}, options), { name: componentName, path: path, project: projectName, projectRoot: projectRoot });
35
35
  return (0, schematics_1.chain)([
36
36
  ensureExternalConfig(),
37
- () => {
38
- return (0, schematics_1.chain)([
39
- generateI18nFiles(finalOptions),
40
- updateAppI18nTypeRule(finalOptions),
41
- registerProviderInComponent(finalOptions, componentFile),
42
- ]);
43
- },
37
+ generateI18nFiles(finalOptions),
38
+ updateAppI18nTypeRule(finalOptions),
39
+ registerProviderInComponent(finalOptions, componentFile),
44
40
  ]);
45
41
  });
46
42
  }
@@ -86,7 +82,7 @@ function updateAppI18nTypeRule(options) {
86
82
  const camelName = schematics_1.strings.camelize(options.name);
87
83
  const i18nConstantName = `${camelName}I18n`;
88
84
  // Inyección de Import
89
- const importStatement = `import { ${i18nConstantName} } from '${options.path.replace("src/app/", "./")}/${schematics_1.strings.dasherize(options.name)}.i18n';\n`;
85
+ const importStatement = `import { ${i18nConstantName} } from '${options.path}/${schematics_1.strings.dasherize(options.name)}.i18n';\n`;
90
86
  if (!content.includes(i18nConstantName)) {
91
87
  content = importStatement + content;
92
88
  }
@@ -117,15 +113,23 @@ function registerProviderInComponent(options, componentFile) {
117
113
  return (tree) => tree;
118
114
  const componentPath = (0, path_1.join)(options.path, componentFile);
119
115
  const i18nConstantName = `${schematics_1.strings.camelize(options.name)}I18n`;
120
- return (0, file_actions_1.addProviderToStandaloneComponent)(componentPath, `provideTranslocoScopeWrapper(${i18nConstantName})`, [
121
- {
122
- symbol: "provideTranslocoScopeWrapper",
123
- path: "@barcidev/typed-transloco",
124
- },
125
- {
126
- symbol: i18nConstantName,
127
- path: `./${schematics_1.strings.dasherize(options.name)}.i18n`,
128
- },
116
+ return (0, schematics_1.chain)([
117
+ (0, file_actions_1.addMetadataToStandaloneComponent)(componentPath, `provideTranslocoScopeWrapper(${i18nConstantName})`, [
118
+ {
119
+ symbol: "provideTranslocoScopeWrapper",
120
+ path: "@barcidev/typed-transloco",
121
+ },
122
+ {
123
+ symbol: i18nConstantName,
124
+ path: `./${schematics_1.strings.dasherize(options.name)}.i18n`,
125
+ },
126
+ ], "providers"),
127
+ (0, file_actions_1.addMetadataToStandaloneComponent)(componentPath, "AppTypedTranslocoDirective", [
128
+ {
129
+ symbol: "AppTypedTranslocoDirective",
130
+ path: "@i18n/app-typed-transloco.directive",
131
+ },
132
+ ]),
129
133
  ]);
130
134
  }
131
135
  /**