UNPKG

@sprucelabs/spruce-cli

Version:

Command line interface for building Spruce skills.

93 lines 3.51 kB
"use strict"; 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 introspection_utility_1 = __importDefault(require("../../../utilities/introspection.utility")); const AbstractAction_1 = __importDefault(require("../../AbstractAction")); class SyncAction extends AbstractAction_1.default { optionsSchema = optionsSchema; commandAliases = ['sync.views']; invocationMessage = 'Syncing view controller types... 🌲'; async execute() { const targetDir = spruce_skill_utils_1.diskUtil.resolvePath(this.cwd, 'src'); const matches = await (0, globby_1.default)(['**/*.svc.ts', '**/*.vc.ts', '**/*.view.plugin.ts', '**/*.ac.ts'], { cwd: targetDir, }); if (matches.length === 0) { return {}; } const paths = matches.map((m) => spruce_skill_utils_1.diskUtil.resolvePath(targetDir, m)); const introspect = introspection_utility_1.default.introspect(paths); const vcTemplateItems = []; let appTemplateItem; const svcTemplateItems = []; const viewPluginItems = []; introspect.forEach(({ classes }) => { for (const thisClass of classes) { const { vc, svc, plugin, ac: avc, } = this.mapIntrospectedClassToTemplateItem(thisClass); if (vc) { vcTemplateItems.push(vc); } else if (plugin) { viewPluginItems.push(plugin); } else if (svc) { svcTemplateItems.push(svc); } else if (avc) { appTemplateItem = avc; } else { throw new Error('Unexpected class type.'); } } }); const namespace = await this.Store('skill').loadCurrentSkillsNamespace(); const files = await this.Writer('view').writeCombinedViewsFile(this.cwd, { namespaceKebab: spruce_skill_utils_1.namesUtil.toKebab(namespace), vcTemplateItems, svcTemplateItems, viewPluginItems, appTemplateItem, }); return { files, }; } mapIntrospectedClassToTemplateItem(c) { const item = { id: c.staticProperties.id, namePascal: c.className, path: c.classPath, }; let vc; let svc; let ac; let plugin; if (c.classPath.endsWith('.svc.ts')) { svc = item; } else if (c.classPath.endsWith('ac.ts')) { ac = item; } else if (c.classPath.endsWith('view.plugin.ts')) { const nameCamel = c.classPath.match(/([^/]+).view.plugin.ts$/)[1]; plugin = { ...item, nameCamel }; } else { vc = item; } return { svc, vc, plugin, ac }; } } exports.default = SyncAction; const optionsSchema = (0, schema_1.buildSchema)({ id: 'syncViewsOptions', description: 'Keep types and generated files based on views in sync.', fields: {}, }); //# sourceMappingURL=SyncAction.js.map