@sprucelabs/spruce-cli
Version:
Command line interface for building Spruce skills.
152 lines • 7.15 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateEventContractFileName = generateEventContractFileName;
const path_1 = __importDefault(require("path"));
const spruce_event_utils_1 = require("@sprucelabs/spruce-event-utils");
const spruce_skill_utils_1 = require("@sprucelabs/spruce-skill-utils");
const AbstractWriter_1 = __importDefault(require("../../../writers/AbstractWriter"));
const CONTRACT_FILE_NAME = `events.contract.ts`;
class EventWriter extends AbstractWriter_1.default {
async writeContracts(destinationDir, options) {
this.isLintEnabled = false;
const { eventContractTemplateItems, shouldImportCoreEvents = true } = options;
const generated = [];
for (const item of eventContractTemplateItems) {
generated.push(this.writeContract({
...options,
destinationDir,
eventContractTemplateItem: item,
}));
}
generated.push(this.writeCombinedEvents({
...options,
shouldImportCoreEvents,
destinationDir,
templateItems: eventContractTemplateItems,
}));
const all = await Promise.all(generated);
this.isLintEnabled = true;
await this.lint(destinationDir);
return all;
}
async writeContract(options) {
const { destinationDir, eventContractTemplateItem, schemaTemplateItems, } = options;
const destinationFile = spruce_skill_utils_1.diskUtil.resolvePath(destinationDir, eventContractTemplateItem.namespaceCamel, generateEventContractFileName(eventContractTemplateItem));
const eventsContractContents = this.templates.eventContract({
...options,
...eventContractTemplateItem,
schemaTemplateItems,
});
const results = await this.writeFileIfChangedMixinResults(destinationFile, eventsContractContents, `The event contract for ${Object.keys(eventContractTemplateItem.eventSignatures)[0]}`);
return results[0];
}
hasCombinedContractBeenWritten(cwd) {
if (spruce_skill_utils_1.diskUtil.doesHashSprucePathExist(cwd)) {
const path = spruce_skill_utils_1.diskUtil.resolveHashSprucePath(cwd, 'events', CONTRACT_FILE_NAME);
return spruce_skill_utils_1.diskUtil.doesFileExist(path);
}
return false;
}
async writeCombinedEvents(options) {
const { destinationDir } = options;
const destinationFile = spruce_skill_utils_1.diskUtil.resolvePath(destinationDir, CONTRACT_FILE_NAME);
const contents = this.templates.combinedEventsContract(options);
const results = await this.writeFileIfChangedMixinResults(destinationFile, contents, 'All event contracts combined to a single export.');
return results[0];
}
async writeListener(destinationDir, options) {
const { schemaTypesLookupDir, fullyQualifiedEventName } = options;
const event = spruce_event_utils_1.eventNameUtil.split(fullyQualifiedEventName);
const resolvedDestination = spruce_event_utils_1.eventDiskUtil.resolveListenerPath(destinationDir, event);
const relativeTypesFile = this.resolveSchemaTypesFile({
namespace: event.eventNamespace,
schemaTypesLookupDir,
resolvedDestination,
});
const listenerContents = this.templates.listener({
...options,
schemaTypesFile: relativeTypesFile,
});
const results = await this.writeFileIfChangedMixinResults(resolvedDestination, listenerContents, `Listener for ${fullyQualifiedEventName}.`);
return results;
}
async writeListenerMap(destinationDir, options) {
const destination = spruce_skill_utils_1.diskUtil.resolvePath(destinationDir, 'listeners.ts');
const contents = this.templates.listeners({
listeners: options.listeners,
});
const results = await this.writeFileIfChangedMixinResults(destination, contents, 'All your listeners imported to one place for your skill to use when booting!');
return results;
}
async writeEvent(destinationDir, options) {
const { version, nameKebab, nameCamel, nameReadable, isGlobal = false, } = options;
const templates = [
{
templateMethod: 'eventEmitPayload',
name: 'emitPayload.builder.ts',
action: 'generated',
description: 'The payload that will be sent when you emit this event.',
},
{
templateMethod: 'eventEmitTarget',
name: 'emitTarget.builder.ts',
action: 'generated',
description: 'The target that will be sent when you emit this event.',
},
{
templateMethod: 'eventResponsePayload',
name: 'responsePayload.builder.ts',
action: 'generated',
description: 'The payload that every listener will need to respond with. Delete this file for events that are fire and forget.',
},
{
templateMethod: 'eventOptions',
name: 'event.options.ts',
action: 'generated',
description: 'Extra options that can be set for your event',
context: {
isGlobal,
},
},
];
const files = [];
for (const file of templates) {
const destination = spruce_event_utils_1.eventDiskUtil.resolveEventPath(destinationDir, {
fileName: file.name,
eventName: nameKebab,
version,
});
const contents = this.templates[file.templateMethod]({
nameCamel,
nameReadable,
version,
...file.context,
});
spruce_skill_utils_1.diskUtil.writeFile(destination, contents);
files.push({
...file,
path: destination,
});
}
await this.lint(destinationDir);
return files;
}
resolveSchemaTypesFile(options) {
const { schemaTypesLookupDir, resolvedDestination, namespace } = options;
if (!namespace) {
return '@sprucelabs/mercury-types';
}
const schemaTypesFile = path_1.default.join(schemaTypesLookupDir, spruce_skill_utils_1.DEFAULT_SCHEMA_TYPES_FILENAME);
let relativeTypesFile = spruce_skill_utils_1.diskUtil.resolveRelativePath(path_1.default.dirname(resolvedDestination), schemaTypesFile);
relativeTypesFile = relativeTypesFile.replace(path_1.default.extname(relativeTypesFile), '');
return relativeTypesFile;
}
}
exports.default = EventWriter;
function generateEventContractFileName(options) {
return `${options.nameCamel}.${options.version}.contract.ts`;
}
//# sourceMappingURL=EventWriter.js.map