@barcidev/ngx-autogen 0.1.26 → 0.1.27
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.d.ts +1 -1
- package/src/transloco/index.js +120 -125
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@barcidev/ngx-autogen",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.27",
|
|
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.10",
|
|
54
54
|
"@schematics/angular": "^21.2.0",
|
|
55
55
|
"typescript": "~5.9.2"
|
|
56
56
|
},
|
package/src/transloco/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Rule } from "@angular-devkit/schematics";
|
|
2
2
|
import { TranslocoSchemaOptions } from "./scheme";
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
4
|
+
* Schematic principal para configurar Transloco en un componente específico
|
|
5
5
|
*/
|
|
6
6
|
export declare function transloco(options: TranslocoSchemaOptions): Rule;
|
package/src/transloco/index.js
CHANGED
|
@@ -15,139 +15,134 @@ const tasks_1 = require("@angular-devkit/schematics/tasks");
|
|
|
15
15
|
const dependencies_1 = require("@schematics/angular/utility/dependencies");
|
|
16
16
|
const workspace_1 = require("@schematics/angular/utility/workspace");
|
|
17
17
|
const path_1 = require("path");
|
|
18
|
-
const jsonc_parser_1 = require("jsonc-parser");
|
|
19
18
|
const file_actions_1 = require("../common/file-actions");
|
|
20
19
|
const pluralize_1 = require("../common/pluralize");
|
|
21
20
|
/**
|
|
22
|
-
*
|
|
21
|
+
* Schematic principal para configurar Transloco en un componente específico
|
|
23
22
|
*/
|
|
24
23
|
function transloco(options) {
|
|
25
|
-
return (tree) => __awaiter(this, void 0, void 0, function* () {
|
|
26
|
-
const workspace = yield (0, workspace_1.getWorkspace)(tree);
|
|
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
|
|
37
|
-
const { path, componentFile, componentName } = resolveComponentContext(tree, options);
|
|
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
|
|
41
|
-
return (0, schematics_1.chain)([
|
|
42
|
-
setupGlobalConfig(projectName),
|
|
43
|
-
generateI18nFiles(finalOptions),
|
|
44
|
-
updateAppI18nTypeRule(finalOptions), // Pasamos las opciones con el root
|
|
45
|
-
registerProviderInComponent(finalOptions, componentFile),
|
|
46
|
-
]);
|
|
47
|
-
});
|
|
48
|
-
}
|
|
49
|
-
function resolveComponentContext(tree, options) {
|
|
50
|
-
const fullPath = process.cwd();
|
|
51
|
-
const srcIndex = fullPath.lastIndexOf("src");
|
|
52
|
-
const path = (0, path_1.normalize)(srcIndex !== -1
|
|
53
|
-
? fullPath.substring(srcIndex)
|
|
54
|
-
: (0, path_1.join)((0, path_1.normalize)("src"), "app"));
|
|
55
|
-
const directory = tree.getDir(path);
|
|
56
|
-
const componentFile = directory.subfiles.find((f) => f.endsWith(".component.ts")) ||
|
|
57
|
-
directory.subfiles.find((f) => f.endsWith(".ts"));
|
|
58
|
-
if (!componentFile && !options.name) {
|
|
59
|
-
throw new schematics_1.SchematicsException("❌ No se encontró un componente y no se proveyó un --name.");
|
|
60
|
-
}
|
|
61
|
-
const componentName = componentFile
|
|
62
|
-
? componentFile.replace(".component.ts", "").replace(".ts", "")
|
|
63
|
-
: options.name;
|
|
64
|
-
return { path, componentFile, componentName };
|
|
65
|
-
}
|
|
66
|
-
/**
|
|
67
|
-
* Ajuste en setupGlobalConfig para recibir el proyecto
|
|
68
|
-
*/
|
|
69
|
-
function setupGlobalConfig(projectName) {
|
|
70
|
-
return (tree, context) => {
|
|
24
|
+
return (tree, _context) => __awaiter(this, void 0, void 0, function* () {
|
|
71
25
|
var _a;
|
|
72
|
-
|
|
73
|
-
const
|
|
74
|
-
if (
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
type: dependencies_1.NodeDependencyType.Default,
|
|
79
|
-
name: "@jsverse/transloco",
|
|
80
|
-
version: "^8.0.0",
|
|
81
|
-
});
|
|
82
|
-
context.addTask(new tasks_1.NodePackageInstallTask());
|
|
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
|
-
});
|
|
87
|
-
};
|
|
88
|
-
}
|
|
89
|
-
function generateI18nFiles(options) {
|
|
90
|
-
return (0, schematics_1.mergeWith)((0, schematics_1.apply)((0, schematics_1.url)("./files/component"), [
|
|
91
|
-
(0, schematics_1.applyTemplates)(Object.assign(Object.assign(Object.assign({}, schematics_1.strings), options), { pluralize: (word) => options.lang === "es" ? (0, pluralize_1.pluralizeEs)(word) : (0, pluralize_1.pluralizeEn)(word) })),
|
|
92
|
-
(0, schematics_1.move)(options.path),
|
|
93
|
-
]));
|
|
94
|
-
}
|
|
95
|
-
/** * Ajuste en updateAppI18nType para usar el root detectado
|
|
96
|
-
*/
|
|
97
|
-
function updateAppI18nTypeRule(options) {
|
|
98
|
-
return (tree, context) => {
|
|
99
|
-
// IMPORTANTE: Construimos la ruta usando el root detectado dinámicamente
|
|
100
|
-
const path = `${options.projectRoot}/app/i18n/app.i18n.ts`;
|
|
101
|
-
const buffer = tree.read(path);
|
|
102
|
-
if (!buffer) {
|
|
103
|
-
context.logger.warn(`⚠️ No se encontró el archivo ${path}. Asegúrate de que ng-add se ejecutó primero.`);
|
|
104
|
-
return tree;
|
|
26
|
+
// --- 1. CONFIGURACIÓN INICIAL Y WORKSPACE ---
|
|
27
|
+
const workspace = yield (0, workspace_1.getWorkspace)(tree);
|
|
28
|
+
if (!options.project) {
|
|
29
|
+
options.project =
|
|
30
|
+
workspace.extensions.defaultProject ||
|
|
31
|
+
Array.from(workspace.projects.keys())[0];
|
|
105
32
|
}
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
// Lógica de importación
|
|
110
|
-
const importStatement = `import { ${name} } from '${importPath}';\n`;
|
|
111
|
-
if (!content.includes(importPath)) {
|
|
112
|
-
content = importStatement + content;
|
|
33
|
+
const project = workspace.projects.get(options.project);
|
|
34
|
+
if (!project) {
|
|
35
|
+
throw new schematics_1.SchematicsException(`El proyecto "${options.project}" no existe.`);
|
|
113
36
|
}
|
|
114
|
-
//
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
const
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
// Reemplazamos el placeholder por la referencia real de TS
|
|
129
|
-
newObjectStr = newObjectStr.replace(`"${placeholder}"`, valueToInject);
|
|
130
|
-
const newContent = content.replace(oldObjectStr, newObjectStr);
|
|
131
|
-
tree.overwrite(path, newContent);
|
|
132
|
-
context.logger.info(`✅ Registro de tipos actualizado en app.i18n.ts`);
|
|
37
|
+
// --- 2. DETERMINACIÓN DE RUTAS Y DETECCIÓN DE COMPONENTE ---
|
|
38
|
+
// Resolvemos el path relativo (src/app/...) basado en el directorio de ejecución
|
|
39
|
+
const fullPath = process.cwd();
|
|
40
|
+
const srcIndex = fullPath.lastIndexOf("src");
|
|
41
|
+
const relativePath = srcIndex !== -1
|
|
42
|
+
? fullPath.substring(srcIndex)
|
|
43
|
+
: (0, path_1.join)((0, path_1.normalize)("src"), "app");
|
|
44
|
+
options.path = (0, path_1.normalize)(relativePath);
|
|
45
|
+
const directory = tree.getDir(options.path);
|
|
46
|
+
// Buscamos el archivo .ts principal del directorio
|
|
47
|
+
const componentFile = directory.subfiles.find((f) => f.endsWith(".component.ts")) ||
|
|
48
|
+
directory.subfiles.find((f) => f.endsWith(".ts"));
|
|
49
|
+
if (!componentFile && !options.name) {
|
|
50
|
+
throw new schematics_1.SchematicsException("❌ No se encontró un archivo .ts en este directorio y no se proveyó un --name.");
|
|
133
51
|
}
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
52
|
+
// Normalizamos el nombre (ej. mi-componente.component -> mi-componente)
|
|
53
|
+
options.name = componentFile
|
|
54
|
+
? componentFile.replace(".component.ts", "").replace(".ts", "")
|
|
55
|
+
: options.name;
|
|
56
|
+
const rules = [
|
|
57
|
+
(0, schematics_1.externalSchematic)("@barcidev/typed-transloco", "ng-add", {}),
|
|
58
|
+
];
|
|
59
|
+
// --- 3. CONFIGURACIÓN GLOBAL (IDEMPOTENTE) ---
|
|
60
|
+
const appConfigPath = "src/app/app.config.ts";
|
|
61
|
+
const appConfigContent = ((_a = tree.read(appConfigPath)) === null || _a === void 0 ? void 0 : _a.toString()) || "";
|
|
62
|
+
if (!appConfigContent.includes("provideTransloco")) {
|
|
63
|
+
_context.logger.info("📦 Transloco no detectado. Configurando globalmente...");
|
|
64
|
+
// Dependencias y tarea de instalación
|
|
65
|
+
rules.push((host) => {
|
|
66
|
+
(0, dependencies_1.addPackageJsonDependency)(host, {
|
|
67
|
+
type: dependencies_1.NodeDependencyType.Default,
|
|
68
|
+
name: "@jsverse/transloco",
|
|
69
|
+
version: "^7.0.0",
|
|
70
|
+
});
|
|
71
|
+
_context.addTask(new tasks_1.NodePackageInstallTask());
|
|
72
|
+
return host;
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
// --- 4. GENERACIÓN DE ARCHIVOS I18N DEL COMPONENTE ---
|
|
76
|
+
rules.push((0, schematics_1.mergeWith)((0, schematics_1.apply)((0, schematics_1.url)("./files/component"), [
|
|
77
|
+
(0, schematics_1.applyTemplates)(Object.assign(Object.assign(Object.assign({}, schematics_1.strings), options), { pluralize: (word) => options.lang === "es" ? (0, pluralize_1.pluralizeEs)(word) : (0, pluralize_1.pluralizeEn)(word) })),
|
|
78
|
+
(0, schematics_1.move)(options.path),
|
|
79
|
+
])));
|
|
80
|
+
// --- 5. REGISTRO DEL SCOPE EN EL COMPONENTE ---
|
|
81
|
+
if (componentFile) {
|
|
82
|
+
const componentPath = (0, path_1.join)(options.path, componentFile);
|
|
83
|
+
const i18nConstantName = `${schematics_1.strings.camelize(options.name)}I18n`;
|
|
84
|
+
rules.push((0, file_actions_1.addProviderToStandaloneComponent)(componentPath, `provideTranslocoScopeWrapper(${i18nConstantName})`, [
|
|
85
|
+
{
|
|
86
|
+
symbol: `provideTranslocoScopeWrapper`,
|
|
87
|
+
path: `@barcidev/typed-transloco`,
|
|
88
|
+
},
|
|
89
|
+
{
|
|
90
|
+
symbol: i18nConstantName,
|
|
91
|
+
path: `./${schematics_1.strings.dasherize(options.name)}.i18n`,
|
|
92
|
+
},
|
|
93
|
+
]));
|
|
94
|
+
}
|
|
95
|
+
return (0, schematics_1.chain)(rules);
|
|
96
|
+
});
|
|
152
97
|
}
|
|
98
|
+
/*function updateAppI18nType(
|
|
99
|
+
root: string,
|
|
100
|
+
name: string,
|
|
101
|
+
importPath: string,
|
|
102
|
+
): Rule {
|
|
103
|
+
return (tree: Tree, context: SchematicContext) => {
|
|
104
|
+
const path = `${root}/app/i18n/app.i18n.ts`;
|
|
105
|
+
const buffer = tree.read(path);
|
|
106
|
+
|
|
107
|
+
if (!buffer) {
|
|
108
|
+
context.logger.warn(`⚠️ No se encontró el archivo ${path}`);
|
|
109
|
+
return tree;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
let content = buffer.toString();
|
|
113
|
+
const importStatement = `import { ${name} } from '${importPath}';\n`;
|
|
114
|
+
|
|
115
|
+
// 1. AGREGAR EL IMPORT (si no existe ya)
|
|
116
|
+
if (!content.includes(importPath)) {
|
|
117
|
+
content = importStatement + content;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// 2. ACTUALIZAR EL OBJETO appI18n
|
|
121
|
+
const options: ModificationOptions = {
|
|
122
|
+
formattingOptions: { insertSpaces: true, tabSize: 2 },
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
// Buscamos el bloque del objeto appI18n
|
|
126
|
+
const regex = /const\s+appI18n\s*=\s*(\{[\s\S]*?\});/;
|
|
127
|
+
const match = content.match(regex);
|
|
128
|
+
|
|
129
|
+
if (match) {
|
|
130
|
+
const oldObjectStr = match[1];
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
const edits = modify(
|
|
134
|
+
oldObjectStr,
|
|
135
|
+
[name],
|
|
136
|
+
[name + ".translations[en-US]"],
|
|
137
|
+
options,
|
|
138
|
+
);
|
|
139
|
+
let newObjectStr = applyEdits(oldObjectStr, edits);
|
|
140
|
+
|
|
141
|
+
const newContent = content.replace(oldObjectStr, newObjectStr);
|
|
142
|
+
tree.overwrite(path, newContent);
|
|
143
|
+
|
|
144
|
+
context.logger.info(`✅ Importado ${name}"`);
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
}*/
|
|
153
148
|
//# sourceMappingURL=index.js.map
|