UNPKG

@sprucelabs/spruce-cli

Version:

Command line interface for building Spruce skills.

201 lines • 7.86 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 spruce_skill_utils_1 = require("@sprucelabs/spruce-skill-utils"); const SpruceError_1 = __importDefault(require("../../../errors/SpruceError")); const AbstractStore_1 = __importDefault(require("../../../stores/AbstractStore")); class SkillStoreImpl 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 && this.assertInSkill(); const { name, slug, description, isPublished } = values; const client = await this.connectToApi(); const [{ skill }] = await client.emitAndFlattenResponses('register-skill::v2020_12_25', { payload: { name, slug, description, isPublished, }, }); if (isRegisteringCurrentSkill) { await this.setCurrentSkillsNamespace(skill.slug); this.Service('auth').updateCurrentSkill(skill); } return skill; } assertInSkill() { const isInstalled = this.Service('settings').isMarkedAsInstalled('skill'); if (!isInstalled) { throw new SpruceError_1.default({ code: 'DIRECTORY_NOT_SKILL' }); } } async loadCurrentSkill() { if (SkillStoreImpl.currentSkill) { return SkillStoreImpl.currentSkill; } this.assertInSkill(); const currentSkill = this.Service('auth').getCurrentSkill(); if (currentSkill) { const client = await this.connectToApi({ shouldAuthAsCurrentSkill: true, }); const [{ skill }] = await client.emitAndFlattenResponses('get-skill::v2020_12_25', { target: { skillId: currentSkill.id, }, }); SkillStoreImpl.currentSkill = { ...skill, namespacePascal: spruce_skill_utils_1.namesUtil.toPascal(skill.slug), isRegistered: true, apiKey: currentSkill.apiKey, }; return SkillStoreImpl.currentSkill; } return { name: this.getNamespaceFromPkg(), namespacePascal: this.getEventNamespaceForNotRegistered(), description: this.getSkillDescriptionFromPkg(), isRegistered: false, }; } async publish(options) { const { isInstallable = true } = options || {}; const skill = await this.loadCurrentSkill(); if (!skill.id) { throw new SpruceError_1.default({ code: 'NO_SKILLS_REGISTERED', friendlyMessage: 'You need to register your skill before you can publish it. Run `spruce register` to get started.', }); } const client = await this.connectToApi(); await client.emitAndFlattenResponses('publish-skill::v2020_12_25', { target: { skillId: skill.id, }, payload: { canBeInstalled: isInstallable, }, }); delete SkillStoreImpl.currentSkill; } async isCurrentSkillRegistered() { const skill = await this.loadCurrentSkill(); return skill.isRegistered; } getGoModuleName(options) { const { shouldIncludePathFromCwd = false } = options || {}; let goModFile = spruce_skill_utils_1.diskUtil.resolvePath(this.cwd, 'go.mod'); if (!spruce_skill_utils_1.diskUtil.doesFileExist(goModFile)) { goModFile = spruce_skill_utils_1.diskUtil.resolvePath(this.cwd, '../go.mod'); } if (!spruce_skill_utils_1.diskUtil.doesFileExist(goModFile)) { throw new SpruceError_1.default({ code: 'DIRECTORY_NOT_GO_MODULE', friendlyMessage: `Couldn't find a go.mod file in the current directory or its parent. Are you in a Go module?`, cwd: this.cwd, }); } const goModContents = spruce_skill_utils_1.diskUtil.readFile(goModFile); const moduleLine = goModContents.match(/module\s+([^\s]+)/); const goModuleName = moduleLine?.[1]; if (shouldIncludePathFromCwd) { const goModDir = path_1.default.dirname(goModFile); const relativePath = this.cwd.replace(goModDir, ''); if (relativePath) { return `${goModuleName}${relativePath}`; } } return goModuleName; } getNamespaceFromPkg() { if (this.isGoModule()) { const module = this.getGoModuleName(); const moduleParts = module.split('/') ?? []; return moduleParts.pop(); } const nameFromPackage = this.Service('pkg').getSkillNamespace(); if (!nameFromPackage) { throw new Error('You need need to set skill.namespace in the package.json'); } return nameFromPackage; } isGoModule() { return spruce_skill_utils_1.diskUtil.detectProjectLanguage(this.cwd) === 'go'; } async loadCurrentSkillsNamespace() { const fallback = this.getNamespaceFromPkg(); if (!this.isGoModule() && this.Service('auth').getCurrentSkill()) { const current = await this.loadCurrentSkill(); return spruce_skill_utils_1.namesUtil.toPascal(current.slug ?? fallback); } return spruce_skill_utils_1.namesUtil.toPascal(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(); let resolvedSkillId = skillId; if (!skillId) { const currentSkill = this.Service('auth').getCurrentSkill(); resolvedSkillId = currentSkill?.id; } await client.emitAndFlattenResponses('unregister-skill::v2020_12_25', { target: { skillId: resolvedSkillId, }, }); if (!skillId || SkillStoreImpl.currentSkill?.id === skillId) { SkillStoreImpl.currentSkill = undefined; this.Service('auth').logoutCurrentSkill(); } } async fetchMySkills() { return this.fetchAllSkills({ shouldOnlyShowMine: true }); } static setCurrentSkill(skill) { this.currentSkill = skill; } async fetchAllSkills(query) { const client = await this.connectToApi(); const [{ skills }] = await client.emitAndFlattenResponses('list-skills::v2020_12_25', { payload: { ...query, }, }); return skills; } } exports.default = SkillStoreImpl; //# sourceMappingURL=SkillStore.js.map