@barcidev/ngx-autogen 0.1.24 → 0.1.26
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 +2 -2
- package/src/transloco/index.js +43 -40
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@barcidev/ngx-autogen",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.26",
|
|
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.
|
|
53
|
+
"@barcidev/typed-transloco": "^0.0.9",
|
|
54
54
|
"@schematics/angular": "^21.2.0",
|
|
55
55
|
"typescript": "~5.9.2"
|
|
56
56
|
},
|
package/src/transloco/index.js
CHANGED
|
@@ -24,30 +24,28 @@ const pluralize_1 = require("../common/pluralize");
|
|
|
24
24
|
function transloco(options) {
|
|
25
25
|
return (tree) => __awaiter(this, void 0, void 0, function* () {
|
|
26
26
|
const workspace = yield (0, workspace_1.getWorkspace)(tree);
|
|
27
|
-
|
|
27
|
+
// 1. Obtención correcta del ROOT del proyecto
|
|
28
|
+
const projectName = options.project ||
|
|
29
|
+
workspace.extensions.defaultProject ||
|
|
30
|
+
Array.from(workspace.projects.keys())[0];
|
|
31
|
+
const project = workspace.projects.get(projectName);
|
|
32
|
+
if (!project)
|
|
33
|
+
throw new schematics_1.SchematicsException(`El proyecto "${projectName}" no existe.`);
|
|
34
|
+
// Usamos project.sourceRoot para ir a 'src' (o lo que esté configurado)
|
|
35
|
+
const projectSourceRoot = project.sourceRoot || "src";
|
|
36
|
+
// 2. Contexto del componente
|
|
28
37
|
const { path, componentFile, componentName } = resolveComponentContext(tree, options);
|
|
29
|
-
|
|
30
|
-
|
|
38
|
+
const finalOptions = Object.assign(Object.assign({}, options), { project: projectName, path, name: componentName, projectRoot: projectSourceRoot });
|
|
39
|
+
// 3. RETORNO DE CADENA LIMPIA
|
|
40
|
+
// El secreto es que todas las reglas sean secuenciales y no disparen procesos paralelos
|
|
31
41
|
return (0, schematics_1.chain)([
|
|
32
|
-
setupGlobalConfig(),
|
|
42
|
+
setupGlobalConfig(projectName),
|
|
33
43
|
generateI18nFiles(finalOptions),
|
|
34
|
-
|
|
44
|
+
updateAppI18nTypeRule(finalOptions), // Pasamos las opciones con el root
|
|
35
45
|
registerProviderInComponent(finalOptions, componentFile),
|
|
36
46
|
]);
|
|
37
47
|
});
|
|
38
48
|
}
|
|
39
|
-
/**
|
|
40
|
-
* --- FUNCIONES HELPER (PURAS O DE CONFIGURACIÓN) ---
|
|
41
|
-
*/
|
|
42
|
-
function getProjectConfiguration(workspace, options) {
|
|
43
|
-
const projectName = options.project ||
|
|
44
|
-
workspace.extensions.defaultProject ||
|
|
45
|
-
Array.from(workspace.projects.keys())[0];
|
|
46
|
-
const project = workspace.projects.get(projectName);
|
|
47
|
-
if (!project)
|
|
48
|
-
throw new schematics_1.SchematicsException(`El proyecto "${projectName}" no existe.`);
|
|
49
|
-
return { project, projectName };
|
|
50
|
-
}
|
|
51
49
|
function resolveComponentContext(tree, options) {
|
|
52
50
|
const fullPath = process.cwd();
|
|
53
51
|
const srcIndex = fullPath.lastIndexOf("src");
|
|
@@ -66,25 +64,26 @@ function resolveComponentContext(tree, options) {
|
|
|
66
64
|
return { path, componentFile, componentName };
|
|
67
65
|
}
|
|
68
66
|
/**
|
|
69
|
-
*
|
|
67
|
+
* Ajuste en setupGlobalConfig para recibir el proyecto
|
|
70
68
|
*/
|
|
71
|
-
function setupGlobalConfig() {
|
|
69
|
+
function setupGlobalConfig(projectName) {
|
|
72
70
|
return (tree, context) => {
|
|
73
71
|
var _a;
|
|
74
|
-
const appConfigPath = "src/app/app.config.ts";
|
|
72
|
+
const appConfigPath = "src/app/app.config.ts"; // Opcional: podrías usar el root aquí también
|
|
75
73
|
const content = ((_a = tree.read(appConfigPath)) === null || _a === void 0 ? void 0 : _a.toString()) || "";
|
|
76
74
|
if (content.includes("provideTransloco"))
|
|
77
75
|
return tree;
|
|
78
|
-
|
|
76
|
+
// Solo añadimos dependencias si no existen
|
|
79
77
|
(0, dependencies_1.addPackageJsonDependency)(tree, {
|
|
80
78
|
type: dependencies_1.NodeDependencyType.Default,
|
|
81
79
|
name: "@jsverse/transloco",
|
|
82
|
-
version: "^
|
|
80
|
+
version: "^8.0.0",
|
|
83
81
|
});
|
|
84
82
|
context.addTask(new tasks_1.NodePackageInstallTask());
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
83
|
+
// Devolvemos el schematic externo como parte de la cadena
|
|
84
|
+
return (0, schematics_1.externalSchematic)("@barcidev/typed-transloco", "ng-add", {
|
|
85
|
+
project: projectName,
|
|
86
|
+
});
|
|
88
87
|
};
|
|
89
88
|
}
|
|
90
89
|
function generateI18nFiles(options) {
|
|
@@ -93,40 +92,44 @@ function generateI18nFiles(options) {
|
|
|
93
92
|
(0, schematics_1.move)(options.path),
|
|
94
93
|
]));
|
|
95
94
|
}
|
|
96
|
-
|
|
95
|
+
/** * Ajuste en updateAppI18nType para usar el root detectado
|
|
96
|
+
*/
|
|
97
|
+
function updateAppI18nTypeRule(options) {
|
|
97
98
|
return (tree, context) => {
|
|
98
|
-
|
|
99
|
+
// IMPORTANTE: Construimos la ruta usando el root detectado dinámicamente
|
|
100
|
+
const path = `${options.projectRoot}/app/i18n/app.i18n.ts`;
|
|
99
101
|
const buffer = tree.read(path);
|
|
100
102
|
if (!buffer) {
|
|
101
|
-
context.logger.warn(`⚠️ No se encontró el archivo ${path}
|
|
103
|
+
context.logger.warn(`⚠️ No se encontró el archivo ${path}. Asegúrate de que ng-add se ejecutó primero.`);
|
|
102
104
|
return tree;
|
|
103
105
|
}
|
|
104
106
|
let content = buffer.toString();
|
|
107
|
+
const name = `${schematics_1.strings.camelize(options.name)}I18n`;
|
|
108
|
+
const importPath = `${options.path}/${schematics_1.strings.dasherize(options.name)}.i18n`;
|
|
109
|
+
// Lógica de importación
|
|
105
110
|
const importStatement = `import { ${name} } from '${importPath}';\n`;
|
|
106
|
-
// 1. AGREGAR EL IMPORT (si no existe ya)
|
|
107
111
|
if (!content.includes(importPath)) {
|
|
108
112
|
content = importStatement + content;
|
|
109
113
|
}
|
|
110
|
-
//
|
|
111
|
-
const
|
|
114
|
+
// Lógica de JSONC-Parser para evitar Overlapping
|
|
115
|
+
const modificationOptions = {
|
|
112
116
|
formattingOptions: { insertSpaces: true, tabSize: 2 },
|
|
113
117
|
};
|
|
114
|
-
// Buscamos el bloque del objeto appI18n
|
|
115
118
|
const regex = /const\s+appI18n\s*=\s*(\{[\s\S]*?\});/;
|
|
116
119
|
const match = content.match(regex);
|
|
117
120
|
if (match) {
|
|
118
121
|
const oldObjectStr = match[1];
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
*/
|
|
125
|
-
const edits = (0, jsonc_parser_1.modify)(oldObjectStr, [name], [name + ".translations[en-US]"], options);
|
|
122
|
+
// El valor que quieres inyectar: name.translations['en-US']
|
|
123
|
+
const valueToInject = `${name}.translations['en-US']`;
|
|
124
|
+
// TRUCO: Usamos un placeholder para que jsonc-parser no ponga comillas de string
|
|
125
|
+
const placeholder = `__REF__${name}`;
|
|
126
|
+
const edits = (0, jsonc_parser_1.modify)(oldObjectStr, [schematics_1.strings.camelize(options.name)], placeholder, modificationOptions);
|
|
126
127
|
let newObjectStr = (0, jsonc_parser_1.applyEdits)(oldObjectStr, edits);
|
|
128
|
+
// Reemplazamos el placeholder por la referencia real de TS
|
|
129
|
+
newObjectStr = newObjectStr.replace(`"${placeholder}"`, valueToInject);
|
|
127
130
|
const newContent = content.replace(oldObjectStr, newObjectStr);
|
|
128
131
|
tree.overwrite(path, newContent);
|
|
129
|
-
context.logger.info(`✅
|
|
132
|
+
context.logger.info(`✅ Registro de tipos actualizado en app.i18n.ts`);
|
|
130
133
|
}
|
|
131
134
|
return tree;
|
|
132
135
|
};
|