@lmc-eu/spirit-analytics
Version:
Analytic tools for Spirit Design System
160 lines (129 loc) • 5.89 kB
JavaScript
;Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }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
var _zx = require('zx');
var errorMessage = (message) => console.error(_zx.chalk.red(message));
var infoMessage = (message) => console.info(_zx.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
var _filedirname = require('filedirname'); var _filedirname2 = _interopRequireDefault(_filedirname);
var [_filename, _dirname] = _filedirname2.default.call(void 0, );
var getOutputPath = (outputPath, name) => {
const outputFilename = `${OUTPUT_FILENAME_PREFIX}-${name}.json`;
return outputPath ? _zx.path.resolve(outputPath, outputFilename) : _zx.path.resolve(process.cwd(), `${OUTPUT_DIR}/${outputFilename}`);
};
// src/helpers/versions.ts
var getVersions = async (pathToFolder) => {
const lockFiles = await _zx.globby.call(void 0, `${pathToFolder}/**/@(yarn.lock|package-lock.json)`);
let version = "";
for (const lock of lockFiles) {
const data = await _zx.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
var OUTPUT_FILE = _zx.path.resolve(process.cwd(), `${OUTPUT_DIR}/${REACT_OUTPUT_FILE_NAME}-${timestamp()}.json`);
var getRelativePath = (absolutePath) => _zx.path.relative(_zx.path.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
var react_scanner_config_default = {
crawlFrom: _zx.path.resolve(process.cwd(), ROOT_PATH),
includeSubComponents: true,
exclude: ["node_modules", "dist", "build", "coverage", "public", "vendor", "storybook-static"],
processors: [spiritAdoptionProcessor_default]
};
exports.OUTPUT_DIR = OUTPUT_DIR; exports.ROOT_PATH = ROOT_PATH; exports.TWIG_OUTPUT_FILE_NAME = TWIG_OUTPUT_FILE_NAME; exports.TWIG_CORE_COMPONENTS_PATH = TWIG_CORE_COMPONENTS_PATH; exports.TWIG_CONFIG_FILE = TWIG_CONFIG_FILE; exports.constants_exports = constants_exports; exports.timestamp = timestamp; exports.errorMessage = errorMessage; exports.infoMessage = infoMessage; exports._dirname = _dirname; exports.getOutputPath = getOutputPath; exports.getVersions = getVersions; exports.helpers_exports = helpers_exports; exports.spiritAdoptionProcessor_default = spiritAdoptionProcessor_default; exports.react_scanner_config_default = react_scanner_config_default;