Omlet Docs
BlogChangelogAsk the CommunityContact Sales
  • Get started
    • What is Omlet?
    • CLI & Dashboard
  • Omlet for VS Code
  • CLI & Dashboard
    • CLI
      • Your first scan
      • Set up your dashbard
      • Future scans
      • Ensure data accuracy
      • Config file
        • Exports configuration
        • Mapping aliases
        • Excluding certain components & files
        • Tutorial: Config file
      • Custom component properties
        • CLI hooks
        • Tutorial 1: Team/code owner usage
        • Tutorial 2: Package version tracking
        • Other example scripts
      • Set up regular scans
      • CLI commands
        • init
        • analyze
        • login
    • Analytics
      • Popular charts
      • Create custom charts
      • Save charts to dashboard
      • Share charts and dashboards with your team
      • Download chart data
    • Components
      • Search and filter components
      • Component tags
      • Dependency Tree
      • Props tracking
    • Workspace & Account
      • Invite team members
      • Renaming projects
      • Update your email address
      • Access your billing details & invoices
  • Security
    • Security in Omlet
    • Data collection
  • Help
    • Pricing
    • FAQs
      • How detection works?
      • Monorepo support
      • How to delete scans?
      • Omlet vs. React Scanner
      • Working with multiple workspaces
    • Troubleshooting
      • Debugging CLI issues
      • Some components aren't detected
      • API failed or timeout
      • Are you behind a proxy?
      • Troubleshooting Git errors
Powered by GitBook
On this page
  1. CLI & Dashboard
  2. CLI
  3. Custom component properties

Tutorial 2: Package version tracking

Step-by-step tutorial for tracking the usage of package versions.

PreviousTutorial 1: Team/code owner usageNextOther example scripts

Last updated 1 month ago

In this tutorial, we’ll guide you through setting up a custom CLI hook in Omlet to track the version of your design system package used across your components. This will help you analyze which versions are in use throughout your codebase.

1

Install pkg-up

Install as a dependency. It will help us locate the nearest package.json file.

npm install pkg-up --save-dev
yarn add pkg-up --dev
pnpm add pkg-up --save-dev
2

Setting up the hook script

Create a file named hook-script.js in the root of your application repository and copy the following code. Then, make sure to replace @design-system with your actual design system package name in line 21.

hook-script.js
const fs = require("fs/promises");
const path = require("path");
const pkgUp = require("pkg-up");

/**
 * Find the closest package.json file and retrieve the design system version.
 *
 * @param {string} filePath - The file path to start the search from.
 * @returns {Promise<string>} - The design system version or 'unknown' if not found.
 */
async function findDSPackageVersion(filePath) {
  try {
    const packageJsonPath = await pkgUp({ cwd: filePath });
    if (!packageJsonPath) return "unknown";

    const packageJson = JSON.parse(await fs.readFile(packageJsonPath, "utf-8"));
    const dependencies = packageJson.dependencies || {};
    const devDependencies = packageJson.devDependencies || {};

    // Example: Replace '@design-system' with your actual design system package name.
    return dependencies["@design-system"] || devDependencies["@design-system"] || "unknown";
  } catch (error) {
    console.error(`Error finding DS version for ${filePath}:`, error);
    return "unknown";
  }
}

/**
 * @type {import('@omlet/cli').CliHookModule}
 */
module.exports = {
  async afterScan(components) {
    const promises = components.map(async (component) => {
      const dsVersion = await findDSPackageVersion(component.filePath);
      component.setMetadata("DS Version", dsVersion);
    });

    // Wait for all metadata to be set
    await Promise.all(promises);
  },
};

This findDSPackageVersion function searches for the nearest package.json file relative to a given component’s file path and retrieves the version of your design system package.

The afterScan hook processes each component and sets it as DS Version property. Feel free to update the property to a preferred name.

3

Scan your repo

4

Filter components by DS Version

Once the CLI scan complete, the DS Version property will be associated with each component.

Navigate to the "Components" page. From the "Custom properties" filter, click DS Version and select a version from the revealed dropdown. This will list all the components using the selected package version.

You can create a tag based on this filter to easily access it later on and generate charts for your specific needs.

5

Analyze the usage

To analyze component usage, open your Analytics dashboard and click "Create new analysis" button on top-right. Under the Analyze section, select "DS Version" from "Custom properties". Total number of component usages for each package version will be listed.

From there, you can break down the chart by "Project it's used in" to understand which projects are using an older version of your design system.

pkg-up

Use the --hook-script argument with the analyze command, specifying the path to your script:

npx @omlet/cli analyze --hook-script ./hook-script.js
yarn dlx @omlet/cli analyze --hook-script ./hook-script.js
pnpm dlx @omlet/cli analyze --hook-script ./hook-script.js

Feel free to customize the script based on your setup—reach us at for any questions/feedback.

support@omlet.dev