UNPKG

@lmc-eu/spirit-analytics

Version:

Analytic tools for Spirit Design System

160 lines (149 loc) 5.34 kB
var __defProp = Object.defineProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; // src/constants.ts var constants_exports = {}; __export(constants_exports, { OUTPUT_DIR: () => OUTPUT_DIR, OUTPUT_FILENAME_PREFIX: () => OUTPUT_FILENAME_PREFIX, REACT_OUTPUT_FILE_NAME: () => REACT_OUTPUT_FILE_NAME, ROOT_PATH: () => ROOT_PATH, TWIG_CONFIG_FILE: () => TWIG_CONFIG_FILE, TWIG_CORE_COMPONENTS_PATH: () => TWIG_CORE_COMPONENTS_PATH, TWIG_OUTPUT_FILE_NAME: () => TWIG_OUTPUT_FILE_NAME }); var OUTPUT_FILENAME_PREFIX = "spirit-analytics"; var OUTPUT_DIR = ".scanner"; var ROOT_PATH = "."; var REACT_OUTPUT_FILE_NAME = "adoption-data-react"; var TWIG_OUTPUT_FILE_NAME = "./.scanner/adoption-data-twig"; var TWIG_CORE_COMPONENTS_PATH = "./vendor/lmc/spirit-web-twig-bundle/src/Resources/twig-components"; var TWIG_CONFIG_FILE = "./config/spirit-web-twig.yml"; // src/helpers/index.ts var helpers_exports = {}; __export(helpers_exports, { _dirname: () => _dirname, errorMessage: () => errorMessage, getModuleName: () => getModuleName, getOutputPath: () => getOutputPath, getVersions: () => getVersions, infoMessage: () => infoMessage, timestamp: () => timestamp }); // src/helpers/date.ts var timestamp = () => { const year = (/* @__PURE__ */ new Date()).getFullYear(); const month = (/* @__PURE__ */ new Date()).getMonth() + 1; const day = (/* @__PURE__ */ new Date()).getDate(); return `${year}-${month}-${day}`; }; // src/helpers/message.ts import { chalk } from "zx"; var errorMessage = (message) => console.error(chalk.red(message)); var infoMessage = (message) => console.info(chalk.magenta.bold(message)); // src/helpers/moduleName.ts var getModuleName = (instance, componentName) => { if (instance.importInfo == null && componentName === componentName.toLowerCase()) { return "html_element"; } if (instance.importInfo == null) { return null; } const { moduleName } = instance.importInfo; if (/^\./.test(moduleName)) { return "local_component"; } return moduleName; }; // src/helpers/path.ts import filedirname from "filedirname"; import { path } from "zx"; var [_filename, _dirname] = filedirname(); var getOutputPath = (outputPath, name) => { const outputFilename = `${OUTPUT_FILENAME_PREFIX}-${name}.json`; return outputPath ? path.resolve(outputPath, outputFilename) : path.resolve(process.cwd(), `${OUTPUT_DIR}/${outputFilename}`); }; // src/helpers/versions.ts import { fs, globby } from "zx"; var getVersions = async (pathToFolder) => { const lockFiles = await globby(`${pathToFolder}/**/@(yarn.lock|package-lock.json)`); let version = ""; for (const lock of lockFiles) { const data = await fs.readFile(lock, "utf-8"); const matchedVersionArray = data.match( /https:\/\/registry.(yarnpkg.com|npmjs.org)\/@lmc-eu\/spirit-web-react\/-\/spirit-web-react-[~^]?([\dvx*]+(?:[-.](?:[\dx*]+|alpha|beta))*)/g ); if (matchedVersionArray) { const [matchedVersionUrl] = matchedVersionArray; [version] = matchedVersionUrl.split("react-").slice(-1); } } return version; }; // src/processors/spiritAdoptionProcessor.ts import { path as path2 } from "zx"; var OUTPUT_FILE = path2.resolve(process.cwd(), `${OUTPUT_DIR}/${REACT_OUTPUT_FILE_NAME}-${timestamp()}.json`); var getRelativePath = (absolutePath) => path2.relative(path2.resolve(_dirname, ROOT_PATH), absolutePath); var spiritAdoptionProcessor = ({ forEachComponent, sortObjectKeysByValue, output }) => { let scannedComponents = {}; forEachComponent(({ componentName, component }) => { const { instances } = component; if (!instances) { return; } scannedComponents = instances.reduce((acc, currentInstance) => { const moduleName = getModuleName(currentInstance, componentName); const componentFullName = `${moduleName}:${componentName}`; if (!moduleName) { return acc; } const currentComponentScan = acc[componentFullName] || []; const componentPathAndProps = { path: `${getRelativePath(currentInstance.location.file)}:${currentInstance.location.start.line}`, props: sortObjectKeysByValue(currentInstance.props) }; return { ...acc, [componentFullName]: [...currentComponentScan, componentPathAndProps] }; }, scannedComponents); }); const sortedScannedComponents = sortObjectKeysByValue( scannedComponents, (component) => component.instances ); output(sortedScannedComponents, OUTPUT_FILE); return sortedScannedComponents; }; var spiritAdoptionProcessor_default = spiritAdoptionProcessor; // src/scanners/react-scanner.config.ts import { path as path3 } from "zx"; var react_scanner_config_default = { crawlFrom: path3.resolve(process.cwd(), ROOT_PATH), includeSubComponents: true, exclude: ["node_modules", "dist", "build", "coverage", "public", "vendor", "storybook-static"], processors: [spiritAdoptionProcessor_default] }; export { OUTPUT_DIR, ROOT_PATH, TWIG_OUTPUT_FILE_NAME, TWIG_CORE_COMPONENTS_PATH, TWIG_CONFIG_FILE, constants_exports, timestamp, errorMessage, infoMessage, _dirname, getOutputPath, getVersions, helpers_exports, spiritAdoptionProcessor_default, react_scanner_config_default };