UNPKG

@sketch-hq/sketch-assistant-utils

Version:

Utility functions and types for Sketch Assistants.

32 lines (31 loc) 1.66 kB
/** * Create a profile data object for a given RunOutput. Profile data collates statistics that may * help provide insight into Assistant performance, including for example rules that may be taking * a long time to execute. One way to view profile data is to pass the `--profile` flag to the * Assistants CLI. */ export const makeProfile = (output) => { const { processedFile } = output.input; const file = { time: processedFile.profile.time, totalObjects: processedFile.profile.numObjects, objectCounts: Object.keys(processedFile.objects).reduce((acc, key) => (Object.assign(Object.assign({}, acc), { [key]: { count: processedFile.objects[key].length } })), {}), }; const assistants = Object.keys(output.assistants).reduce((acc, key) => { const result = output.assistants[key]; if (result.code === 'error') return acc; const { result: successResult } = result; const { ruleTimings } = successResult.profile; return Object.assign(Object.assign({}, acc), { [key]: { violations: successResult.violations.length, ruleErrors: successResult.ruleErrors.length, time: Object.keys(ruleTimings).reduce((acc, key) => acc + ruleTimings[key], 0), rules: Object.keys(ruleTimings).reduce((acc, key) => (Object.assign(Object.assign({}, acc), { [key]: { time: ruleTimings[key], violations: successResult.violations.filter((violation) => violation.ruleName === key).length, } })), {}), } }); }, {}); return { file, assistants }; };