@spartacus/schematics
Version:
Spartacus schematics
78 lines • 3.43 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getB2bConfiguration = exports.normalizeConfiguration = exports.getSpartacusProviders = void 0;
const ts_morph_1 = require("ts-morph");
const constants_1 = require("../constants");
const import_utils_1 = require("./import-utils");
const new_module_utils_1 = require("./new-module-utils");
function getSpartacusProviders(sourceFile) {
var _a;
const module = new_module_utils_1.getModule(sourceFile);
if (!module) {
return [];
}
const literal = module.getFirstDescendantByKind(ts_morph_1.ts.SyntaxKind.ObjectLiteralExpression);
const providers = [];
const properties = (_a = literal === null || literal === void 0 ? void 0 : literal.getChildrenOfKind(ts_morph_1.ts.SyntaxKind.PropertyAssignment)) !== null && _a !== void 0 ? _a : [];
properties.forEach((property) => {
if (property.getNameNode().getText() === 'providers' &&
property.getInitializerIfKind(ts_morph_1.ts.SyntaxKind.ArrayLiteralExpression)) {
const initializer = property.getInitializerIfKind(ts_morph_1.ts.SyntaxKind.ArrayLiteralExpression);
initializer === null || initializer === void 0 ? void 0 : initializer.getElements().forEach((element) => {
if (ts_morph_1.Node.isCallExpression(element) || ts_morph_1.Node.isSpreadElement(element)) {
const expression = element.getExpression();
if (ts_morph_1.Node.isIdentifier(expression) &&
import_utils_1.isImportedFromSpartacusLibs(expression)) {
providers.push(element);
}
}
});
}
});
return providers;
}
exports.getSpartacusProviders = getSpartacusProviders;
const EMPTY_SPACE_REG_EXP = /\s+/gm;
function normalizeConfiguration(config) {
let newConfig = typeof config === 'string' ? config : config.getText();
newConfig = newConfig.trim();
if (newConfig.startsWith(constants_1.PROVIDE_CONFIG_FUNCTION)) {
newConfig = newConfig.replace(`${constants_1.PROVIDE_CONFIG_FUNCTION}(`, '');
newConfig = newConfig.substring(0, newConfig.length - 1);
}
newConfig = newConfig.replace(EMPTY_SPACE_REG_EXP, '');
return newConfig;
}
exports.normalizeConfiguration = normalizeConfiguration;
function getB2bConfiguration() {
return [
{
import: [
{
moduleSpecifier: constants_1.SPARTACUS_CORE,
namedImports: [constants_1.PROVIDE_CONFIG_FUNCTION],
},
{
moduleSpecifier: constants_1.SPARTACUS_SETUP,
namedImports: ['defaultB2bOccConfig'],
},
],
content: `provideConfig(defaultB2bOccConfig)`,
},
{
import: [
{
moduleSpecifier: constants_1.SPARTACUS_CORE,
namedImports: [constants_1.PROVIDE_CONFIG_FUNCTION],
},
{
moduleSpecifier: constants_1.SPARTACUS_SETUP,
namedImports: ['defaultB2bCheckoutConfig'],
},
],
content: `provideConfig(defaultB2bCheckoutConfig)`,
},
];
}
exports.getB2bConfiguration = getB2bConfiguration;
//# sourceMappingURL=config-utils.js.map