Extend a Turborepo with code generation utilities for creating workspaces and custom generators
—
Programmatic APIs for creating new workspaces in Turborepo monorepos, supporting both empty workspace creation and copying from existing templates.
Main function for creating new workspaces programmatically.
/**
* Create a new workspace in the Turborepo monorepo
* @param opts - Workspace generation options
* @returns Promise that resolves when workspace is created
*/
function workspace(opts: TurboGeneratorCLIOptions): Promise<void>;
interface TurboGeneratorCLIOptions {
/** Name for the new workspace */
name?: string;
/** Generate empty workspace (default: true) */
empty: boolean;
/** Generate using existing workspace as template */
copy?: string | boolean;
/** Where the new workspace should be created */
destination?: string;
/** Workspace type */
type?: WorkspaceType;
/** Repository root directory */
root?: string;
/** Path to example for GitHub URLs */
examplePath?: string;
/** Don't filter dependencies by workspace type */
showAllDependencies: boolean;
}
type WorkspaceType = "app" | "package";Usage Examples:
import { workspace } from "@turbo/gen/dist/commands";
// Create empty package
await workspace({
name: "my-package",
type: "package",
empty: true,
showAllDependencies: false
});
// Copy existing workspace
await workspace({
name: "new-app",
type: "app",
empty: false,
copy: "existing-app",
showAllDependencies: false
});
// Create with custom destination
await workspace({
name: "shared-ui",
type: "package",
empty: true,
destination: "packages/ui",
showAllDependencies: false
});Low-level generator functions for different workspace creation methods.
/**
* Generate an empty workspace
* @param args - Generator arguments with project and options
* @returns Promise that resolves when generation is complete
*/
function generate(args: TurboGeneratorArguments): Promise<void>;
interface TurboGeneratorArguments {
project: Project;
opts: TurboGeneratorOptions;
}
type TurboGeneratorOptions = Omit<TurboGeneratorCLIOptions, "copy" | "empty"> & {
copy: CopyData;
method: "copy" | "empty";
};/**
* Generate workspace by copying from existing template
* @param args - Generator arguments with project and options
* @returns Promise that resolves when generation is complete
*/
function generate(args: TurboGeneratorArguments): Promise<void>;
interface CopyData {
type: "internal" | "external";
source: string;
}Usage Examples:
import { generate as emptyGenerate } from "@turbo/gen/dist/generators/empty";
import { generate as copyGenerate } from "@turbo/gen/dist/generators/copy";
import { getProject } from "@turbo/gen/dist/utils/getProject";
// Generate empty workspace
const project = await getProject({ root: process.cwd() });
await emptyGenerate({
project,
opts: {
name: "my-package",
type: "package",
method: "empty",
copy: { type: "internal", source: "" },
showAllDependencies: false
}
});
// Copy workspace
await copyGenerate({
project,
opts: {
name: "new-app",
type: "app",
method: "copy",
copy: { type: "internal", source: "existing-app" },
showAllDependencies: false
}
});Utility for collecting all requirements needed for workspace creation.
/**
* Gather all requirements for workspace creation including prompts
* @param args - Generator arguments
* @returns Promise with collected workspace requirements
*/
function gatherAddRequirements(args: TurboGeneratorArguments): Promise<{
type: WorkspaceType;
name: string;
location: { absolute: string; relative: string };
source: Workspace | undefined;
dependencies: DependencyGroups;
}>;
interface DependencyGroups {
dependencies: string[];
devDependencies: string[];
peerDependencies: string[];
}Usage Examples:
import { gatherAddRequirements } from "@turbo/gen/dist/utils/gatherAddRequirements";
const requirements = await gatherAddRequirements({
project,
opts: {
name: "my-package",
type: "package",
method: "empty",
copy: { type: "internal", source: "" },
showAllDependencies: false
}
});
console.log(requirements.name); // "my-package"
console.log(requirements.location.relative); // "packages/my-package"
console.log(requirements.dependencies); // ["react", "typescript"]Get filtered lists of available workspaces.
/**
* Get filtered list of workspaces for selection
* @param args - Project and filtering options
* @returns Array of workspaces or separators for UI
*/
function getWorkspaceList(args: {
project: Project;
type: WorkspaceType;
showAllDependencies?: boolean;
}): Array<Workspace | Separator>;Analyze the structure of workspaces in the monorepo.
/**
* Analyze workspace structure in the monorepo
* @param args - Project instance
* @returns Workspace structure information
*/
function getWorkspaceStructure(args: {
project: Project;
}): WorkspaceStructure;
interface WorkspaceStructure {
hasRootApps: boolean;
hasRootPackages: boolean;
workspacesByGroup: Record<string, Array<Workspace>>;
nonAppWorkspaces: Array<Workspace>;
}
/**
* Get group name for a workspace based on its location
* @param args - Project and workspace
* @returns Group name for the workspace
*/
function getGroupFromWorkspace(args: {
project: Project;
workspace: Workspace;
}): string;Get available workspace root directories.
/**
* Get workspace root directories from project configuration
* @param args - Project instance
* @returns Array of workspace root directory paths
*/
function getWorkspaceRoots(args: {
project: Project;
}): Array<string>;Usage Examples:
import {
getWorkspaceList,
getWorkspaceStructure,
getWorkspaceRoots
} from "@turbo/gen/dist/utils";
// Get available workspaces for copying
const workspaces = getWorkspaceList({
project,
type: "package",
showAllDependencies: false
});
// Analyze workspace structure
const structure = getWorkspaceStructure({ project });
console.log(structure.hasRootApps); // true/false
console.log(structure.workspacesByGroup); // { "apps": [...], "packages": [...] }
// Get workspace roots
const roots = getWorkspaceRoots({ project });
console.log(roots); // ["apps", "packages", "tools"]Programmatic access to the same prompts used by the CLI.
/**
* Prompt for workspace name with validation
* @param args - Prompt configuration
* @returns Promise with user's answer
*/
function name(args: {
override?: string;
suggestion?: string;
workspaceType: WorkspaceType;
}): Promise<{ answer: string }>;/**
* Prompt for workspace type selection
* @param args - Prompt configuration
* @returns Promise with selected workspace type
*/
function type(args: {
override?: WorkspaceType;
message?: string;
}): Promise<{ answer: WorkspaceType }>;/**
* Determine workspace location with user input
* @param args - Location prompt configuration
* @returns Promise with absolute and relative paths
*/
function location(args: {
workspaceType: WorkspaceType;
workspaceName: string;
destination?: string;
project: Project;
}): Promise<{ absolute: string; relative: string }>;/**
* Prompt for source workspace selection when copying
* @param args - Source selection configuration
* @returns Promise with selected workspace
*/
function source(args: {
override?: string;
workspaces: Array<Workspace | Separator>;
workspaceName: string;
}): Promise<{ answer: Workspace }>;/**
* Prompt for dependency selection
* @param args - Dependency prompt configuration
* @returns Promise with selected dependency groups
*/
function dependencies(args: {
workspaceName: string;
project: Project;
workspaceSource?: Workspace;
showAllDependencies?: boolean;
}): Promise<DependencyGroups>;Usage Examples:
import {
name,
type,
location,
dependencies
} from "@turbo/gen/dist/commands/workspace/prompts";
// Get workspace name
const nameResult = await name({
workspaceType: "package",
suggestion: "my-package"
});
// Get workspace type
const typeResult = await type({
message: "What type of workspace would you like to create?"
});
// Get location
const locationResult = await location({
workspaceType: "package",
workspaceName: "my-package",
project
});
// Get dependencies
const deps = await dependencies({
workspaceName: "my-package",
project,
showAllDependencies: false
});Install with Tessl CLI
npx tessl i tessl/npm-turbo--gen