Learn using the CLI hooks to add custom component properties to your scan data.
Omlet's CLI hooks allow you to run custom scripts on Omlet CLI scans before the final result is sent to the Omlet backend. This offers an interface to process the component data and add custom properties.
Once those custom properties are added to components, you and your teammates can create tags based on them. Here are some use cases that you can achieve by utilizing the CLI hooks to create tags with custom properties:
Automatically tag components based on their category, such as Icons or Pages
Identify the product team or user who created a component
Find components that have missing documentation or tests
Differentiate visual vs. non-visual components
Custom properties
The custom properties can be string, number, date or boolean values. For example, you can add additional information such as:
owner: The code owner information in the CODEOWNERS file or any custom source. i.e. @acme/design-system, @acme/backoffice, @acme/marketplace
hasStories: True or false depending on whether Storybook stories exist for a given component
hasTests: True or false depending on test specs that exist for a given component
Components can be tagged accordingly by applying the following filters:
You can place your hook script in a preferred directory/folder. Once your script is ready, you scan your repository using the --hook-script command line argument.
If you have the @omlet/cli package installed as a dependency, the @type annotation will enable auto-complete features and inline documentation for functions and components.
Here are the properties of the Component object provided with the afterScan hook:
Here are a few things to know about the afterScan hook:
afterScan hook can be defined both as an async or sync function.
The data passed to the hook is read-only, except the metadata that is editable with the setMetadata function.
You can use npm packages as long as they're available in the Node.js runtime.
Sample hook scripts
Code owners
You can add codeowners package as a dependency to your repository and use the following code example to add the code owner information to each component's metadata.
The following hook function marks add hasTests and hasStories properties to each component's metadata to mark the components in terms of their test and Storybook coverages.
The following hook function marks components as visual if they render a visual HTML element (e.g. <div>, <img />) or another component marked as visual. Here you can find this snippet with a full list of html tags.
hook-script.js
// A set of HTML tags that are considered to be UI elementsconsthtmlUiTags=newSet(["a","abbr","acronym","address",// ...]);/** * @type{import('@omlet/cli').CliHookModule} */module.exports= {afterScan(components) {constvisualComponents=newSet();let updated;do { updated =false;for (constcomponentof components) {if (visualComponents.has(component.id)) {continue; } elseif (component.htmlElementsUsed.some((tag) =>htmlUiTags.has(tag))) {component.setMetadata("isVisualComponent",true);visualComponents.add(component.id); updated =true; } elseif (component.children.some(child =>visualComponents.has(child.id))) {component.setMetadata("isVisualComponent",true);visualComponents.add(component.id); updated =true; } else {component.setMetadata("isVisualComponent",false); } } } while (updated); }}
Deprecated components
The following hook function marks components that contains the @deprecated comment.
const { promises: fs, constants: fsConstants } =require("fs");constpath=require("path");// Function to check if a file contains the @deprecated commentasyncfunctionisDeprecated(filePath) {try {constfileContent=awaitfs.readFile(filePath,"utf-8");constdeprecatedPattern= /@deprecated/; // Simple pattern to detect @deprecatedreturndeprecatedPattern.test(fileContent); } catch (err) {console.error(`Error reading file ${filePath}:`, err);returnfalse; }}/** * @type{import('@omlet/cli').CliHookModule} */module.exports= {asyncafterScan(components) {for (constcomponentof components) {// Check if component is deprecatedconstdeprecated=awaitisDeprecated(component.filePath);component.setMetadata("Is deprecated", deprecated); } }}