mirror of
https://github.com/pengzhanbo/vuepress-theme-plume.git
synced 2026-04-23 10:58:13 +08:00
chore: improve cli code comments
This commit is contained in:
parent
17646708b1
commit
7751e4c798
@ -1,3 +1,15 @@
|
||||
/**
|
||||
* VuePress Theme Plume CLI Entry Point
|
||||
*
|
||||
* VuePress Theme Plume CLI 入口文件
|
||||
*
|
||||
* This module provides command-line interface for creating and initializing
|
||||
* VuePress projects with vuepress-theme-plume.
|
||||
*
|
||||
* 本模块提供用于创建和初始化 VuePress 项目的命令行接口。
|
||||
*
|
||||
* @module cli
|
||||
*/
|
||||
import cac from 'cac'
|
||||
import { version } from '../package.json'
|
||||
import { Mode } from './constants.js'
|
||||
|
||||
@ -1,5 +1,10 @@
|
||||
import type { Locale } from '../types.js'
|
||||
|
||||
/**
|
||||
* English locale configuration for CLI prompts and messages.
|
||||
*
|
||||
* CLI 提示和消息的英语本地化配置。
|
||||
*/
|
||||
export const en: Locale = {
|
||||
'question.root': 'Where would you want to initialize VuePress?',
|
||||
'question.site.name': 'Site Name:',
|
||||
|
||||
@ -2,6 +2,15 @@ import type { Langs, Locale } from '../types.js'
|
||||
import { en } from './en.js'
|
||||
import { zh } from './zh.js'
|
||||
|
||||
/**
|
||||
* Locale configurations for different languages.
|
||||
*
|
||||
* 不同语言的本地化配置。
|
||||
*
|
||||
* Maps language codes to their respective locale strings.
|
||||
*
|
||||
* 将语言代码映射到相应的本地化字符串。
|
||||
*/
|
||||
export const locales: Record<Langs, Locale> = {
|
||||
'zh-CN': zh,
|
||||
'en-US': en,
|
||||
|
||||
@ -1,5 +1,10 @@
|
||||
import type { Locale } from '../types.js'
|
||||
|
||||
/**
|
||||
* Chinese (Simplified) locale configuration for CLI prompts and messages.
|
||||
*
|
||||
* CLI 提示和消息的简体中文本地化配置。
|
||||
*/
|
||||
export const zh: Locale = {
|
||||
'question.root': '您想在哪里初始化 VuePress?',
|
||||
'question.site.name': '站点名称:',
|
||||
|
||||
@ -5,6 +5,14 @@ import _sortPackageJson from 'sort-package-json'
|
||||
import { Mode } from './constants.js'
|
||||
import { readJsonFile, resolve } from './utils/index.js'
|
||||
|
||||
/**
|
||||
* Sort package.json fields in a consistent order.
|
||||
*
|
||||
* 按一致顺序排序 package.json 字段。
|
||||
*
|
||||
* @param json - Package.json object to sort / 要排序的 package.json 对象
|
||||
* @returns Sorted package.json object / 排序后的 package.json 对象
|
||||
*/
|
||||
function sortPackageJson(json: Record<any, any>) {
|
||||
return _sortPackageJson(json, {
|
||||
sortOrder: ['name', 'type', 'version', 'private', 'description', 'packageManager', 'author', 'license', 'scripts', 'devDependencies', 'dependencies', 'pnpm'],
|
||||
@ -111,12 +119,29 @@ export async function createPackageJson(
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get user information from git global configuration.
|
||||
*
|
||||
* 从 git 全局配置获取用户信息。
|
||||
*
|
||||
* @returns User information object with username and email / 包含用户名和邮箱的用户信息对象
|
||||
* @throws Error if git command fails / 如果 git 命令失败则抛出错误
|
||||
*/
|
||||
async function getUserInfo() {
|
||||
const { output: username } = await spawn('git', ['config', '--global', 'user.name'])
|
||||
const { output: email } = await spawn('git', ['config', '--global', 'user.email'])
|
||||
return { username, email }
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the version of a package manager.
|
||||
*
|
||||
* 获取包管理器的版本。
|
||||
*
|
||||
* @param pkg - Package manager name (npm, yarn, pnpm) / 包管理器名称
|
||||
* @returns Version string of the package manager / 包管理器的版本字符串
|
||||
* @throws Error if package manager command fails / 如果包管理器命令失败则抛出错误
|
||||
*/
|
||||
async function getPackageManagerVersion(pkg: string) {
|
||||
const { output } = await spawn(pkg, ['--version'])
|
||||
return output
|
||||
|
||||
@ -78,6 +78,15 @@ export async function run(mode: Mode, root?: string): Promise<void> {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve prompt result into final configuration data.
|
||||
*
|
||||
* 将提示结果解析为最终配置数据。
|
||||
*
|
||||
* @param result - Prompt result from user input / 用户输入的提示结果
|
||||
* @param mode - Operation mode (init or create) / 操作模式(初始化或创建)
|
||||
* @returns Resolved configuration data / 解析后的配置数据
|
||||
*/
|
||||
function resolveData(result: PromptResult, mode: Mode): ResolvedData {
|
||||
return {
|
||||
...result,
|
||||
|
||||
@ -1,6 +1,19 @@
|
||||
import type { PackageManager } from '../types.js'
|
||||
import process from 'node:process'
|
||||
|
||||
/**
|
||||
* Detect the current package manager from environment variables.
|
||||
*
|
||||
* 从环境变量检测当前使用的包管理器。
|
||||
*
|
||||
* @returns The detected package manager name / 检测到的包管理器名称
|
||||
* @example
|
||||
* // When using pnpm
|
||||
* const pm = getPackageManager() // returns 'pnpm'
|
||||
*
|
||||
* // When using npm
|
||||
* const pm = getPackageManager() // returns 'npm'
|
||||
*/
|
||||
export function getPackageManager(): PackageManager {
|
||||
const name = process.env?.npm_config_user_agent || 'npm'
|
||||
return name.split('/')[0] as PackageManager
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user