Command-line interface tool for Taro, a cross-platform framework that enables developers to build apps for Mini Programs, Web, and mobile platforms
—
Core utility functions for path resolution, package management, file operations, and development helpers.
Essential utilities for path resolution and version management within the Taro CLI ecosystem.
/**
* Get the root path of the Taro CLI package
* @returns Absolute path to CLI package root directory
*/
function getRootPath(): string;
/**
* Get the current version of the Taro CLI package
* @returns Version string from package.json
*/
function getPkgVersion(): string;
/**
* Get a specific field from the CLI package.json
* @param key - Package.json field key to retrieve
* @returns Field value or empty object if key doesn't exist
*/
function getPkgItemByKey(key: string): any;
/**
* Print the Taro CLI version to console with emoji
*/
function printPkgVersion(): void;Usage Examples:
import { getRootPath, getPkgVersion, getPkgItemByKey } from "@tarojs/cli";
// Get CLI root path
const cliRoot = getRootPath();
console.log(cliRoot); // /path/to/node_modules/@tarojs/cli
// Get CLI version
const version = getPkgVersion();
console.log(version); // "4.1.6"
// Get package dependencies
const deps = getPkgItemByKey('dependencies');
console.log(deps); // { "@tarojs/service": "...", ... }
// Print version with formatting
printPkgVersion(); // 👽 Taro v4.1.6Comprehensive file system operations for directory traversal and file management.
/**
* Get all files in a folder recursively with optional filtering
* @param folder - Directory path to scan
* @param filter - Optional array of filenames to exclude
* @returns Promise resolving to array of file paths
*/
function getAllFilesInFolder(
folder: string,
filter?: string[]
): Promise<string[]>;
/**
* File stat information interface
*/
interface FileStat {
/** File or directory name */
name: string;
/** Whether item is a directory */
isDirectory: boolean;
/** Whether item is a file */
isFile: boolean;
}
/**
* Read directory contents with file type information
* @param folder - Directory path to read
* @returns Array of file stat objects
*/
function readDirWithFileTypes(folder: string): FileStat[];Usage Examples:
import { getAllFilesInFolder, readDirWithFileTypes } from "@tarojs/cli";
// Get all files recursively
const allFiles = await getAllFilesInFolder('./src');
console.log(allFiles); // ['/src/app.tsx', '/src/pages/index.tsx', ...]
// Get files excluding certain patterns
const filteredFiles = await getAllFilesInFolder('./src', ['.DS_Store', 'Thumbs.db']);
// Read directory with file types
const dirContents = readDirWithFileTypes('./src');
dirContents.forEach(item => {
console.log(`${item.name}: ${item.isDirectory ? 'DIR' : 'FILE'}`);
});Utilities for handling template sources and determining template types.
/**
* Template source type enumeration
*/
type TemplateSourceType = 'git' | 'url';
/**
* Determine the type of template source from URL
* @param url - Template URL or reference string
* @returns Template source type
*/
function getTemplateSourceType(url: string): TemplateSourceType;Usage Examples:
import { getTemplateSourceType } from "@tarojs/cli";
// Git-based templates
getTemplateSourceType('github:user/repo'); // 'git'
getTemplateSourceType('gitlab:user/repo'); // 'git'
getTemplateSourceType('direct:https://github.com/user/repo.git'); // 'git'
// URL-based templates
getTemplateSourceType('https://example.com/template.zip'); // 'url'
getTemplateSourceType('/local/path/template'); // 'url'Helper functions for development workflow and console interaction.
/**
* Print development tips for specific platforms
* @param platform - Target platform name
*/
function printDevelopmentTip(platform: string): void;
/**
* Clear the console output
*/
function clearConsole(): void;
/**
* Execute shell command with callbacks for output handling
* @param params - Command execution parameters
*/
function execCommand(params: {
/** Command to execute */
command: string;
/** Callback for successful output */
successCallback?: (data: string) => void;
/** Callback for error output */
failCallback?: (data: string) => void;
}): void;Usage Examples:
import { printDevelopmentTip, clearConsole, execCommand } from "@tarojs/cli";
// Print platform-specific development tips
printDevelopmentTip('weapp');
// Output: Tips: 预览模式生成的文件较大,设置 NODE_ENV 为 production 可以开启压缩...
// Clear console for better UX
clearConsole();
// Execute command with output handling
execCommand({
command: 'npm install',
successCallback: (data) => console.log('Install output:', data),
failCallback: (error) => console.error('Install error:', error)
});Utilities for package name handling and validation.
/**
* Extract package name by removing version suffix
* @param pkgString - Package string with possible version
* @returns Package name without version
*/
function getPkgNameByFilterVersion(pkgString: string): string;
/**
* Check if value is null or undefined
* @param value - Value to check
* @returns True if value is null or undefined
*/
function isNil(value: any): value is null | undefined;Usage Examples:
import { getPkgNameByFilterVersion, isNil } from "@tarojs/cli";
// Extract package names
getPkgNameByFilterVersion('@tarojs/cli@4.1.6'); // '@tarojs/cli'
getPkgNameByFilterVersion('react@18.0.0'); // 'react'
getPkgNameByFilterVersion('@scoped/package'); // '@scoped/package'
// Null/undefined checking
isNil(null); // true
isNil(undefined); // true
isNil(0); // false
isNil(''); // false
isNil(false); // falseBase class for template creation and project scaffolding functionality.
/**
* Base creator class for template and project scaffolding
*/
class Creator {
protected _rootPath: string;
public rootPath: string;
/**
* Initialize creator with optional source root
* @param sourceRoot - Optional source root path
*/
constructor(sourceRoot?: string);
/**
* Initialize creator instance (override in subclasses)
*/
init(): void;
/**
* Set or get the source root path
* @param rootPath - Optional root path to set
* @returns Current root path
*/
sourceRoot(rootPath?: string): string;
/**
* Get template file path relative to templates directory
* @param args - Path segments to join
* @returns Complete template file path
*/
templatePath(...args: string[]): string;
/**
* Write template files (override in subclasses)
*/
write(): void;
}Usage Examples:
import { Creator } from "@tarojs/cli";
// Create custom creator
class CustomCreator extends Creator {
init() {
console.log('Initializing custom creator');
}
write() {
const templatePath = this.templatePath('custom', 'template.js');
// Write template files...
}
}
// Use creator
const creator = new CustomCreator('/custom/root');
creator.init();
// Get template paths
const jsTemplate = creator.templatePath('javascript', 'component.js');
const vueTemplate = creator.templatePath('vue', 'component.vue');Utilities for environment detection and system interaction.
/**
* Common system and environment checking utilities
*/
interface SystemUtilities {
/** Check if running on Windows */
isWindows: boolean;
/** Get user home directory */
getUserHomeDir(): string;
/** Check Node.js version compatibility */
checkNodeVersion(required: string): boolean;
/** Detect package manager in project */
detectPackageManager(projectPath: string): NpmType;
}
/**
* Command execution utilities for cross-platform compatibility
*/
interface CommandUtilities {
/** Get appropriate command for platform */
getPlatformCommand(command: string): string;
/** Execute command with platform-specific handling */
executePlatformCommand(command: string): Promise<string>;
/** Build command with proper escaping */
buildCommand(cmd: string, args: string[]): string;
}Common error handling patterns and utilities for CLI operations.
/**
* Common CLI error types
*/
type CLIErrorType =
| 'FILE_NOT_FOUND'
| 'PERMISSION_DENIED'
| 'INVALID_CONFIG'
| 'NETWORK_ERROR'
| 'TEMPLATE_ERROR'
| 'BUILD_ERROR';
/**
* CLI error class with context information
*/
class CLIError extends Error {
code: CLIErrorType;
context?: any;
constructor(message: string, code: CLIErrorType, context?: any);
}
/**
* Error handling utilities
*/
interface ErrorUtilities {
/** Handle and format errors for console output */
handleError(error: Error): void;
/** Create user-friendly error messages */
formatError(error: CLIError): string;
/** Log error with context */
logError(error: Error, context?: any): void;
}Install with Tessl CLI
npx tessl i tessl/npm-tarojs--cli