@sprucelabs/spruce-cli
Version:
Command line interface for building Spruce skills.
129 lines • 5.69 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const globby_1 = __importDefault(require("@sprucelabs/globby"));
const schema_1 = require("@sprucelabs/schema");
const spruce_skill_utils_1 = require("@sprucelabs/spruce-skill-utils");
const lodash_1 = require("lodash");
const EventTemplateItemBuilder_1 = __importDefault(require("../../../templateItemBuilders/EventTemplateItemBuilder"));
const validateAndNormalize_utility_1 = __importDefault(require("../../validateAndNormalize.utility"));
class EventContractBuilder {
optionsSchema;
ui;
eventWriter;
cwd;
eventStore;
skillStore;
dependencyService;
constructor(options) {
this.optionsSchema = options.optionsSchema;
this.ui = options.ui;
this.eventWriter = options.eventGenerator;
this.cwd = options.cwd;
this.eventStore = options.eventStore;
this.skillStore = options.skillStore;
this.dependencyService = options.dependencyService;
}
async fetchAndWriteContracts(options) {
const { shouldOnlySyncRemoteEvents, ...rest } = options;
const normalizedOptions = validateAndNormalize_utility_1.default.validateAndNormalize(this.optionsSchema, rest);
const { contractDestinationDir } = normalizedOptions;
const resolvedDestination = spruce_skill_utils_1.diskUtil.resolvePath(this.cwd, contractDestinationDir);
const { errors, schemaTemplateItems, eventContractTemplateItems } = await this.fetchAndBuildTemplateItems({
shouldSyncOnlyCoreEvents: options.shouldSyncOnlyCoreEvents ?? false,
eventBuilderFile: normalizedOptions.eventBuilderFile,
shouldOnlySyncRemoteEvents,
});
if (errors && errors?.length > 0) {
return {
errors,
};
}
this.ui.startLoading('Generating contracts...');
const files = await this.eventWriter.writeContracts(resolvedDestination, {
...normalizedOptions,
eventContractTemplateItems,
schemaTemplateItems,
shouldImportCoreEvents: !options.shouldSyncOnlyCoreEvents,
});
await this.deleteOrphanedEventContracts(resolvedDestination, files.map((a) => a.path));
return {
files,
};
}
async deleteOrphanedEventContracts(lookupDir, existingContracts) {
const matches = await (0, globby_1.default)(lookupDir + '/**/*.contract.ts');
const diff = matches.filter((m) => !existingContracts.includes(m));
diff.forEach((f) => spruce_skill_utils_1.diskUtil.deleteFile(f));
spruce_skill_utils_1.diskUtil.deleteEmptyDirs(lookupDir);
}
async fetchContractsAndGenerateUniqueSchemas(existingSchemas, shouldSyncOnlyCoreEvents, shouldOnlySyncRemoteEvents) {
const { errors, schemaTemplateItems } = await this.fetchAndBuildTemplateItems({
shouldSyncOnlyCoreEvents,
shouldOnlySyncRemoteEvents,
});
if (errors && errors?.length > 0) {
return {
errors,
};
}
const filteredSchemas = this.filterSchemasBasedOnCallbackPayload(existingSchemas, schemaTemplateItems);
return {
schemas: filteredSchemas,
};
}
filterSchemasBasedOnCallbackPayload(existingSchemas, schemaTemplateItems) {
const schemas = schemaTemplateItems.map((i) => i.schema);
const filteredSchemas = schemas.filter((schema) => {
const idWithVersion = (0, schema_1.normalizeSchemaToIdWithVersion)(schema);
return !existingSchemas.find((s) => (0, lodash_1.isEqual)((0, schema_1.normalizeSchemaToIdWithVersion)(s), idWithVersion));
});
return filteredSchemas;
}
async fetchAndBuildTemplateItems(options) {
const { shouldSyncOnlyCoreEvents, eventBuilderFile, shouldOnlySyncRemoteEvents, } = options;
this.ui.startLoading('Loading skill details...');
let namespace = await this.skillStore.loadCurrentSkillsNamespace();
this.ui.startLoading('Fetching event contracts...');
const namespaces = shouldSyncOnlyCoreEvents
? ['core']
: this.dependencyService.get().map((d) => d.namespace);
const contractResults = await this.eventStore.fetchEventContracts({
localNamespace: namespace,
namespaces,
shouldOnlySyncRemoteEvents,
didUpdateHandler: (msg) => {
this.ui.startLoading(msg);
},
});
if (contractResults.errors.length > 0) {
return {
errors: contractResults.errors,
eventContractTemplateItems: [],
schemaTemplateItems: [],
};
}
if (shouldSyncOnlyCoreEvents) {
namespace = undefined;
}
else {
namespace = spruce_skill_utils_1.namesUtil.toKebab(namespace);
}
this.ui.startLoading('Building contracts...');
const itemBuilder = new EventTemplateItemBuilder_1.default();
const { eventContractTemplateItems, schemaTemplateItems } = itemBuilder.buildTemplateItems({
contracts: contractResults.contracts,
localNamespace: namespace,
eventBuilderFile,
});
return {
eventContractTemplateItems,
schemaTemplateItems,
errors: [],
};
}
}
exports.default = EventContractBuilder;
//# sourceMappingURL=EventContractBuilder.js.map