UNPKG

@morjs/utils

Version:
142 lines (141 loc) 4.95 kB
import { CompileModuleKindType } from './constants'; import { ComposeModuleScriptCommand } from './hooks'; /** * 基于可选值生成描述信息 * @param keys - 可选值 * @returns 可选值为 值1, 值2 */ export declare function validKeysMessage<M, U extends string, T extends Readonly<[U, ...U[]]>>(keys: T | string[] | { [k: string]: string; } | M): string; /** * 将 16 进制的颜色值转换成 rgb 格式 * @param hex - 16 进制的颜色值 */ export declare function hexToRgb(hex: string): { r: number; g: number; b: number; } | null; /** * 是否是浅色 * @param r - rgb 色值区域中的 red * @param g - rgb 色值区域中的 green * @param b - rgb 色值区域中的 blue */ export declare function isLightColor(r: number, g: number, b: number): boolean; /** * 设置 NPM .bin 路径以复用 npm bin 文件 * @param projectPath 项目路径 * @param env 环境变量 * @returns 添加过 NPM .bin 文件的 env */ export declare function setNPMBinPATH(projectPath: string, env: Record<string, string>): Record<string, string>; /** * 生成二维码字符串 * * @param input - 用于生成二维码的字符串 * @return 生成可用于 console 的字符串 */ export declare function generateQrcodeForTerminal(input: string): Promise<string>; /** * 将普通后缀扩展为 普通后缀和带条件后缀的集合 * 条件后缀优先级高于普通后缀 * @param exts - 后缀列表 * @param conditionalExts - 条件后缀 * @returns 普通后缀和带条件后缀的集合 */ export declare function expandExtsWithConditionalExt(exts: string[] | readonly string[], conditionalExts?: string | string[]): string[]; /** * 基于 module 类型 生成引用代码 * 规则: * 1. 如果没有 importName 则仅 import 或 require * 2. 如果只有 importName 则当做 default 引用 * 3. 如果 importName 和 importAs 都存在 且 相等 则当做 named import 引用 * @param moduleKind - module 类型, 用于生成 import 或 require 引用代码 * @param importPath - 引用地址 * @param importName - 引用名称 * @param importAs - 引用别名 * @param fileContent - 文件内容,用于辅助判断 commonjs 的情况 * @returns 生成引用代码 */ export declare function makeImportClause(moduleKind: CompileModuleKindType, importPath: string, importName?: string, importAs?: string, fileContent?: string): string; /** * 判断文件是否为 commonjs 模块 * @param fileContent - 文件内容 * @param moduleKind - 文件类型 * @param recheckWhenMatched - 文件类型 * @returns `true` or `false` */ export declare function isCommonJsModule(fileContent: string, moduleKind: CompileModuleKindType, recheckWhenMatched?: boolean, filePath?: string): Promise<boolean>; /** * 返回从 from 到 to 的相对路径 * @param from - 参考路径 * @param to - 需要转换的路径 * @param forcePosix - 是否使用 POSIX 格式路径 * @returns 相对路径 */ export declare function getRelativePath(from: any, to: any, forcePosix?: boolean): string; /** * 获取 utils 的依赖地址 * @param depName - 依赖名称 */ export declare function resolveDependency(depName: string): string; /** * 运行模块脚本并标记脚本运行状态 */ export declare function execCommands({ commands, tips, env, cwd, options, timeout, callbacks, throwOnError, verbose }: { /** * 需要执行的脚本 */ commands: ComposeModuleScriptCommand[]; /** * 脚本执行提示信息 */ tips?: string; /** * 脚本执行环境变量 */ env?: Record<string, string>; /** * 脚本执行选项,参考 execa 的 options: https://github.com/sindresorhus/execa */ options?: Record<string, any>; /** * 脚本执行超时时间,单位为毫秒,默认为 30000 */ timeout?: number; /** * 当前工作区 */ cwd: string; /** * 脚本执行回调函数 */ callbacks?: { beforeAll?: (commands: ComposeModuleScriptCommand[]) => Promise<void> | void; /** * 执行每个命令之前执行的函数 * @param command - 当前执行的脚本 * @returns 修改后的脚本及命令执行信息 */ beforeEach?: (commandStr: string, command: ComposeModuleScriptCommand) => Promise<{ command: string; info?: string; }> | { command: string; info?: string; }; afterEach?: (result: any, command: ComposeModuleScriptCommand) => Promise<void> | void; afterAll?: (commands: ComposeModuleScriptCommand[]) => Promise<void> | void; onError?: (err?: Error) => string | void; }; /** * 脚本执行失败时是否抛出异常,默认为 false */ throwOnError?: boolean; /** * 是否打印脚本执行日志,默认为 false */ verbose?: boolean; }): Promise<void>;