t-comm
Version:
专业、稳定、纯粹的工具库
86 lines (79 loc) • 2 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var fs = require('fs');
function _interopNamespace(e) {
if (e && e.__esModule) return e;
var n = Object.create(null);
if (e) {
Object.keys(e).forEach(function (k) {
if (k !== 'default') {
var d = Object.getOwnPropertyDescriptor(e, k);
Object.defineProperty(n, k, d.get ? d : {
enumerable: true,
get: function () { return e[k]; }
});
}
});
}
n["default"] = e;
return Object.freeze(n);
}
var fs__namespace = /*#__PURE__*/_interopNamespace(fs);
/**
* 写入文件
* @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, isJson) {
if (isJson === void 0) {
isJson = false;
}
var fileData = isJson ? JSON.stringify(data, null, 2) : data;
fs__namespace.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, isJson) {
if (isJson === void 0) {
isJson = false;
}
var content = fs__namespace.readFileSync(file, {
encoding: 'utf-8'
});
var result = content;
if (isJson) {
try {
result = JSON.parse(content);
} catch (e) {}
}
return result;
}
function isDirectory(filePath) {
if (filePath === void 0) {
filePath = '';
}
var stat = fs__namespace.lstatSync(filePath);
return stat.isDirectory();
}
exports.isDirectory = isDirectory;
exports.readFileSync = readFileSync;
exports.writeFileSync = writeFileSync;