@sprucelabs/spruce-templates
Version:
Templates used for code generation in skills!
40 lines (39 loc) • 1.97 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const schema_1 = require("@sprucelabs/schema");
const handlebars_1 = __importDefault(require("handlebars"));
const lodash_1 = require("lodash");
handlebars_1.default.registerHelper('importRelatedSchemas', function (schema, options) {
var _a;
if (!schema) {
throw new Error('importRelatedSchemas needs a Schema as the first arg');
}
const { data: { root }, } = options;
const schemaTemplateItems = root === null || root === void 0 ? void 0 : root.schemaTemplateItems;
if (!schemaTemplateItems) {
throw new Error('importRelatedSchemas needs schemaTemplateItems passed to parent template');
}
const fields = schema.dynamicFieldSignature
? [schema.dynamicFieldSignature]
: Object.values((_a = schema.fields) !== null && _a !== void 0 ? _a : {});
const imports = [];
fields.forEach((field) => {
if (field.type === 'schema') {
const related = schema_1.SchemaField.mapFieldDefinitionToSchemaIdsWithVersion(field);
related.forEach((idWithVersion) => {
const matched = schemaTemplateItems.find((t) => t.id === idWithVersion.id &&
t.schema.version === idWithVersion.version &&
t.schema.namespace === idWithVersion.namespace);
if (matched) {
imports.push(`import ${matched.nameCamel}Schema${matched.schema.version
? `_${matched.schema.version}`
: ''} from '${matched.destinationDir}/${(0, lodash_1.camelCase)(matched.namespace)}${matched.schema.version ? `/${matched.schema.version}` : ''}/${matched.id}.schema'`);
}
});
}
});
return (0, lodash_1.uniq)(imports).join('\n');
});