or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-lerna--init

Create a new Lerna repo or upgrade an existing repo to the current version of Lerna

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/@lerna/init@6.6.x

To install, run

npx @tessl/cli install tessl/npm-lerna--init@6.6.0

index.mddocs/

Lerna Init

Lerna Init creates new Lerna repositories or upgrades existing repositories to the current version of Lerna. It handles the initialization of monorepo project structures by creating essential configuration files, setting up directory structures, and configuring workspace dependencies.

Package Information

  • Package Name: @lerna/init
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install @lerna/init
  • Node Version: ^14.17.0 || >=16.0.0

Core Imports

import initFactory from "@lerna/init";
import { InitCommand } from "@lerna/init";
import command from "@lerna/init/command";

CommonJS:

const initFactory = require("@lerna/init");
const { InitCommand } = require("@lerna/init");
const command = require("@lerna/init/command");

Basic Usage

import initFactory from "@lerna/init";

// Create InitCommand instance using factory
const initCommand = initFactory(process.argv);

// Execute initialization
await initCommand.execute();

Using InitCommand directly:

import { InitCommand } from "@lerna/init";

// Create command instance
const initCommand = new InitCommand(process.argv);
await initCommand.execute();

Using as command module:

import command from "@lerna/init/command";

// Command configuration for CLI
const initModule = {
  command: "init",
  describe: "Create a new Lerna repo or upgrade an existing repo to the current version of Lerna",
  builder: command.builder,
  handler: command.handler
};

Architecture

Lerna Init is built around several key components:

  • Factory Function: Main export creates InitCommand instances with provided arguments
  • InitCommand Class: Core command class extending Lerna's base Command class
  • Configuration Management: Handles lerna.json and package.json creation/updates
  • File System Operations: Creates directory structures and essential files
  • Git Integration: Optionally initializes Git repositories when needed

Capabilities

Command Factory

Creates new InitCommand instances for repository initialization.

/**
 * Factory function that creates new InitCommand instances
 * @param argv - Command line arguments from Node.js process
 * @returns InitCommand instance ready for execution
 */
function factory(argv: NodeJS.Process["argv"]): InitCommand;

InitCommand Class

Main command class that handles Lerna repository initialization logic.

/**
 * Command class for initializing Lerna repositories
 * Extends the base Command class from @lerna/core
 */
class InitCommand extends Command<InitCommandOptions> {
  /** Whether to use exact version matching for dependencies */
  exact?: boolean;
  
  /** Version of Lerna to install in the repository */
  lernaVersion: string;
  
  /** Whether Git repository is required (always false for init) */
  get requiresGit(): boolean;
  
  /**
   * Initialize the command, optionally creating Git repository
   * @returns Promise that resolves when initialization is complete
   */
  override initialize(): Promise<void>;
  
  /**
   * Execute the initialization process
   * @returns Promise that resolves when all initialization steps complete
   */
  override execute(): Promise<void>;
  
  /**
   * Skip validation phase (overridden for init command)
   */
  runValidations(): void;
  
  /**
   * Skip preparation phase (overridden for init command)
   */
  runPreparations(): void;
  
  /**
   * Ensure .gitignore file exists with node_modules exclusion
   * @returns Promise that resolves when .gitignore is created or exists
   */
  ensureGitIgnore(): Promise<void>;
  
  /**
   * Create or update lerna.json and package.json configuration files
   * @returns Promise that resolves when configuration is complete
   */
  ensureConfig(): Promise<void | Package>;
  
  /**
   * Create packages directory structure for monorepo
   * @returns Promise that resolves when directories are created
   */
  ensurePackagesDir(): Promise<void[]>;
}

Command Module Configuration

Yargs command module configuration for CLI integration.

/**
 * Yargs command module for the init command
 */
interface CommandModule {
  /** Command name used in CLI */
  command: "init";
  
  /** Command description for help text */
  describe: "Create a new Lerna repo or upgrade an existing repo to the current version of Lerna";
  
  /** Command options builder configuration */
  builder: {
    exact: {
      describe: "Specify lerna dependency version in package.json without a caret (^)";
      type: "boolean";
    };
    independent: {
      describe: "Version packages independently";
      alias: "i";
      type: "boolean";
    };
  };
  
  /**
   * Command handler that executes the init command
   * @param argv - Parsed command line arguments  
   * @returns InitCommand instance created by factory
   */
  handler(argv: any): InitCommand;
}

Core Dependencies

Types and interfaces from the Lerna core system.

/**
 * Base command class from @lerna/core that InitCommand extends
 */
abstract class Command<T extends CommandConfigOptions> {
  constructor(argv: NodeJS.Process["argv"]);
  abstract initialize(): Promise<void>;
  abstract execute(): Promise<void>;
}

/**
 * Package representation from @lerna/core
 */
interface Package {
  name: string;
  version: string;
  location: string;
  dependencies?: Record<string, string>;
  devDependencies?: Record<string, string>;
  serialize(): Promise<void>;
}

/**
 * Base command configuration options from @lerna/core
 */
interface CommandConfigOptions {
  _?: string[];
  concurrency?: number;
  sort?: boolean;
  maxBuffer?: number;
  stream?: boolean;
  loglevel?: string;
  verbose?: boolean;
  progress?: boolean;
  npmClient?: string;
}

/**
 * Options interface for InitCommand configuration
 */
interface InitCommandOptions extends CommandConfigOptions {
  /** Use exact version matching for lerna dependency (no caret prefix) */
  exact: boolean;
  
  /** Version of Lerna to install as dependency */
  lernaVersion: string;
}

Command Line Usage

The init command supports the following options:

--exact

Specify lerna dependency version in package.json without a caret (^) prefix.

lerna init --exact

This creates exact version matching and saves the setting in lerna.json:

{
  "command": {
    "init": {
      "exact": true
    }
  }
}

--independent / -i

Version packages independently rather than using fixed versioning.

lerna init --independent
# or
lerna init -i

This sets the version field in lerna.json to "independent".

Files Created/Modified

lerna.json

Main Lerna configuration file created with:

  • JSON schema reference
  • Version number (0.0.0 or "independent")
  • Workspace configuration
  • Package glob patterns
  • Nx integration settings

package.json

Root package.json created/updated with:

  • Private flag set to true
  • Workspaces configuration
  • Lerna devDependency
  • Root package name

.gitignore

Git ignore file created with node_modules exclusion if not present.

packages/

Directory structure created for monorepo packages.

Integration Notes

  • Git Integration: Optionally initializes Git repository if not present
  • Workspace Support: Configures npm workspaces by default
  • Nx Integration: Enables Nx integration unless explicitly disabled
  • Schema Validation: Includes JSON schema reference for IDE support
  • Dependency Management: Handles both dependencies and devDependencies placement