t-comm
Version:
专业、稳定、纯粹的工具库
134 lines (129 loc) • 3.82 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var nodejs_crypto = require('../nodejs/crypto.js');
var nodejs_fs = require('../nodejs/fs.js');
var nodejs_os = require('../nodejs/os.js');
/**
* 写入文件
* @param {string} file 文件地址
* @param {any} data 文件数据
* @param {boolean} [isJson] 是否需要 json 序列化
* @example
* ```ts
* writeFileSync('a', 'b.txt', false);
*
* writeFileSync({ a: 1 }, 'b.json', true);
* ```
*/
function writeFileSync(file, data) {
var isJson = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
var _a;
var fileData = isJson ? JSON.stringify(data, null, 2) : data;
var eol = nodejs_os.getOs().EOL || '\n';
if (!((_a = fileData === null || fileData === void 0 ? void 0 : fileData.endsWith) === null || _a === void 0 ? void 0 : _a.call(fileData, eol))) {
fileData += eol;
}
nodejs_fs.getFs().writeFileSync(file, fileData, {
encoding: 'utf-8'
});
}
/**
* 读取文件
* @param {string} file 文件地址
* @param {boolean} [isJson] 是否需要 json 反序列化
* @returns {any} 文件内容
* @example
* ```ts
* readFileSync('b.txt', false);
*
* readFileSync('b.json', true);
* ```
*/
function readFileSync(file) {
var isJson = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
var content = nodejs_fs.getFs().readFileSync(file, {
encoding: 'utf-8'
});
var result = content;
if (isJson) {
try {
result = JSON.parse(content);
} catch (e) {}
}
return result;
}
function isDirectory() {
var filePath = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
var stat = nodejs_fs.getFs().lstatSync(filePath);
return stat.isDirectory();
}
function ensureDir(dir) {
if (!nodejs_fs.getFs().existsSync(dir)) {
nodejs_fs.getFs().mkdirSync(dir, {
recursive: true
});
}
}
/**
* 计算本地文件的哈希值
* @param {string} filePath 文件路径
* @param {'md5' | 'sha1' | 'sha256'} [algorithm='md5'] 哈希算法
* @returns {string | null} 哈希值(hex 字符串);文件不存在或读取失败时返回 null
* @example
* ```ts
* const md5 = getLocalFileHash('/path/to/file');
* const sha256 = getLocalFileHash('/path/to/file', 'sha256');
* ```
*/
function getLocalFileHash(filePath) {
var algorithm = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'md5';
var fs = nodejs_fs.getFs();
if (!filePath || !fs.existsSync(filePath)) {
return null;
}
try {
var buffer = fs.readFileSync(filePath);
return nodejs_crypto.getCrypto().createHash(algorithm).update(buffer).digest('hex');
} catch (e) {
return null;
}
}
/**
* 计算本地文件的 MD5
* @param {string} filePath 文件路径
* @returns {string | null} MD5 值(hex 字符串);文件不存在或读取失败时返回 null
* @example
* ```ts
* const md5 = getLocalFileMd5('/path/to/file');
* ```
*/
function getLocalFileMd5(filePath) {
return getLocalFileHash(filePath, 'md5');
}
/**
* 获取本地文件大小(字节数)
* @param {string} filePath 文件路径
* @returns {number | null} 文件大小(字节);文件不存在或读取失败时返回 null
* @example
* ```ts
* const size = getLocalFileSize('/path/to/file');
* ```
*/
function getLocalFileSize(filePath) {
var fs = nodejs_fs.getFs();
if (!filePath || !fs.existsSync(filePath)) {
return null;
}
try {
return fs.statSync(filePath).size;
} catch (e) {
return null;
}
}
exports.ensureDir = ensureDir;
exports.getLocalFileHash = getLocalFileHash;
exports.getLocalFileMd5 = getLocalFileMd5;
exports.getLocalFileSize = getLocalFileSize;
exports.isDirectory = isDirectory;
exports.readFileSync = readFileSync;
exports.writeFileSync = writeFileSync;