@rxap/schematics-ts-morph
Version:
This package provides utilities for manipulating TypeScript code using ts-morph, particularly for Angular and NestJS projects. It offers functions to add, coerce, and modify code elements like classes, methods, decorators, and imports. The package also in
24 lines • 1.34 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.AddVariableProvider = AddVariableProvider;
const ts_morph_1 = require("ts-morph");
const add_provider_to_array_1 = require("./add-provider-to-array");
const coerce_variable_declaration_1 = require("./coerce-variable-declaration");
const coerce_imports_1 = require("./ts-morph/coerce-imports");
function AddVariableProvider(sourceFile, variableName, providerObject, structures = [], overwrite = false) {
(0, coerce_imports_1.CoerceImports)(sourceFile, structures);
const variableDeclaration = (0, coerce_variable_declaration_1.CoerceVariableDeclaration)(sourceFile, variableName, {
initializer: '[]',
type: 'Provider[]',
});
(0, coerce_imports_1.CoerceImports)(sourceFile, {
namedImports: ['Provider'],
moduleSpecifier: '@angular/core',
});
const providerArray = variableDeclaration.getInitializer();
if (!(providerArray === null || providerArray === void 0 ? void 0 : providerArray.isKind(ts_morph_1.SyntaxKind.ArrayLiteralExpression))) {
throw new Error(`The variable '${variableName}' initializer is not an array literal expression`);
}
(0, add_provider_to_array_1.AddProviderToArray)(providerObject, providerArray, overwrite);
}
//# sourceMappingURL=add-variable-provider.js.map