UNPKG

@sprucelabs/spruce-cli

Version:

Command line interface for building Spruce skills.

155 lines 8.09 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const path_1 = __importDefault(require("path")); const path_2 = __importDefault(require("path")); const spruce_skill_utils_1 = require("@sprucelabs/spruce-skill-utils"); const spruce_skill_utils_2 = require("@sprucelabs/spruce-skill-utils"); const spruce_skill_utils_3 = require("@sprucelabs/spruce-skill-utils"); const SpruceError_1 = __importDefault(require("../../../errors/SpruceError")); const AbstractWriter_1 = __importDefault(require("../../../writers/AbstractWriter")); const schemaDisk_utility_1 = __importDefault(require("../utilities/schemaDisk.utility")); class SchemaWriter extends AbstractWriter_1.default { fieldTemplates = [ { filename: 'fields.types.ts', templateFuncName: 'fieldsTypes', description: 'All the interfaces generated for every type of schema field (text, number, address, etc).', }, { filename: 'fieldClassMap.ts', templateFuncName: 'fieldClassMap', description: 'An object that is injected into the FieldFactory and ensures 3rd party fields are integrated.', }, ]; async writeBuilder(destinationDir, options) { this.ui.startLoading('Writing builder...'); const filename = `${options.nameCamel}.builder.ts`; const resolvedBuilderDestination = options.shouldEnableVersioning === false ? path_2.default.resolve(destinationDir, filename) : spruce_skill_utils_1.versionUtil.resolveNewLatestPath(destinationDir, options.version ?? spruce_skill_utils_3.LATEST_HANDLEBARS, filename); if (spruce_skill_utils_2.diskUtil.doesFileExist(resolvedBuilderDestination)) { throw new SpruceError_1.default({ code: 'SCHEMA_EXISTS', schemaId: options.nameCamel, destination: destinationDir, }); } const builderContent = this.templates.schemaBuilder(options); const results = await this.writeFileIfChangedMixinResults(resolvedBuilderDestination, builderContent, 'The source of truth for building your schemas and their types. Run spruce sync.errors when you change this.'); return results; } async writeFieldTypes(destinationDir, options) { this.ui.startLoading('Checking schema field types...'); const { fieldTemplateItems } = options; let results = []; for (const fileAndFunc of this.fieldTemplates) { const { filename, templateFuncName, description } = fileAndFunc; const resolvedDestination = path_1.default.join(destinationDir, 'fields', filename); const contents = this.templates[templateFuncName]({ fieldTemplateItems, }); results = await this.writeFileIfChangedMixinResults(resolvedDestination, contents, description, results); } return results; } async writeSchemasAndTypes(destinationDirOrFilename, options) { const { fieldTemplateItems, schemaTemplateItems, valueTypes, typesTemplate, language, } = options; this.isLintEnabled = false; const resolvedTypesDestination = this.resolveFilenameWithFallback(destinationDirOrFilename, spruce_skill_utils_3.DEFAULT_SCHEMA_TYPES_FILENAME); let results = []; this.ui.startLoading('Generating schema types...'); const localItems = schemaTemplateItems.filter((i) => !i.importFrom); if (localItems.length > 0) { let schemaTypesContents = this.templates.schemasTypes({ language: language === 'go' ? 'go' : 'typescript', schemaTemplateItems: localItems, fieldTemplateItems, valueTypes, globalSchemaNamespace: options.globalSchemaNamespace, typesTemplate, }); if (language === 'go') { const referencesSchemaType = schemaTypesContents.split('SpruceSchema.'); if (referencesSchemaType.length === 1) { schemaTypesContents = schemaTypesContents.replace('import SpruceSchema "github.com/sprucelabsai-community/spruce-schema/v32/pkg/fields"', ''); } } results = await this.writeFileIfChangedMixinResults(resolvedTypesDestination, schemaTypesContents, 'Namespace for accessing all your schemas. Type `SpruceSchemas` in your IDE to get started. ⚡️'); } this.ui.startLoading(`Checking ${schemaTemplateItems.length} schemas for changes...`); const allSchemaResults = await this.writeAllSchemas(path_2.default.dirname(resolvedTypesDestination), { ...options, typesFile: resolvedTypesDestination, }); results.push(...allSchemaResults); this.isLintEnabled = true; await this.lint(destinationDirOrFilename); return results; } async writeAllSchemas(destinationDir, options) { const { language } = options; const results = []; for (const item of options.schemaTemplateItems) { if (language === 'ts' || (language === 'go' && !item.importFrom)) { const schemaResults = await this.writeSchema(destinationDir, { ...options, ...item, }); results.push(...schemaResults); } } return results; } async writeSchema(destinationDir, options) { let { schemaTemplateItems, fieldTemplateItems, valueTypes, registerBuiltSchemas = true, language, schema, typesFile, ...item } = options; const resolvedDestination = schemaDisk_utility_1.default.resolvePath({ destination: destinationDir, schema, language, }); typesFile = this.resolveTypesFile(typesFile, resolvedDestination); let templateFile = item.importFrom && options.shouldImportCoreSchemas ? `schema/imported.schema.ts.hbs` : undefined; if (language === 'go') { templateFile = 'schema/schema.go.hbs'; } const schemaContents = this.templates.schema({ ...item, registerBuiltSchemas, schemaTemplateItems, fieldTemplateItems, valueTypes, typesFile, schema, language: language === 'go' ? 'go' : 'typescript', schemaFile: templateFile, }); return this.writeFileIfChangedMixinResults(resolvedDestination, schemaContents, `${schema.description ? `${schema.description} ` : ''}This is the schema generated by ${item.id}.builder.ts. AUTOGENERATED. DO NOT EDIT.`); } resolveTypesFile(typesFile, resolvedDestination) { let resolvedType = typesFile ? spruce_skill_utils_2.diskUtil.resolveRelativePath(path_2.default.dirname(resolvedDestination), typesFile) : undefined; if (resolvedType) { resolvedType = resolvedType.replace(path_2.default.extname(resolvedType), ''); } return resolvedType; } async writeValueTypes(destinationDir, options) { const contents = this.templates.valueTypes(options); const destination = path_2.default.join(destinationDir, 'tmp', 'valueType.tmp.ts'); return this.writeFileIfChangedMixinResults(destination, contents, 'For constructing what goes to the right of the : after each property in the interface.'); } writePlugin(cwd) { const destination = spruce_skill_utils_2.diskUtil.resolveHashSprucePath(cwd, 'features', 'schema.plugin.ts'); const pluginContents = this.templates.schemaPlugin(); const results = this.writeFileIfChangedMixinResults(destination, pluginContents, 'Enable schema support in your skill.'); return results; } } exports.default = SchemaWriter; //# sourceMappingURL=SchemaWriter.js.map