@sprucelabs/spruce-cli
Version:
Command line interface for building Spruce skills.
143 lines • 5.5 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const spruce_event_utils_1 = require("@sprucelabs/spruce-event-utils");
const spruce_skill_utils_1 = require("@sprucelabs/spruce-skill-utils");
const SpruceError_1 = __importDefault(require("../../../errors/SpruceError"));
const AbstractStore_1 = __importDefault(require("../../../stores/AbstractStore"));
class SkillStore extends AbstractStore_1.default {
name = 'skill';
static currentSkill;
constructor(options) {
super(options);
}
static clearCurrentSkill() {
this.currentSkill = undefined;
}
async register(values, options) {
const isRegisteringCurrentSkill = options?.isRegisteringCurrentSkill !== false;
isRegisteringCurrentSkill && (await this.assertInSkill());
const { name, slug, description, isPublished } = values;
const client = await this.connectToApi();
const results = await client.emit('register-skill::v2020_12_25', {
payload: {
name,
slug,
description,
isPublished,
},
});
const { skill } = spruce_event_utils_1.eventResponseUtil.getFirstResponseOrThrow(results);
if (isRegisteringCurrentSkill) {
await this.setCurrentSkillsNamespace(skill.slug);
this.Service('auth').updateCurrentSkill(skill);
}
return skill;
}
async assertInSkill() {
const isInstalled = this.Service('settings').isMarkedAsInstalled('skill');
if (!isInstalled) {
throw new SpruceError_1.default({ code: 'DIRECTORY_NOT_SKILL' });
}
}
async loadCurrentSkill() {
if (SkillStore.currentSkill) {
return SkillStore.currentSkill;
}
await this.assertInSkill();
const currentSkill = this.Service('auth').getCurrentSkill();
if (currentSkill) {
const client = await this.connectToApi({
shouldAuthAsCurrentSkill: true,
});
const response = await client.emit('get-skill::v2020_12_25', {
target: {
skillId: currentSkill.id,
},
});
const { skill } = spruce_event_utils_1.eventResponseUtil.getFirstResponseOrThrow(response);
SkillStore.currentSkill = {
...skill,
namespacePascal: spruce_skill_utils_1.namesUtil.toPascal(skill.slug),
isRegistered: true,
apiKey: currentSkill.apiKey,
};
return SkillStore.currentSkill;
}
return {
name: this.getNamespaceFromPkg(),
namespacePascal: this.getEventNamespaceForNotRegistered(),
description: this.getSkillDescriptionFromPkg(),
isRegistered: false,
};
}
async isCurrentSkillRegistered() {
const skill = await this.loadCurrentSkill();
return skill.isRegistered;
}
getNamespaceFromPkg() {
const nameFromPackage = this.Service('pkg').getSkillNamespace();
if (!nameFromPackage) {
throw new Error('You need need to set skill.namespace in the package.json');
}
return nameFromPackage;
}
async loadCurrentSkillsNamespace() {
const fallback = spruce_skill_utils_1.namesUtil.toPascal(this.getNamespaceFromPkg());
if (this.Service('auth').getCurrentSkill()) {
const current = await this.loadCurrentSkill();
return spruce_skill_utils_1.namesUtil.toPascal(current.slug ?? fallback);
}
return fallback;
}
async setCurrentSkillsNamespace(namespace) {
let isRegistered = false;
try {
isRegistered = await this.isCurrentSkillRegistered();
}
catch { }
if (isRegistered) {
throw new SpruceError_1.default({
code: 'GENERIC',
friendlyMessage: `You can't set the namespace of a skill that is registered.`,
});
}
this.Service('auth').updateCurrentSkillNamespace(namespace);
}
getEventNamespaceForNotRegistered() {
return spruce_skill_utils_1.namesUtil.toPascal(this.getNamespaceFromPkg());
}
getSkillDescriptionFromPkg() {
const pkg = this.Service('pkg');
return pkg.get('description');
}
async unregisterSkill(skillId) {
const client = await this.connectToApi();
const response = await client.emit('unregister-skill::v2020_12_25', {
target: {
skillId,
},
});
spruce_event_utils_1.eventResponseUtil.getFirstResponseOrThrow(response);
if (SkillStore.currentSkill?.id === skillId) {
SkillStore.currentSkill = undefined;
this.Service('auth').logoutCurrentSkill();
}
}
async fetchMySkills() {
return this.fetchAllSkills({ shouldOnlyShowMine: true });
}
async fetchAllSkills(query) {
const client = await this.connectToApi();
const [{ skills }] = await client.emitAndFlattenResponses('list-skills::v2020_12_25', {
payload: {
...query,
},
});
return skills;
}
}
exports.default = SkillStore;
//# sourceMappingURL=SkillStore.js.map