UNPKG

ph-utils

Version:

js 开发工具集,前后端都可以使用(commonjs和es module)

30 lines (29 loc) 1.21 kB
/** * 读取文件内容 * @example <caption>1. 读取JSON文件, 内容为字符串列表</caption> * read<string[]>('a.json', []); * @example <caption>2. 读取JSON文件, 内容为对象</caption> * read<{ name: string }>('b.json', {}); * @param filepath 文件路径 * @param defaultValue 文件不存在时默认值, 不传则抛异常, 如果传递的是对象形式则会将结果转换为 JSON * @returns 文件内容 */ export declare function read<T>(filepath: string, defaultValue?: T): Promise<T>; /** * 写入 JSON 格式的数据到文件 * @param file 待写入的文件 * @param data 待写入的数据 * @param opts 写入配置 * @property opts.json 是否写入 JSON 格式的数据,写入数据时对数据进行 JSON 格式化,默认为:true * @property opts.format 是否在写入 json 数据时,将 JSON 数据格式化2个空格写入, 默认为 true */ export declare function write(file: string, data: any, opts?: { json: boolean; format: boolean; }): Promise<void>; /** * 根据文件的 stat 获取文件的 etag * @param filePath 文件地址 * @returns file stat etag */ export declare function statTag(filePath: string): Promise<string>;