Abstraction layer for package managers with support for NPM and Yarn operations, providing unified interface for package installation and command execution.
Base package manager abstraction providing common functionality for all package managers.
/**
* Base package manager abstraction
*/
class BasePackageManager {
/**
* Initialize package manager with executable name
* @param executableName - Name of package manager executable
*/
constructor(executableName: string);
/**
* Install packages using the package manager
* @param packages - Array of package names to install (empty for install all)
* @param workingDirectory - Directory to run command in
* @param command - Package manager command to use
* @returns Promise resolving when installation completes
*/
install(packages?: string[], workingDirectory?: string, command?: string): Promise<void>;
/**
* Run package manager command with arguments
* @param command - Command to execute
* @param args - Command arguments
* @param workingDirectory - Directory to run command in
* @returns Promise resolving when command completes
*/
run(command: string, args?: string[], workingDirectory?: string): Promise<void>;
/**
* Get executable path for the package manager
* @param directory - Directory to search from
* @returns Executable path or null if not found
*/
getExecutablePath(directory: string): string | null;
/**
* Check if package manager is available in directory
* @param directory - Directory to check
* @returns True if package manager is available
*/
isAvailable(directory: string): boolean;
// Properties
executableName: string;
proc?: ChildProcess;
}Usage Examples:
const { BasePackageManager } = require("aurelia-cli");
// Create custom package manager
class CustomPackageManager extends BasePackageManager {
constructor() {
super("custom-pm");
}
}
const pm = new CustomPackageManager();
// Check availability
if (pm.isAvailable(process.cwd())) {
console.log("Custom package manager is available");
}
// Install packages
await pm.install(["lodash", "moment"]);
// Run custom command
await pm.run("audit", ["--level", "moderate"]);NPM package manager implementation extending BasePackageManager.
/**
* NPM package manager implementation
*/
class NPM extends BasePackageManager {
/**
* Initialize NPM package manager
*/
constructor();
}Usage Examples:
const { NPM } = require("aurelia-cli");
// Create NPM instance
const npm = new NPM();
// Check if NPM is available
if (npm.isAvailable(process.cwd())) {
console.log("NPM is available");
// Install all dependencies
await npm.install();
// Install specific packages
await npm.install(["express", "lodash"]);
// Install dev dependencies
await npm.install(["@types/node", "typescript"], process.cwd(), "install --save-dev");
// Run NPM scripts
await npm.run("run", ["build"]);
await npm.run("run", ["test"]);
// Run other NPM commands
await npm.run("audit", ["fix"]);
await npm.run("outdated");
}Yarn package manager implementation with Yarn-specific installation logic.
/**
* Yarn package manager implementation
*/
class Yarn extends BasePackageManager {
/**
* Initialize Yarn package manager
*/
constructor();
/**
* Install packages with Yarn-specific logic
* @param packages - Array of package names (empty for install all)
* @param workingDirectory - Directory to run command in
* @returns Promise resolving when installation completes
*/
install(packages?: string[], workingDirectory?: string): Promise<void>;
}Usage Examples:
const { Yarn } = require("aurelia-cli");
// Create Yarn instance
const yarn = new Yarn();
// Check if Yarn is available
if (yarn.isAvailable(process.cwd())) {
console.log("Yarn is available");
// Install all dependencies (uses 'install' command)
await yarn.install();
// Install specific packages (uses 'add' command)
await yarn.install(["react", "react-dom"]);
// Run Yarn scripts
await yarn.run("run", ["dev"]);
await yarn.run("run", ["build"]);
// Run other Yarn commands
await yarn.run("upgrade");
await yarn.run("why", ["lodash"]);
}Utility functions for detecting and selecting appropriate package managers.
/**
* Detect available package managers in directory
* @param directory - Directory to check
* @returns Array of available package manager names
*/
function detectPackageManagers(directory: string): string[];
/**
* Get preferred package manager for project
* @param directory - Project directory
* @returns Preferred package manager instance
*/
function getPreferredPackageManager(directory: string): BasePackageManager;
/**
* Package manager priority order:
* 1. yarn.lock present -> Yarn
* 2. package-lock.json present -> NPM
* 3. Yarn available -> Yarn
* 4. NPM available -> NPM
* 5. Error if none available
*/Usage Examples:
const { NPM, Yarn } = require("aurelia-cli");
// Detect available package managers
function detectPackageManagers(directory) {
const managers = [];
if (new NPM().isAvailable(directory)) {
managers.push("npm");
}
if (new Yarn().isAvailable(directory)) {
managers.push("yarn");
}
return managers;
}
// Get preferred package manager
function getPreferredPackageManager(directory) {
const fs = require("fs");
const path = require("path");
// Check for lock files
if (fs.existsSync(path.join(directory, "yarn.lock"))) {
return new Yarn();
}
if (fs.existsSync(path.join(directory, "package-lock.json"))) {
return new NPM();
}
// Check availability
if (new Yarn().isAvailable(directory)) {
return new Yarn();
}
if (new NPM().isAvailable(directory)) {
return new NPM();
}
throw new Error("No package manager available");
}
// Usage
const projectDir = process.cwd();
const available = detectPackageManagers(projectDir);
console.log("Available package managers:", available);
const preferred = getPreferredPackageManager(projectDir);
console.log("Using package manager:", preferred.executableName);
await preferred.install();Package managers handle cross-platform execution with proper shell and path handling.
/**
* Cross-platform considerations:
* - Windows: Executable paths are quoted and shell is enabled
* - Unix: Direct executable execution
* - Path resolution: Uses npm-which for local executable discovery
*/
interface PackageManagerProcess {
/** Child process instance */
proc: ChildProcess;
/** Process ID */
pid: number;
/** Standard input/output handling */
stdio: 'inherit' | 'pipe';
/** Working directory */
cwd: string;
/** Shell execution (Windows) */
shell: boolean;
}Usage Examples:
// Cross-platform usage
const npm = new NPM();
// Works on all platforms
await npm.install(["cross-env"], process.cwd());
// Platform-specific executable resolution
const execPath = npm.getExecutablePath(process.cwd());
console.log("NPM executable:", execPath);
// On Windows: C:\Users\...\AppData\Roaming\npm\npm.cmd
// On Unix: /usr/local/bin/npmExtended package management operations for development workflows.
/**
* Advanced package operations
*/
interface AdvancedPackageOperations {
/** Install packages with specific version */
installVersion(packageName: string, version: string): Promise<void>;
/** Install packages from git repository */
installFromGit(repository: string, branch?: string): Promise<void>;
/** Install packages from local path */
installFromPath(localPath: string): Promise<void>;
/** Uninstall packages */
uninstall(packages: string[]): Promise<void>;
/** Update packages to latest versions */
update(packages?: string[]): Promise<void>;
/** List installed packages */
list(depth?: number): Promise<PackageInfo[]>;
}
interface PackageInfo {
name: string;
version: string;
dependencies?: PackageInfo[];
}Usage Examples:
// Advanced NPM operations
const npm = new NPM();
// Install specific version
await npm.run("install", ["lodash@4.17.21"]);
// Install from git
await npm.run("install", ["git+https://github.com/user/repo.git"]);
// Install dev dependencies
await npm.run("install", ["--save-dev", "typescript", "@types/node"]);
// Uninstall packages
await npm.run("uninstall", ["unused-package"]);
// Update packages
await npm.run("update");
// List packages
await npm.run("list", ["--depth=0"]);
// Advanced Yarn operations
const yarn = new Yarn();
// Add with specific version
await yarn.run("add", ["react@^17.0.0"]);
// Add dev dependencies
await yarn.run("add", ["--dev", "webpack", "babel-loader"]);
// Remove packages
await yarn.run("remove", ["old-package"]);
// Upgrade packages
await yarn.run("upgrade");
// Check why package is installed
await yarn.run("why", ["lodash"]);