Spartan CLI and workspace generators
The Spartan CLI is an Nx-backed workspace tool that connects published Spartan packages with a consuming Angular workspace. It prepares shared settings, installs required packages, generates editable UI code, reports workspace state, and provides repair or migration paths as Spartan changes.
System role
The CLI is the main workspace-facing surface for adding and maintaining Spartan capabilities. It keeps package dependencies, local styled code, import paths, and workspace layout aligned so an application does not need to connect these concerns by hand.
Workspace entry points
An Nx workspace runs the plugin through nx g @spartan-ng/cli:<name>. An Angular CLI workspace uses the same generator names through ng g @spartan-ng/cli:<name>. In both cases, the consuming workspace installs @spartan-ng/cli as a development dependency.
References: libs/…/README.md• L10-24 libs/…/README.md• L26-43 libs/…/package.json• L1-3 libs/…/generators.json• L1-16
Capability groups
The public commands are grouped by the kind of workspace change they make:
| Group | Primary generators | Workspace outcome |
|---|---|---|
| Setup | init, ui-theme | Establishes Spartan packages, theme input, and design tokens. |
| UI capabilities | ui | Adds selected Brain-backed capabilities and editable Helm code. |
| Workspace insight | info | Reports workspace type, configuration, versions, paths, and detected components. |
| Workspace repair | healthcheck | Finds known stale patterns and can apply supported fixes. |
| Release migrations | migrate-* | Moves application code, imports, and package contracts to newer Spartan releases. |
This split keeps setup, inspection, repair, and release change as separate user actions. The setup commands still use the same workspace configuration and generation context.
References: libs/…/README.md• L47-95 libs/…/generators.json• L3-16 libs/…/generators.json• L43-142
Generator architecture
Generators receive a virtual workspace tree and return file changes plus package-install tasks. Public generators define the user-facing commands. Shared rules keep paths, dependencies, imports, and generated source aligned across workspace types.
Generator registration
libs/…/generators.json maps each public name to its generator and input schema. Nx calls these generators directly. Angular CLI uses compatibility adapters that expose the same intent through the schematic interface. The package also declares an executor registration point for Nx compatibility, but consuming-workspace actions are primarily generator-led.
References: libs/…/package.json• L34-39 libs/…/generators.json• L1-16 libs/…/generators.json• L43-47 libs/…/generators.json• L144-188 libs/…/executors.json• L1-3 libs/…/compat.ts• L1-4
Shared generation context
The shared generation context resolves the destination, import alias, visual style, and library layout before it writes files. It also adds package requirements and avoids creating a capability that already has the target path mapping. Nx workspaces can use separate libraries or one shared entry point. Angular CLI workspaces use application-level paths and source inclusion instead.
References: libs/…/config.ts• L13-37 libs/…/generator.ts• L197-252 libs/…/get-target-library-directory.ts• L1-11 libs/…/initialize-angular-library.ts• L127-176
Dependency alignment
Each UI capability declares the package support it needs. The CLI combines that information with versions already declared by the consuming workspace. Current generated capabilities align Brain-backed entries with @spartan-ng/brain 1.10.0 and use cn for class merging. The CLI also matches carousel support to the consumer's Angular major when that package is selected.
This alignment keeps generated imports compatible with the packages that the workspace installs. The running CLI version supplies the Brain version when the generator must add or update that package, including release preversions.
References: libs/…/build-dependency-array.ts• L7-40 libs/…/versions.ts• L3-14 libs/…/version-utils.ts• L6-35 libs/…/supported-ui-libraries.json• L147-163 libs/…/supported-ui-libraries.json• L276-286 libs/…/supported-ui-libraries.json• L576-583
Generated style translation
Generated Helm source may use named spartan-* style tokens so one style choice can shape many components. The style transformation resolves those tokens in shared class helpers, HTML class strings, inline templates, and component variant definitions before the source reaches the consuming workspace.
Most tokens become Tailwind classes and are removed from the generated source. A small allowlist remains because those tokens carry runtime meaning rather than only visual style. The transformation also preserves valid selector text and resolves conflicting classes so generated output remains usable by Tailwind and Angular.
References: libs/…/transform-style-map.ts• L14-40 libs/…/transform-style-map.ts• L169-220 libs/…/transform-style-map.ts• L223-339 libs/…/transform-style-map.spec.ts• L165-204 libs/…/transform-style-map.spec.ts• L358-560
Workspace lifecycle
The consuming workspace moves from setup to capability selection and then to maintenance. Each stage changes a different workspace contract: theme setup prepares global styles, UI generation adds packages and local source, and maintenance keeps existing code aligned with newer Spartan releases.
Initialization and theme setup
init adds the base package set and starts theme setup with Tailwind support. The theme flow selects an application and a supported base theme, then adds the Spartan preset and design tokens to the application styles entry point. A workspace can run ui-theme again to add or change a prefixed theme.
References: libs/…/generator.ts• L10-30 libs/…/generator.ts• L8-15 libs/…/generator.ts• L49-96 libs/…/add-theme-to-application-styles.ts• L15-101 libs/…/colors.ts• L1-18
UI capability installation
ui accepts one capability name, all, or an interactive selection. A capability is a named UI feature such as code-block, input-otp, questionnaire, or trace. The generator adds required dependent capabilities, records the needed packages, and places editable Helm source in the configured components path. Brain supplies shared behavior where needed. The consuming workspace owns the generated styled layer.
The supported registry covers the current UI surface and its peer package needs. It also defines the shared utils capability used by generated Helm code to merge class sources through cn and the Brain class helper. Generated templates keep behavior, style, configuration tokens, and workspace import aliases in the local Helm layer. The CLI UI library generation page explains the generated UI layer in more detail.
The following diagram shows how a capability choice becomes local source and package links in the consuming workspace.
flowchart LR
A[CLI ui command] --> B[Workspace configuration]
B --> C[Selected capability]
C --> D[Required packages]
C --> E[Local Helm source]
E --> F[Workspace import mapping]
D --> G[Application imports]
F --> G
References: libs/…/generator.ts• L16-50 libs/…/generator.ts• L53-128 libs/…/primitive-deps.ts• L3-70 libs/…/primitives.ts• L1-67 libs/…/schema.json• L7-23 libs/…/supported-ui-libraries.json• L147-163 libs/…/supported-ui-libraries.json• L276-286 libs/…/supported-ui-libraries.json• L576-583 libs/…/hlm.ts.template• L1-27 libs/…/hlm-accordion-trigger.ts.template• L1-41
Workspace inspection
info gives engineers and tools a read-only view of the consuming workspace. It resolves configuration defaults when components.json is absent, reads declared package versions, identifies the workspace type, finds a likely Tailwind stylesheet, and detects installed Spartan components from TypeScript import paths. It reports the available capability list from the same registry used by UI generation.
The inspection path does not create or modify components.json. This makes it safe to use for diagnostics before a setup or migration command.
References: libs/…/collect-info.ts• L4-35 libs/…/collect-info.ts• L52-70 libs/…/collect-info.ts• L88-129 libs/…/collect-info.ts• L132-167 libs/…/generator.spec.ts• L49-68 libs/…/generator.spec.ts• L89-113
Maintenance and migration
healthcheck runs a set of named checks over the workspace. It reports stale patterns, marks some findings as fixable, and can ask before applying a safe fix or apply fixes with --autoFix. Named migration generators handle release-specific source, import, and package changes. The workspace maintenance generators page covers this repair and migration surface.
The class-merging migration moves generated Spartan sources from clsx and tailwind-merge to cn. It replaces untouched generated hlm.ts files with the shared template, rewrites supported imports in generated Helm libraries, and leaves custom or unsupported uses for manual work. It removes old packages only when no source in the workspace still uses them. It also aligns Brain with the running CLI when the new shared helper requires a newer Brain contract.
The healthcheck and migration share the same scope and parsing rules. This lets a fix remove a finding when the change is safe while preserving a clear warning when a developer must finish the change by hand. Other migrations update component markup, imports, package structure, or local libraries. The consuming workspace still installs the resulting packages through its package manager.
References: libs/…/generator.ts• L42-118 libs/…/cn.ts• L8-43 libs/…/generator.ts• L65-92 libs/…/generator.ts• L94-140 libs/…/generator.ts• L241-274 libs/…/legacy-imports.ts• L1-14 libs/…/source-fingerprint.ts• L11-50 libs/…/generator.spec.ts• L42-147
Configuration model
components.json is the shared record for local component destinations and import names. It also selects the Nx library layout and build mode. Generators can create missing configuration, but invalid values are surfaced because later commands depend on the same destination and alias.
Workspace configuration
The main settings have these roles:
| Setting | Role | Scope |
|---|---|---|
componentsPath | Root for generated local UI code. | Nx and Angular CLI |
importAlias | Prefix for application imports and path mappings. | Nx and Angular CLI |
style | Visual style used by generated Helm code. | Nx and Angular CLI |
buildable | Controls whether Nx creates buildable component libraries. | Nx |
generateAs | Chooses one library per component or a shared entry point. | Nx |
References: libs/…/config.ts• L13-37 libs/…/config.ts• L91-100 libs/…/config.ts• L102-170 libs/…/generator.ts• L84-121
Workspace type handling
The same generator intent works in two workspace shapes. Nx can create and remove named projects. Angular CLI uses application-level path mappings and source inclusion instead. The compatibility layer passes the workspace mode into shared generators so the user-facing commands remain consistent.
References: libs/…/generator.ts• L16-22 libs/…/generator.ts• L84-121 libs/…/generator.ts• L54-87 libs/…/generator.ts• L154-182
Repository tooling boundary
The repository also has a maintainer-facing Nx plugin named @spartan-ng/tools. It builds, documents, and releases Spartan itself. It is not the installation surface for a consuming application.
Repository generators
The tools plugin generates source entry points, stories, documentation scaffolds, snippets, previews, contributor updates, and release changes. These commands operate on Spartan's own source and generated artifacts. Their lifecycle is covered in workspace maintenance generators.
References: libs/…/generators.json• L1-59 libs/…/package.json• L1-20 package.json• L15-29
Repository executors
An Nx executor runs a task for a target such as a build, documentation job, or publish step. @spartan-ng/tools registers executors for UI documentation and release work. Consuming workspaces use the CLI generators rather than CLI executors.
References: libs/…/executors.json• L1-19 libs/…/executors.json• L1-3 libs/…/package.json• L37-39
Change checklist
When these files or sections are changed, remember to consider these:
- When a UI capability changes, keep its registry entry, dependency graph, generator entry point, and generated exports aligned.
- When the components.json contract changes, update both Nx and Angular CLI parsing because the workspace layouts differ.
- When a package entry point or import path changes, update the related migration and healthcheck so consumers have both a repair path and a report path.
- When a wrapper must retain a Brain directive, keep its files in the migration and healthcheck exclusion contract.
- When a public generator changes, keep its Nx registration, Angular CLI adapter, and schema aligned.
- When a Brain-backed template changes, keep its registry metadata and generated imports aligned with the Brain package version and import alias.
- When class merging changes, update the
cndependency contract, the shared hlm.ts template, the migration, and the healthcheck together. - When generated style tokens change, update the style map transform and its handling of runtime placeholders together.
- When
infochanges, keep its read-only behavior and its reports for configuration, package versions, stylesheets, and installed capabilities aligned.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| components.json has an invalid alias or layout value | Generation or migration can stop before changing the workspace. | Validate the file with the workspace-aware CLI rules. |
| The configured style entry point is missing | Theme setup cannot add the preset and theme variables. | Use a valid stylesEntryPoint or keep the application style entry in its build configuration. |
| A capability appears in the registry without matching dependencies or a local generator | Selection can succeed but installation can fail. | Keep registry metadata, dependency data, generator entry points, and templates in sync. |
A selected local library is customized before migrate-helm-libraries runs | The migration can replace the selected local Helm source. | Preserve custom work before confirming that migration. |
| Consumer code keeps deprecated imports or markup | The workspace can keep an old contract after a release. | Run the matching migration or healthcheck; wrapper files are excluded by design. |
| A registry package version does not match the installed Brain or Angular package | Generated source can use a contract the workspace cannot resolve. | Keep registry peer versions and generated dependency rules aligned with the consumer's package majors. |
Generated Helm code still imports clsx or tailwind-merge in an unsupported form | migrate-cn leaves the code and legacy package in place for manual work. | Resolve the cn healthcheck finding and migrate unsupported imports by hand. |
| A customized hlm.ts matches no known generated fingerprint | The migration changes supported imports but keeps the custom class-merging behavior. | Review the warning and move the custom behavior to the supported cn or Brain contract. |
| A style token is removed even though it carries runtime meaning | Generated behavior can lose a selector or state marker. | Keep runtime tokens in the style placeholder allowlist and cover them with transform tests. |
info scans a workspace with a non-standard stylesheet or import alias | The report can miss the stylesheet or installed component. | Treat the report as best-effort and check the workspace paths when the result is incomplete. |
Key files
| File | Why to read it |
|---|---|
| libs/…/README.md | Public purpose, install steps, command groups, and Brain/Helm ownership. |
| libs/…/package.json | Published plugin identity and generator, schematic, and executor registration. |
| libs/…/generators.json | Public generator names, schemas, and Nx/Angular CLI entry points. |
| libs/…/generator.ts | Entry point for selecting and coordinating UI capability generation. |
| libs/…/primitive-deps.ts | Dependency relationships between selected capabilities. |
| libs/…/supported-ui-libraries.json | Capability names and peer package requirements used during installation. |
libs/cli/src/generators/ui/libs/utils/files/lib/hlm.ts.template | Shared generated class helper that joins cn with Brain behavior. |
libs/cli/src/generators/ui/libs/accordion/files/lib/hlm-accordion-trigger.ts.template | Example of generated Helm source that exposes Brain behavior through local styled UI. |
| libs/…/generator.ts | Shared workspace behavior for paths, source, dependencies, and layout choices. |
| libs/…/build-dependency-array.ts | Package alignment rules for Angular, Brain, cn, icons, and selected peers. |
| libs/…/transform-style-map.ts | Translation of generated style tokens into workspace-ready classes. |
| libs/…/config.ts | components.json defaults, validation, and workspace configuration creation. |
| libs/…/version-utils.ts | Version selection for the running CLI and packages already installed in the workspace. |
| libs/…/collect-info.ts | Read-only workspace inspection and capability detection. |
| libs/…/generator.ts | Initial package and theme setup flow. |
| libs/…/generator.ts | Theme selection and application setup. |
| libs/…/generator.ts | Healthcheck orchestration and fix approval flow. |
| libs/…/cn.ts | Detection and repair boundary for legacy class-merging packages. |
| libs/…/generator.ts | Migration of generated Helm code and package contracts to cn. |
| libs/…/generator.ts | Replacement flow for installed local Helm libraries. |
| libs/…/generators.json | Maintainer-facing generator surface. |
| libs/…/executors.json | Maintainer-facing task runners for documentation and release work. |