@barcidev/ngx-autogen 0.1.15 → 0.1.16
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 +1 -1
- package/src/common/file-actions.js +40 -10
package/package.json
CHANGED
|
@@ -28,27 +28,57 @@ exports.mergeFilesSmart = mergeFilesSmart;
|
|
|
28
28
|
*/
|
|
29
29
|
function addProviderToStandaloneComponent(componentPath, providerName, imports) {
|
|
30
30
|
return (tree) => {
|
|
31
|
+
var _a;
|
|
31
32
|
const text = tree.read(componentPath);
|
|
32
|
-
if (!text)
|
|
33
|
+
if (!text)
|
|
33
34
|
throw new schematics_2.SchematicsException(`No se encontró el archivo: ${componentPath}`);
|
|
34
|
-
}
|
|
35
35
|
const sourceText = text.toString("utf-8");
|
|
36
36
|
const source = ts.createSourceFile(componentPath, sourceText, ts.ScriptTarget.Latest, true);
|
|
37
37
|
const recorder = tree.beginUpdate(componentPath);
|
|
38
|
-
// 1.
|
|
39
|
-
const providerChanges = (0, ast_utils_1.addSymbolToNgModuleMetadata)(source, componentPath, "providers", providerName, null);
|
|
40
|
-
providerChanges.forEach((change) => {
|
|
41
|
-
if (change instanceof change_1.InsertChange) {
|
|
42
|
-
recorder.insertLeft(change.pos, change.toAdd);
|
|
43
|
-
}
|
|
44
|
-
});
|
|
45
|
-
// 2. Añadir todos los imports necesarios
|
|
38
|
+
// 1. Gestionar Imports
|
|
46
39
|
imports.forEach((item) => {
|
|
47
40
|
const importChange = (0, ast_utils_1.insertImport)(source, componentPath, item.symbol, item.path);
|
|
48
41
|
if (importChange instanceof change_1.InsertChange) {
|
|
49
42
|
recorder.insertLeft(importChange.pos, importChange.toAdd);
|
|
50
43
|
}
|
|
51
44
|
});
|
|
45
|
+
// 2. Buscar el decorador @Component y su propiedad 'providers'
|
|
46
|
+
const componentClass = source.statements.find((s) => {
|
|
47
|
+
var _a;
|
|
48
|
+
return ts.isClassDeclaration(s) &&
|
|
49
|
+
!!((_a = s.modifiers) === null || _a === void 0 ? void 0 : _a.some((m) => m.kind === ts.SyntaxKind.ExportKeyword));
|
|
50
|
+
});
|
|
51
|
+
if (!componentClass)
|
|
52
|
+
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
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
52
82
|
tree.commitUpdate(recorder);
|
|
53
83
|
return tree;
|
|
54
84
|
};
|