UNPKG

@kiwicom/orbit-tracking

Version:

Tracking for orbit design system

94 lines (91 loc) 3.16 kB
import { getCategory } from "./chunk-H6RNZU2N.js"; import { __spreadProps, __spreadValues } from "./chunk-EQVCDNQH.js"; // src/orbitProcessor.ts import { path } from "zx"; var isIcon = (instance) => instance.importInfo.moduleName.includes("/icons"); var orbitProcessor_default = ({ forEachComponent, deprecated, sortObjectKeysByValue, output }) => { let result = {}; forEachComponent(({ componentName: name, component }) => { const { instances } = component; if (!instances) return; const { REPO_URL = "", OUTPUT_DIR = "" } = process.env; result[name] = { instances: instances.length, sources: instances.map((instance) => ({ url: path.join(REPO_URL.replace(/\.git$/, ""), "-/blob/master", `${path.relative(OUTPUT_DIR, instance.location.file)}#L${instance.location.start.line}`).replace("https:/", "https://"), props: Object.entries(instance.props).map(([k, v]) => ({ name: k, value: v === null ? "null" : v })) })), props: {}, category: getCategory(name), isDeprecated: false, icon: false }; for (const instance of instances) { const { imported } = instance.importInfo; if (deprecated.includes(imported)) { result[name].isDeprecated = true; } if (isIcon(instance)) { result[name].icon = true; result[name].category = "icons"; } for (const prop in instance.props) { if (prop) { const propValue = instance.props[prop]; if (result[name].props[prop] === void 0) { result[name].props[prop] = { used: 0, values: {} }; } if (result[name].props[prop].used === void 0) { result[name].props[prop].used = 0; } else { result[name].props[prop].used += 1; } if (result[name].props[prop].values[propValue] === void 0) { result[name].props[prop].values[propValue] = { value: propValue, used: 1 }; } else { result[name].props[prop].values[propValue].used += 1; } result[name].props = sortObjectKeysByValue(result[name].props, (property) => property.used); result[name].props[prop].values = sortObjectKeysByValue(result[name].props[prop].values, (value) => value.used); } } } }); result = sortObjectKeysByValue(result, (component) => component.instances); const outputData = []; for (const [name, value] of Object.entries(result)) { const properties = []; for (const [key, used] of Object.entries(value.props)) { const values = []; for (const propName of Object.keys(used.values)) { values.push({ name: propName, used: used.values[propName].used }); } properties.push(__spreadProps(__spreadValues({ name: key }, used), { values })); } outputData.push(__spreadProps(__spreadValues({ name }, value), { props: properties })); } output(JSON.stringify(outputData)); return outputData; }; export { orbitProcessor_default };