Tutorial 2: Package version tracking
Step-by-step tutorial for tracking the usage of package versions.
2
Setting up the hook script
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);
},
};3
Last updated



