Rails-inspired generator system that provides scaffolding for your apps
npx @tessl/cli install tessl/npm-yeoman-generator@7.5.0Yeoman Generator is a Rails-inspired generator system that provides scaffolding for applications. It offers a comprehensive framework for building interactive command-line tools that can scaffold entire applications, copy boilerplate files, prompt users for preferences, and generate tailored project structures.
npm install yeoman-generatorimport Generator from "yeoman-generator";For ESM with typed import:
import Generator, { type BaseOptions, type BaseFeatures, Storage } from "yeoman-generator";For CommonJS:
const Generator = require("yeoman-generator");import Generator from "yeoman-generator";
export default class MyGenerator extends Generator {
// Define lifecycle methods
async prompting() {
this.answers = await this.prompt([
{
type: "input",
name: "name",
message: "Your project name",
default: this.appname
}
]);
}
writing() {
this.fs.copy(
this.templatePath("index.js"),
this.destinationPath("src/index.js")
);
this.renderTemplate(
"package.json",
"package.json",
{ name: this.answers.name }
);
}
async install() {
await this.spawnCommand("npm", ["install"]);
}
}Yeoman Generator is built around several key components:
The foundational Generator class and lifecycle management system that orchestrates the entire scaffolding process.
export default class Generator<
O extends BaseOptions = BaseOptions,
F extends BaseFeatures = BaseFeatures
> extends BaseGenerator<O, F> {
constructor(options: O, features?: F);
constructor(args: string[], options: O, features?: F);
readonly options: O;
readonly env: Environment;
readonly fs: MemFsEditor;
readonly log: Logger;
readonly args: string[];
readonly appname: string;
readonly config: Storage;
readonly packageJson: Storage;
readonly yoGeneratorVersion: string;
readonly simpleGit: SimpleGit;
run(): Promise<void>;
}Template rendering, file copying, and destination management with support for EJS templating and conflict resolution.
function renderTemplate<D>(
source: string | string[],
destination?: string | string[],
templateData?: string | D,
templateOptions?: TemplateOptions,
copyOptions?: CopyOptions
): void;
function copyTemplate(...args: Parameters<MemFsEditor['copy']>): ReturnType<MemFsEditor['copy']>;
function readDestination(...args: Parameters<MemFsEditor['read']>): ReturnType<MemFsEditor['read']>;
function writeDestination(...args: Parameters<MemFsEditor['write']>): ReturnType<MemFsEditor['write']>;Interactive prompting system with storage, prefilling, and validation capabilities built on Inquirer.js.
async prompt<A extends PromptAnswers = PromptAnswers>(
questions: PromptQuestions<A>,
storage?: string | Storage
): Promise<A>;
function registerConfigPrompts(questions: QuestionRegistrationOptions[]): void;Persistent storage system for generator configurations, user preferences, and project settings.
class Storage {
constructor(name: string | undefined, fs: MemFsEditor, configPath: string, options?: StorageOptions);
get<T>(key: string): T;
set<V>(key: string | number | V, value?: V): V | undefined;
getAll(): StorageRecord;
merge(source: StorageRecord): StorageRecord;
createStorage(path: string): Storage;
}Options and arguments definition system with parsing, validation, and help generation.
function option(name: string | CliOptionSpec | CliOptionSpec[], config?: Partial<Omit<CliOptionSpec, 'name'>>): this;
function argument(name: string, config?: Partial<ArgumentSpec>): this;
function help(): string;
function usage(): string;Queue-based task execution system with custom priorities, composition, and lifecycle management.
function queueTask(task: Task): void;
function composeWith<G extends BaseGenerator = BaseGenerator>(
generator: string | { Generator: any; path: string },
options?: ComposeOptions<G>
): Promise<G | G[]>;Package.json manipulation and dependency resolution with version management.
async function addDependencies(dependencies: string | string[] | Record<string, string>): Promise<Record<string, string>>;
async function addDevDependencies(devDependencies: string | string[] | Record<string, string>): Promise<Record<string, string>>;Process spawning and command execution with both synchronous and asynchronous support.
function spawnCommand(command: string, options?: ExecaOptions): ExecaChildProcess;
function spawn(command: string, args?: readonly string[], options?: ExecaOptions): ExecaChildProcess;
function spawnCommandSync(command: string, options?: SyncOptions): ExecaSyncReturnValue;Git utilities for user information and repository operations through simple-git integration.
interface GitUtil {
name(): Promise<string | undefined>;
email(): Promise<string | undefined>;
}
readonly git: GitUtil;
readonly simpleGit: SimpleGit;interface BaseOptions {
destinationRoot?: string;
skipInstall?: boolean;
skipCheckEnv?: boolean;
ignoreVersionCheck?: boolean;
askAnswered?: boolean;
localConfigOnly?: boolean;
skipCache?: boolean;
skipLocalCache?: boolean;
description?: string;
}
interface BaseFeatures {
uniqueBy?: string;
unique?: true | 'argument' | 'namespace';
tasksMatchingPriority?: boolean;
taskPrefix?: string;
customInstallTask?: boolean | ((...args: any[]) => void | Promise<void>);
customCommitTask?: boolean | ((...args: any[]) => void | Promise<void>);
skipParseOptions?: boolean;
customPriorities?: Priority[];
inheritTasks?: boolean;
}
interface CliOptionSpec {
name: string;
type: typeof Boolean | typeof String | typeof Number | ((opt: string) => any);
required?: boolean;
alias?: string;
default?: any;
description?: string;
hide?: boolean;
storage?: string | Storage;
}
interface ArgumentSpec {
name: string;
description?: string;
required?: boolean;
optional?: boolean;
type: typeof String | typeof Number | typeof Array | typeof Object;
default?: any;
}
type StorageValue = string | number | boolean | null | undefined | StorageValue[] | StorageRecord;
type StorageRecord = Record<string, StorageValue>;