UNPKG

@sprucelabs/spruce-cli

Version:

Command line interface for building Spruce skills.

85 lines 4.12 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const spruce_skill_utils_1 = require("@sprucelabs/spruce-skill-utils"); const AbstractWriter_1 = __importDefault(require("../../../writers/AbstractWriter")); class ErrorWriter extends AbstractWriter_1.default { async writeOrAppendErrorsToClass(destinationDir, errors) { let results = []; if (errors.length === 0) { // todo move to proper error throw new Error('Need at least one error'); } const resolvedDestination = spruce_skill_utils_1.diskUtil.resolvePath(destinationDir, `SpruceError.ts`); if (!spruce_skill_utils_1.diskUtil.doesFileExist(resolvedDestination)) { this.ui.startLoading('Creating Error class'); const errorContents = this.templates.error({ errors: errors.filter((error) => !error.isNested), }); results = await this.writeFileIfChangedMixinResults(resolvedDestination, errorContents, 'A new subclass of SpruceBaseError where you can control your error messaging.', results); } else { this.ui.startLoading('Updating Error class'); const updates = await this.dropInNewErrorCases(errors, resolvedDestination); if (updates.length > 0) { results.push({ ...updates[0], description: `${updates.length} new error cases were dropped in.`, }); } } return results; } async dropInNewErrorCases(errors, destinationFile) { let results = []; for (const error of errors) { if (!error.isNested) { const dropInResults = await this.dropInErrorCaseIfMissing(error, destinationFile); results.push(...dropInResults); } } return results; } async dropInErrorCaseIfMissing(error, destinationFile) { let results = []; const errorBlock = this.templates.error({ errors: [error], renderClassDefinition: false, }); // Try and drop in the block right before "default:" const currentErrorContents = spruce_skill_utils_1.diskUtil.readFile(destinationFile); const blockMatches = currentErrorContents.search(/default:/g); if (blockMatches > -1 && !this.doesErrorCaseExist(currentErrorContents, error)) { const newErrorContents = currentErrorContents.substring(0, blockMatches) + '\n' + errorBlock + '\n' + currentErrorContents.substring(blockMatches); results = await this.writeFileIfChangedMixinResults(destinationFile, newErrorContents, `A new block was added to handle ${error.code}.`, results); } return results; } doesErrorCaseExist(currentContents, error) { return currentContents.search(new RegExp(`case '${error.code}':`)) > -1; } async writeOptionsTypesFile(destinationDir, errorTemplateItems) { const contents = this.templates.errorOptionsTypes({ options: errorTemplateItems, }); const destination = spruce_skill_utils_1.diskUtil.resolvePath(destinationDir, 'options.types.ts'); this.ui.startLoading('Updating error options...'); const results = this.writeFileIfChangedMixinResults(destination, contents, 'A union of all possible error codes and their options.'); return results; } async writePlugin(cwd) { const destination = spruce_skill_utils_1.diskUtil.resolveHashSprucePath(cwd, 'features', 'error.plugin.ts'); const pluginContents = this.templates.errorPlugin(); const results = this.writeFileIfChangedMixinResults(destination, pluginContents, 'Supports your skill with Error generation and handling.'); return results; } } exports.default = ErrorWriter; //# sourceMappingURL=ErrorWriter.js.map