Core file system operations, path utilities, URL encoding/decoding, and cross-platform compatibility helpers for uni-app project management and build processes.
Utility functions for managing directories and their contents.
/**
* Empties a directory while optionally skipping specified files
* @param dir - Target directory path to empty
* @param skip - Array of filenames to skip during deletion (optional, defaults to empty array)
*/
function emptyDir(dir: string, skip?: string[]): void;Usage Example:
import { emptyDir } from "@dcloudio/uni-cli-shared";
// Empty entire directory
emptyDir('./temp');
// Empty directory but preserve specific files
emptyDir('./dist', ['manifest.json', 'config.xml']);
// Preserve multiple files and patterns
emptyDir('./output', [
'package.json',
'README.md',
'.gitkeep'
]);Cross-platform path normalization and manipulation utilities.
/**
* Normalizes file paths for cross-platform compatibility
* @param id - File path string to normalize
* @returns Normalized path with forward slashes
*/
function normalizePath(id: string): string;
/**
* Platform detection flag for Windows systems
*/
const isWindows: boolean;Usage Example:
import { normalizePath, isWindows } from "@dcloudio/uni-cli-shared";
// Normalize Windows-style paths to Unix-style
const windowsPath = 'C:\\Users\\Dev\\project\\src\\main.js';
const normalized = normalizePath(windowsPath);
// Result: 'C:/Users/Dev/project/src/main.js'
// Cross-platform path handling
const projectPath = isWindows
? 'C:\\projects\\uni-app'
: '/home/user/projects/uni-app';
const safePath = normalizePath(projectPath);Base64URL encoding and decoding utilities for safe URL parameter handling.
/**
* Encodes a string to base64url format (URL-safe base64)
* @param str - String to encode
* @returns Base64url encoded string
*/
function encodeBase64Url(str: string): string;
/**
* Decodes a base64url encoded string
* @param str - Base64url string to decode
* @returns Decoded string
*/
function decodeBase64Url(str: string): string;Usage Example:
import { encodeBase64Url, decodeBase64Url } from "@dcloudio/uni-cli-shared";
// Encode data for URL parameters
const userData = JSON.stringify({ name: 'Alice', id: 123 });
const encoded = encodeBase64Url(userData);
// Safe to use in URLs without additional encoding
// Decode URL parameter back to original data
const decoded = decodeBase64Url(encoded);
const user = JSON.parse(decoded); // { name: 'Alice', id: 123 }
// Handling special characters safely
const specialText = 'Hello + World / Test = Success';
const safeEncoded = encodeBase64Url(specialText);
const restored = decodeBase64Url(safeEncoded);
console.log(restored); // 'Hello + World / Test = Success'Type guards and utilities for working with Vue.js components and templates.
/**
* Type guard for Vue element nodes with specific tag names
* @param node - Vue template AST node to check
* @param tag - Expected tag name
* @returns True if node is an ElementNode with the specified tag
*/
function checkElementNodeTag(node: any, tag: string): node is ElementNode;
interface ElementNode {
type: number;
tag: string;
props: any[];
children: any[];
// Additional Vue template AST properties
}Usage Example:
import { checkElementNodeTag } from "@dcloudio/uni-cli-shared";
function processVueTemplate(node: any) {
if (checkElementNodeTag(node, 'view')) {
// TypeScript now knows node is an ElementNode with tag 'view'
console.log(`Processing view element with ${node.props.length} props`);
// Process child elements
node.children.forEach(child => {
if (checkElementNodeTag(child, 'text')) {
// Handle text elements
console.log('Found text element');
}
});
}
}Essential utility functions re-exported for convenience.
/**
* Hash function utility from hash-sum package
* @param input - Input data to hash
* @returns Hash string
*/
function hash(input: any): string;
/**
* Converts kebab-case or snake_case to camelCase
* @param str - String to convert
* @returns camelCase string
*/
function camelize(str: string): string;
/**
* Capitalizes the first letter of a string
* @param str - String to capitalize
* @returns String with first letter capitalized
*/
function capitalize(str: string): string;
/**
* Type guard to check if a value is an array
* @param value - Value to check
* @returns True if value is an array
*/
function isArray(value: any): value is any[];Usage Examples:
import { hash, camelize, capitalize, isArray } from "@dcloudio/uni-cli-shared";
// Generate consistent hash for caching
const configHash = hash({
platform: 'mp-weixin',
mode: 'production'
}); // '4a5f7c2d'
// Convert CSS property names
const cssProperty = camelize('background-color'); // 'backgroundColor'
const vueProperty = camelize('my_custom_prop'); // 'myCustomProp'
// Format user-facing strings
const title = capitalize('welcome to uni-app'); // 'Welcome to uni-app'
// Type-safe array processing
function processConfig(config: unknown) {
if (isArray(config)) {
// TypeScript knows config is an array
return config.map(item => processItem(item));
}
return processItem(config);
}The file system utilities integrate seamlessly with uni-app's build process:
// Typical usage in build scripts
import { emptyDir, normalizePath } from "@dcloudio/uni-cli-shared";
// Clean output directory before build
emptyDir('./dist');
// Normalize paths from configuration
const configPaths = projectConfig.paths.map(normalizePath);
// Ensure cross-platform compatibility
const outputPath = normalizePath(path.join(process.cwd(), 'dist'));// Development server setup
import { isWindows, normalizePath } from "@dcloudio/uni-cli-shared";
// Handle different path separators in watch patterns
const watchPatterns = [
'src/**/*.vue',
'src/**/*.js',
'src/**/*.ts'
].map(pattern => isWindows ? pattern.replace(/\//g, '\\') : pattern);
// Normalize paths for consistent processing
const srcPath = normalizePath('./src');
const publicPath = normalizePath('./static');emptyDir() is optimized for large directories and handles file system permissions gracefullynormalizePath() uses efficient string operations and caches results when possible