UNPKG

@sketch-hq/sketch-assistant-utils

Version:

Utility functions and types for Sketch Assistants.

70 lines (69 loc) 3.84 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getRuleDefinition = exports.assign = exports.prepare = void 0; const sketch_assistant_types_1 = require("@sketch-hq/sketch-assistant-types"); /** * Assistant packages should be exported on the default export. However the shape of that export * differs depending on the esModuleInterop strategy. This function should consistently return * the inner AssistantPackage value either way. */ const getDefaultExport = (target) => '__esModule' in target ? target.default : target; /** * Prepare an assistant package. That is, un-roll its exported value into a flat list of assistant * functions, invoke and await them to obtain a flat list of concrete assistant definitions which is * then merged to form a final/single assistant definition. * * Assistant preparation is performed at runtime by an assistant runner. * * @param source The assistant package to prepare * @param context The env within which the assistant package is being prepared */ const prepare = (pkg, env) => __awaiter(void 0, void 0, void 0, function* () { pkg = getDefaultExport(pkg); const definitions = yield Promise.all((Array.isArray(pkg) ? pkg : [pkg]) .flat(Infinity) .map(getDefaultExport) .map((f) => f(env))); return assign(...definitions); }); exports.prepare = prepare; /** * Merge assistant definitions together to form a single assistant definition, with a syntax similar * to Object.assign(). Assistants are merged from the right-most argument to the left into * preceeding arguments, according to the following algorithm: * * 1. Rule configuration objects are merged together, with values from right-most assistants * overriding those from the next assistant to the left * 2. Assistant rule function arrays are concatenated * * @param sources Assistant definitions to merge */ const assign = (...sources) => { return sources.reduceRight((acc, curr) => { return { name: acc.name || curr.name || '', config: Object.assign(Object.assign({}, (typeof acc.config.defaultSeverity === 'undefined' ? {} : { defaultSeverity: acc.config.defaultSeverity })), { rules: Object.entries(curr.config.rules).reduceRight((a, [name, opts]) => { var _a, _b, _c; return Object.assign(Object.assign({}, a), { [name]: Object.assign(Object.assign(Object.assign({}, opts), a[name]), { active: (_c = (_b = (_a = a[name]) === null || _a === void 0 ? void 0 : _a[sketch_assistant_types_1.ReservedRuleOptionNames.active]) !== null && _b !== void 0 ? _b : opts === null || opts === void 0 ? void 0 : opts[sketch_assistant_types_1.ReservedRuleOptionNames.active]) !== null && _c !== void 0 ? _c : false }) }); }, acc.config.rules) }), rules: [...curr.rules, ...acc.rules], }; }); }; exports.assign = assign; /** * Lookup a rule definition by rule name. */ const getRuleDefinition = (assistant, ruleName) => assistant.rules.find((rule) => rule.name === ruleName); exports.getRuleDefinition = getRuleDefinition;