@sprucelabs/spruce-cli
Version:
Command line interface for building Spruce skills.
134 lines • 7.08 kB
JavaScript
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, } = 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) {
const schemaTypesContents = this.templates.schemasTypes({
schemaTemplateItems: localItems,
fieldTemplateItems,
valueTypes,
globalSchemaNamespace: options.globalSchemaNamespace,
typesTemplate,
});
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 results = [];
for (const item of options.schemaTemplateItems) {
const schemaResults = await this.writeSchema(destinationDir, {
...options,
...item,
});
results.push(...schemaResults);
}
return results;
}
async writeSchema(destinationDir, options) {
const { schemaTemplateItems, fieldTemplateItems, valueTypes, registerBuiltSchemas = true, ...item } = options;
const resolvedDestination = schemaDisk_utility_1.default.resolvePath({
destination: destinationDir,
schema: options.schema,
});
let typesFile = options.typesFile
? spruce_skill_utils_2.diskUtil.resolveRelativePath(path_2.default.dirname(resolvedDestination), options.typesFile)
: undefined;
if (typesFile) {
typesFile = typesFile.replace(path_2.default.extname(typesFile), '');
}
const schemaContents = this.templates.schema({
...item,
registerBuiltSchemas,
schemaTemplateItems,
fieldTemplateItems,
valueTypes,
typesFile,
schemaFile: item.importFrom && options.shouldImportCoreSchemas
? `schema/imported.schema.ts.hbs`
: undefined,
});
return this.writeFileIfChangedMixinResults(resolvedDestination, schemaContents, `${item.schema.description ? `${item.schema.description} ` : ''}This is the schema generated by ${item.id}.builder.ts. AUTOGENERATED. DO NOT EDIT.`);
}
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
;