This directory contains utility scripts for managing and analyzing the cmsds-open-data-components library.
This script automatically generates a comprehensive markdown inventory report for the cmsds-open-data-components library.
Run the script using npm:
npm run generate:inventoryOr run it directly:
node scripts/generate-inventory.cjsThe script generates COMPONENTS_INVENTORY.md in the root directory containing:
- Inventory Table: Complete list of all components, services, templates, hooks, contexts, utilities, types, and assets
- Public Export Status: Indicates which items are publicly exported vs internal-only
- Storybook Status: Shows which items have Storybook stories for visual documentation
- Unit Test Status: Shows which items have unit test coverage
- Quality Metrics: Summary statistics for documentation and testing coverage
- Export Summary: Count of public vs internal items by category
The script automatically scans the following directories:
src/components/- React componentssrc/templates/- Page-level templatessrc/services/- API service hookssrc/utilities/- Utility functionssrc/types/- TypeScript type definitionssrc/assets/- Static assets and data files
It also automatically detects:
- Hooks: Directories or files starting with
use(e.g.,useScrollToTop) - Contexts: Files ending with
Contextthat containcreateContext()calls
- Reads
src/index.tsto determine which items are publicly exported - Scans each directory for subdirectories and files
- Dynamically discovers hooks and contexts by naming patterns and file content
- Checks for
.stories.*files to determine Storybook coverage - Checks for
.test.*and.spec.*files to determine unit test coverage - Calculates statistics for overall project quality metrics
- Generates markdown with formatted tables and summaries
The script is designed to automatically adapt to changes in the codebase:
- New components/templates/services/hooks/contexts are automatically discovered
- Public export status updates when
src/index.tschanges - Story and test status updates as files are added/removed
- Statistics recalculate automatically
No manual updates needed when adding or removing hooks and contexts!
Hooks are detected by:
- Directory names starting with
use(e.g.,src/components/useScrollToTop/) - File names starting with
use(e.g.,useAddLoginLink.ts)
Contexts are detected by:
- File names ending with
Context(e.g.,HeaderContext.tsx) - File must contain a
createContext()call - Export name is extracted from
export defaultorexport conststatements
The scanner automatically excludes test and story files to prevent false positives.
The script handles several special cases:
- DatasetAdditionalInformation: Exports
buildRowsfunction - DatasetTableTab: Exported as
DatasetTable - Datatable: Exported as
DataTable(case difference) - frequencyMap: Commented out in exports
- aca.ts: Exports
acaToParamsfunction
To add new special cases, update the scanning functions in generate-inventory.cjs.
scripts/
generate-inventory.cjs # Component inventory script
generate-inventory.test.js # Tests for inventory script
generate-usage-report.cjs # Component usage report script
generate-usage-report.test.js # Tests for usage report script
README.md # This file
COMPONENTS_INVENTORY.md # Generated inventory (root directory)
COMPONENT_USAGE_REPORT.md # Generated usage report (root directory, when run in dependent projects)
SAMPLE_COMPONENT_USAGE_REPORT.md # Sample usage report (root directory, when run locally)
This script analyzes projects that use cmsds-open-data-components as a dependency and generates a comprehensive report showing which components are being used and where.
From a project that has @civicactions/cmsds-open-data-components as a dependency:
npx generate-usage-reportOr add to your project's scripts in package.json:
{
"scripts": {
"usage-report": "generate-usage-report"
}
}Then run:
npm run usage-reportRun the script directly in this repository to generate a sample report:
npm run generate:usage-reportOr run it directly:
node scripts/generate-usage-report.cjsWhen run in the library repository, it generates a sample report (SAMPLE_COMPONENT_USAGE_REPORT.md) by analyzing Storybook files.
The script generates a markdown report containing:
- Library Version: Version of cmsds-open-data-components being analyzed
- Summary Statistics: Total unique components, import statements, and files analyzed
- Category Breakdown: Components grouped by type (Component, Template, Hook, Context, Service, Utility)
- Component Usage Details: Table showing each component with:
- Component name (linked to GitHub repository)
- Category/type
- Number of times imported
- List of files where it's used
The script scans the following directories in your project:
src/- Main source directoryapp/- Next.js app directorypages/- Next.js pages directorycomponents/- Components directorytemplates/- Templates directory
It searches for these file types:
.js,.jsx- JavaScript/React files.ts,.tsx- TypeScript/React files
The script automatically excludes:
node_modules/- Dependenciesdist/,build/- Build output directories.next/,.cache/- Framework cache directories
- Detects execution context: Determines if running in the library repo or a dependent project
- Scans project files: Recursively searches for JavaScript/TypeScript files
- Parses imports: Uses regex to find imports from
@civicactions/cmsds-open-data-components - Tracks usage: Records each component, where it's imported, and at what line number
- Categorizes components: Automatically categorizes by naming patterns:
- Hooks: Names starting with
use - Contexts: Names ending with
Context - Templates: From
templates/directory - Services: From
services/directory - Utilities: From
utilities/directory - Components: Everything else
- Hooks: Names starting with
- Generates report: Creates formatted markdown with statistics and detailed usage tables
- Adds GitHub links: Links each component name to its source in the GitHub repository
Components are automatically categorized based on naming conventions and source location:
- Hook: Components starting with
use(e.g.,useScrollToTop,useDatastore) - Context: Components ending with
Context(e.g.,ACAContext) - Template: Components from the
templates/directory - Service: Components from the
services/directory - Utility: Components from the
utilities/directory - Component: All other React components
In the library repository: Generates SAMPLE_COMPONENT_USAGE_REPORT.md by analyzing how components are used in Storybook files. This serves as an example of the report format.
In dependent projects: Generates COMPONENT_USAGE_REPORT.md by analyzing actual component usage throughout the project's codebase.
- Node.js v14 or higher
- No additional dependencies required (uses Node.js built-in
fsandpathmodules)
Error: "require is not defined"
- The script uses
.cjsextension to work with the ES module package type - Make sure the file is named
generate-usage-report.cjs(not.js)
No components found in scan
- Verify that
@civicactions/cmsds-open-data-componentsis installed as a dependency - Check that your import statements use the full package name
- Ensure source files are in scanned directories (
src/,app/,pages/,components/,templates/)
Missing some component usages
- The script only detects standard ES6 import syntax
- Dynamic imports (e.g.,
import()) are not currently detected - Ensure imports use the package name:
from '@civicactions/cmsds-open-data-components'
- Node.js v14 or higher
- No additional dependencies required (uses Node.js built-in
fsandpathmodules)
Error: "require is not defined"
- The script uses
.cjsextension to work with the ES module package type - Make sure the file is named
generate-inventory.cjs(not.js)
Missing components in output
- Ensure new components follow the standard directory structure
- Component directories should be in
src/components/ - Each component should have an
index.tsxor similar entry file
Incorrect public export status
- Check if the component is exported in
src/index.ts - For special export names, add them to the scanning logic in the script