lazy-js-utils
Version:
A collection of lazy-loaded JavaScript utilities for efficient development
161 lines (143 loc) • 5.27 kB
TypeScript
import { I as IShellMessage, P as PkgTool, N as NodeWorkerPayload } from '../types-D4gI4rGL.js';
import { StdioOptions, SpawnOptions } from 'node:child_process';
/**
* 将文件拷贝到另一个目录
* @param urls 需要被拷贝的文件路径
* @param destination 目录
* @returns IShellMessage
*/
declare function fileCopy(urls: string[], destination: string): Promise<IShellMessage>;
/**
* 获取npm包导出的文件内容
* @param { string } url npm包名
* @returns
*/
declare function getExportBundle(url: string): Promise<string>;
interface IPackage {
name: string;
version: string;
description?: string;
main?: string;
module?: string;
types?: string;
files?: string[];
scripts?: Record<string, string>;
}
/**
* 获取当前package.json 对象
* @param { string } url 路径 默认 ./package.json
* @returns 返回json格式package.json
*/
declare function getPkg(url?: string): Promise<IPackage & Record<string, any>>;
/**
* 获取当前包管理器 ‘yarn’ | 'pnpm' | 'bun' | 'npm'
* @returns 返回当前package环境 ‘yarn’ | 'pnpm' | 'bun' | 'npm'
*/
declare function getPkgTool(): Promise<PkgTool>;
/**
* 执行 shell 命令的工具函数,支持单个或多个命令的执行。
*
* @template T - 指令类型,可以是字符串或字符串数组。
* @param {string | string[]} commander - 要执行的命令或命令数组。
* @param {Options | Options['stdio']} [options] - 配置选项或 stdio 配置。
* @param {string[]} [options.args] - 传递给命令的参数数组。
* @param {StdioOptions} [options.stdio] - 子进程的 stdio 配置,默认为 'pipe'。
* @param {boolean} [options.errorExit=true] - 是否在命令失败时退出进程。
* @param {boolean} [options.isLog=false] - 是否在控制台输出日志。
* @param {string} [options.cwd] - 子进程的工作目录。
* @param {SpawnOptions} [options.options] - 额外的子进程配置选项。
* @returns {Promise<IShellMessage | IShellMessage[]>} - 返回一个 Promise,解析为命令执行结果或结果数组。
*
* @throws {Error} - 如果命令执行失败且 `errorExit` 为 true,则抛出错误。
*
* @example
* // 执行单个命令
* jsShell('ls', { isLog: true })
* .then(result => console.log(result))
* .catch(error => console.error(error));
*
* @example
* // 执行多个命令
* jsShell(['ls', 'pwd'], { isLog: true })
* .then(results => console.log(results))
* .catch(error => console.error(error));
*/
interface Options {
args?: string[];
stdio?: StdioOptions | undefined;
errorExit?: boolean;
isLog?: boolean;
cwd?: string;
options?: SpawnOptions;
}
declare function jsShell<T extends string | string[]>(commander: T, options?: Options | Options['stdio']): T extends string ? Promise<IShellMessage> : Promise<IShellMessage[]>;
/**
*
* @returns 处理argv --flag如果未设置值默认为true
*/
declare function transformArgv(): Record<string, any>;
type NodeWorkReturn<T> = T extends {
params: string[];
stdio?: 'inherit' | 'pipe';
} ? IShellMessage[] : IShellMessage;
/**
* 这个api需要 dependencies 中安装lazy-js-utils
* @param { string | NodeWorkerPayload } payload 字符串 | {
params: string[]
stdio?: 'inherit' | 'pipe'
}
* @param { string } [url] 自定义worker路径
* @returns
*/
declare function useNodeWorker<T extends NodeWorkerPayload | string>(payload: T, url?: string): Promise<NodeWorkReturn<T>>;
declare function useProcressNodeWorker(callback: (data: any) => any): void;
/**
* 处理gulp任务
* @param name 任务名
* @param fn 任务函数
* @returns
*/
declare function withTaskName<T extends Function>(name: string, fn: T): T & {
displayName: string;
};
/**
* 重写文件
* @param { string } paths 路径
* @param { (content: string, index: number) => string } callback 回调接收文件字符串将返回的内容重新写入该文件
* @param { string } [encoding] 默认 'utf-8'
*/
declare function writeFile(paths: string[] | string, callback: (content: string, index: number) => string, encoding?: BufferEncoding): void;
/**
* 判断是否存在package.json
* @param { string } rootPath 绝对路径
* @returns boolean
*/
declare function hasPkg(rootPath: string): Promise<boolean>;
declare function isInstallPkg(pkg: string): Promise<boolean>;
/**
* 判断文件是否存在
* @param url
* @returns
*/
declare function isExist(url: string): boolean;
/**
* 判断是否是在go环境
* @returns
*/
declare function isGo(rootPath?: string): Promise<boolean>;
/**
* 判断是否是rust环境
*/
declare function isRust(rootPath?: string): Promise<boolean>;
/**
* 同步地测试用户对 path 指定的文件或目录的权限
* @param { string } filename 文件或目录路径
* @returns
*/
declare function isWritable(filename: string): boolean;
/**
* 判断路径下是否有package.jsons
* @param { string } rootPath 默认 process.cwd()
*/
declare function isPkg(rootPath?: string): Promise<boolean>;
export { fileCopy, getExportBundle, getPkg, getPkgTool, hasPkg, isExist, isGo, isInstallPkg, isPkg, isRust, isWritable, jsShell, transformArgv, useNodeWorker, useProcressNodeWorker, withTaskName, writeFile };